diff --git a/composer.json b/composer.json index a26cef5..5f59143 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "ext-json": "*", "php": ">=7.4", - "laminas/laminas-diactoros": "^2.4", + "nyholm/psr7": "^1.5", "mockery/mockery": "^1.5", "phpunit/phpunit": "^8.5|^9.5", "spiral/auth": "^2.9", diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index 29fe6e8..22b579c 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -4,10 +4,11 @@ namespace Spiral\Testing\Http; -use Laminas\Diactoros\ServerRequest; -use Laminas\Diactoros\Stream; +use Nyholm\Psr7\ServerRequest; +use Nyholm\Psr7\Stream; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -17,7 +18,6 @@ use Spiral\Auth\TransportRegistry; use Spiral\Core\Container; use Spiral\Http\Http; -use Spiral\Security\ActorInterface; use Spiral\Session\SessionInterface; use Spiral\Testing\Auth\FakeActorProvider; use Spiral\Testing\Session\FakeSession; @@ -198,21 +198,33 @@ public function getWithAttributes(string $uri, array $attributes, array $headers return $this->handleRequest($r); } + /** + * @param array|object|StreamInterface $data + */ public function post( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] ): TestResponse { + $this->validateRequestData($data); + + $request = $this->createRequest($uri, 'POST', [], $headers, $cookies, $files); + return $this->handleRequest( - $this->createRequest($uri, 'POST', [], $headers, $cookies, $files)->withParsedBody($data) + $data instanceof StreamInterface + ? $request->withBody($data) + : $request->withParsedBody($data) ); } + /** + * @param array|StreamInterface $data + */ public function postJson( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] @@ -224,13 +236,19 @@ public function postJson( public function put( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] ): TestResponse { + $this->validateRequestData($data); + + $request = $this->createRequest($uri, 'PUT', [], $headers, $cookies, $files); + return $this->handleRequest( - $this->createRequest($uri, 'PUT', $data, $headers, $cookies, $files) + $data instanceof StreamInterface + ? $request->withBody($data) + : $request->withParsedBody($data) ); } @@ -248,13 +266,19 @@ public function putJson( public function delete( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] ): TestResponse { + $this->validateRequestData($data); + + $request = $this->createRequest($uri, 'DELETE', [], $headers, $cookies, $files); + return $this->handleRequest( - $this->createRequest($uri, 'DELETE', $data, $headers, $cookies, $files) + $data instanceof StreamInterface + ? $request->withBody($data) + : $request->withParsedBody($data) ); } @@ -273,21 +297,29 @@ public function deleteJson( protected function createJsonRequest( string $uri, string $method, - array $data, + $data, array $headers, array $cookies, array $files = [] ): ServerRequest { - $content = \json_encode($data); + if (!\is_array($data) && !$data instanceof StreamInterface) { + throw new \InvalidArgumentException( + \sprintf('$data should be array or instance of `%s` interface.', StreamInterface::class) + ); + } - $headers = array_merge([ - 'CONTENT_LENGTH' => mb_strlen($content, '8bit'), + if (! $data instanceof StreamInterface) { + $data = Stream::create(\json_encode($data)); + } + + $headers = \array_merge([ + 'CONTENT_LENGTH' => $data->getSize(), 'CONTENT_TYPE' => 'application/json', 'Accept' => 'application/json', ], $headers); return $this->createRequest($uri, $method, [], $headers, $cookies, $files) - ->withBody(new Stream($content)); + ->withBody($data); } /** @@ -304,22 +336,25 @@ protected function createRequest( $cookies = \array_merge($this->defaultCookies, $cookies); $headers = \array_merge($this->defaultHeaders, $headers); - return new ServerRequest( - $this->defaultServerVariables, - $files, - $uri, + $request = new ServerRequest( $method, - 'php://input', + $uri, $headers, - $cookies, - $query + 'php://input', + '1.1', + $this->defaultServerVariables ); + + return $request + ->withCookieParams($cookies) + ->withQueryParams($query) + ->withUploadedFiles($files); } protected function handleRequest(ServerRequestInterface $request, array $bindings = []): TestResponse { if ($this->actor) { - $request = $request->withHeader(static::AUTH_TOKEN_HEADER_KEY, spl_object_hash($this->actor)); + $request = $request->withHeader(static::AUTH_TOKEN_HEADER_KEY, \spl_object_hash($this->actor)); $bindings[ActorProviderInterface::class] = new FakeActorProvider($this->actor); $bindings[TokenStorageInterface::class] = new FakeTokenStorage(); @@ -341,4 +376,11 @@ protected function handleRequest(ServerRequestInterface $request, array $binding return new TestResponse($scope($handler, $bindings)); } + + protected function validateRequestData($data): void + { + if (!\is_array($data) && !\is_object($data)) { + throw new \InvalidArgumentException('$data should be an array or an object.'); + } + } } diff --git a/src/Http/File.php b/src/Http/File.php index 4bffa80..f39fe0a 100644 --- a/src/Http/File.php +++ b/src/Http/File.php @@ -4,7 +4,7 @@ namespace Spiral\Testing\Http; -use Laminas\Diactoros\UploadedFile; +use Nyholm\Psr7\UploadedFile; use Symfony\Component\Mime\MimeTypes; class File extends UploadedFile diff --git a/src/Traits/InteractsWithStorage.php b/src/Traits/InteractsWithStorage.php index faa945c..a9c00a7 100644 --- a/src/Traits/InteractsWithStorage.php +++ b/src/Traits/InteractsWithStorage.php @@ -4,7 +4,7 @@ namespace Spiral\Testing\Traits; -use Laminas\Diactoros\Uri; +use Nyholm\Psr7\Uri; use Spiral\Bootloader\Storage\StorageConfig; use Spiral\Distribution\Resolver\StaticResolver; use Spiral\Storage\StorageInterface;