Skip to content

Commit 84d2d47

Browse files
committed
Decouple HttpTestTrait from slim/psr7
1 parent f4c97fa commit 84d2d47

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/Traits/HttpTestTrait.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Selective\TestTrait\Traits;
44

5+
use Psr\Container\ContainerInterface;
6+
use Psr\Http\Message\ResponseFactoryInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestFactoryInterface;
59
use Psr\Http\Message\ServerRequestInterface;
610
use Psr\Http\Message\UriInterface;
7-
use Slim\Psr7\Factory\ServerRequestFactory;
11+
use RuntimeException;
812

913
/**
1014
* HTTP Test Trait.
@@ -18,11 +22,19 @@ trait HttpTestTrait
1822
* @param string|UriInterface $uri The URI
1923
* @param array<mixed> $serverParams The server parameters
2024
*
25+
* @throws RuntimeException
26+
*
2127
* @return ServerRequestInterface The request
2228
*/
2329
protected function createRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
2430
{
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);
2638
}
2739

2840
/**
@@ -32,7 +44,7 @@ protected function createRequest(string $method, $uri, array $serverParams = [])
3244
* @param string|UriInterface $uri The URI
3345
* @param array<mixed>|null $data The form data
3446
*
35-
* @return ServerRequestInterface
47+
* @return ServerRequestInterface The request
3648
*/
3749
protected function createFormRequest(string $method, $uri, array $data = null): ServerRequestInterface
3850
{
@@ -44,4 +56,25 @@ protected function createFormRequest(string $method, $uri, array $data = null):
4456

4557
return $request->withHeader('Content-Type', 'application/x-www-form-urlencoded');
4658
}
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+
}
4780
}

0 commit comments

Comments
 (0)