2
2
3
3
namespace Selective \TestTrait \Traits ;
4
4
5
+ use Psr \Container \ContainerInterface ;
6
+ use Psr \Http \Message \ResponseFactoryInterface ;
7
+ use Psr \Http \Message \ResponseInterface ;
8
+ use Psr \Http \Message \ServerRequestFactoryInterface ;
5
9
use Psr \Http \Message \ServerRequestInterface ;
6
10
use Psr \Http \Message \UriInterface ;
7
- use Slim \ Psr7 \ Factory \ ServerRequestFactory ;
11
+ use RuntimeException ;
8
12
9
13
/**
10
14
* HTTP Test Trait.
@@ -18,11 +22,19 @@ trait HttpTestTrait
18
22
* @param string|UriInterface $uri The URI
19
23
* @param array<mixed> $serverParams The server parameters
20
24
*
25
+ * @throws RuntimeException
26
+ *
21
27
* @return ServerRequestInterface The request
22
28
*/
23
29
protected function createRequest (string $ method , $ uri , array $ serverParams = []): ServerRequestInterface
24
30
{
25
- return (new ServerRequestFactory ())->createServerRequest ($ method , $ uri , $ serverParams );
31
+ if (!$ this ->container instanceof ContainerInterface) {
32
+ throw new RuntimeException ('DI container not found ' );
33
+ }
34
+
35
+ $ factory = $ this ->container ->get (ServerRequestFactoryInterface::class);
36
+
37
+ return $ factory ->createServerRequest ($ method , $ uri , $ serverParams );
26
38
}
27
39
28
40
/**
@@ -32,7 +44,7 @@ protected function createRequest(string $method, $uri, array $serverParams = [])
32
44
* @param string|UriInterface $uri The URI
33
45
* @param array<mixed>|null $data The form data
34
46
*
35
- * @return ServerRequestInterface
47
+ * @return ServerRequestInterface The request
36
48
*/
37
49
protected function createFormRequest (string $ method , $ uri , array $ data = null ): ServerRequestInterface
38
50
{
@@ -44,4 +56,25 @@ protected function createFormRequest(string $method, $uri, array $data = null):
44
56
45
57
return $ request ->withHeader ('Content-Type ' , 'application/x-www-form-urlencoded ' );
46
58
}
59
+
60
+ /**
61
+ * Create a new response.
62
+ *
63
+ * @param int $code HTTP status code; defaults to 200
64
+ * @param string $reasonPhrase Reason phrase to associate with status code
65
+ *
66
+ * @throws RuntimeException
67
+ *
68
+ * @return ResponseInterface The response
69
+ */
70
+ protected function createResponse (int $ code = 200 , string $ reasonPhrase = '' ): ResponseInterface
71
+ {
72
+ if (!$ this ->container instanceof ContainerInterface) {
73
+ throw new RuntimeException ('DI container not found ' );
74
+ }
75
+
76
+ $ factory = $ this ->container ->get (ResponseFactoryInterface::class);
77
+
78
+ return $ factory ->createResponse ($ code , $ reasonPhrase );
79
+ }
47
80
}
0 commit comments