Repository navigation
Expand file tree
/
Copy pathTestCase.php
More file actions
130 lines (113 loc) · 3.27 KB
/
Copy pathTestCase.php
File metadata and controls
130 lines (113 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2021 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Testing;
use DI\Container;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Slim\App;
use Slim\Psr7\Factory\ServerRequestFactory;
use UserFrosting\UserFrosting;
/**
* Case for test that requires the full App instance.
* This can be used for HTTP testing against the real deal.
*/
class TestCase extends BaseTestCase
{
use CustomAssertionsTrait;
/**
* The global container object, which holds all services.
*/
protected Container $ci;
/**
* The Slim App Instance.
*/
protected App $app;
/**
* The UF app instance.
*/
protected UserFrosting $userfrosting;
/**
* String reference to SprinkleRecipe.
*/
protected string $mainSprinkle;
/**
* Setup the test environment.
*
* @codeCoverageIgnore
*/
protected function setUp(): void
{
parent::setUp();
$this->refreshApplication();
}
/**
* Refresh the application instance by setting up a basic UF app.
*
* @codeCoverageIgnore
*/
protected function refreshApplication(): void
{
$this->userfrosting = new UserFrosting($this->mainSprinkle);
$this->app = $this->userfrosting->getApp();
$this->ci = $this->userfrosting->getContainer();
}
/**
* Create a server request.
*
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param mixed[] $serverParams The server parameters
*
* @return ServerRequestInterface
* @codeCoverageIgnore
*/
protected function createRequest(
string $method,
string|UriInterface $uri,
array $serverParams = []
): ServerRequestInterface {
$request = new ServerRequestFactory();
return $request->createServerRequest($method, $uri, $serverParams);
}
/**
* Create a JSON request.
*
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param mixed[]|null $data The json data
*
* @return ServerRequestInterface
* @codeCoverageIgnore
*/
protected function createJsonRequest(
string $method,
string|UriInterface $uri,
?array $data = null
): ServerRequestInterface {
$request = $this->createRequest($method, $uri);
if ($data !== null) {
$request = $request->withParsedBody($data);
}
return $request->withHeader('Accept', 'application/json');
}
/**
* Handle request and returns the response.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
* @codeCoverageIgnore
*/
protected function handleRequest(ServerRequestInterface $request): ResponseInterface
{
return $this->app->handle($request);
}
}