From 906ee700bec8eafda52a499ecf6413f60bda8674 Mon Sep 17 00:00:00 2001 From: butschster Date: Fri, 27 May 2022 16:10:53 +0400 Subject: [PATCH 1/3] Adds an ability to send Stream via fake HTTP requests --- src/Http/FakeHttp.php | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index 29fe6e8..8ad5168 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -8,6 +8,7 @@ use Laminas\Diactoros\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; @@ -198,21 +199,35 @@ 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 { + if (! \is_array($data) && ! \is_object($data)) { + throw new \InvalidArgumentException('$data should be an array or an object.'); + } + + $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 = [] @@ -273,21 +288,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) + ); + } + + if (! $data instanceof StreamInterface) { + $data = new Stream(\json_encode($data)); + } - $headers = array_merge([ - 'CONTENT_LENGTH' => mb_strlen($content, '8bit'), + $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); } /** @@ -319,7 +342,7 @@ protected function createRequest( 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(); From 9ba87e7287949202ab08a7f269aacd5a2103f07d Mon Sep 17 00:00:00 2001 From: butschster Date: Fri, 27 May 2022 17:33:15 +0400 Subject: [PATCH 2/3] Fixes PUT and DELETE requests --- src/Http/FakeHttp.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Http/FakeHttp.php b/src/Http/FakeHttp.php index 8ad5168..483e492 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -239,13 +239,21 @@ public function postJson( public function put( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] ): TestResponse { + if (! \is_array($data) && ! \is_object($data)) { + throw new \InvalidArgumentException('$data should be an array or an object.'); + } + + $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) ); } @@ -263,13 +271,21 @@ public function putJson( public function delete( string $uri, - array $data = [], + $data = [], array $headers = [], array $cookies = [], array $files = [] ): TestResponse { + if (! \is_array($data) && ! \is_object($data)) { + throw new \InvalidArgumentException('$data should be an array or an object.'); + } + + $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) ); } From ef5d3c8561763dff9740a30059cb13fdb65501d1 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Mon, 30 May 2022 13:59:15 +0300 Subject: [PATCH 3/3] Replace Laminas\Diactoros to Nyholm\Psr7 --- composer.json | 2 +- src/Http/FakeHttp.php | 45 +++++++++++++++-------------- src/Http/File.php | 2 +- src/Traits/InteractsWithStorage.php | 2 +- 4 files changed, 27 insertions(+), 24 deletions(-) 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 483e492..22b579c 100644 --- a/src/Http/FakeHttp.php +++ b/src/Http/FakeHttp.php @@ -4,8 +4,8 @@ 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; @@ -18,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; @@ -209,9 +208,7 @@ public function post( array $cookies = [], array $files = [] ): TestResponse { - if (! \is_array($data) && ! \is_object($data)) { - throw new \InvalidArgumentException('$data should be an array or an object.'); - } + $this->validateRequestData($data); $request = $this->createRequest($uri, 'POST', [], $headers, $cookies, $files); @@ -244,9 +241,7 @@ public function put( array $cookies = [], array $files = [] ): TestResponse { - if (! \is_array($data) && ! \is_object($data)) { - throw new \InvalidArgumentException('$data should be an array or an object.'); - } + $this->validateRequestData($data); $request = $this->createRequest($uri, 'PUT', [], $headers, $cookies, $files); @@ -276,9 +271,7 @@ public function delete( array $cookies = [], array $files = [] ): TestResponse { - if (! \is_array($data) && ! \is_object($data)) { - throw new \InvalidArgumentException('$data should be an array or an object.'); - } + $this->validateRequestData($data); $request = $this->createRequest($uri, 'DELETE', [], $headers, $cookies, $files); @@ -309,14 +302,14 @@ protected function createJsonRequest( array $cookies, array $files = [] ): ServerRequest { - if (! \is_array($data) && ! $data instanceof StreamInterface) { + if (!\is_array($data) && !$data instanceof StreamInterface) { throw new \InvalidArgumentException( \sprintf('$data should be array or instance of `%s` interface.', StreamInterface::class) ); } if (! $data instanceof StreamInterface) { - $data = new Stream(\json_encode($data)); + $data = Stream::create(\json_encode($data)); } $headers = \array_merge([ @@ -343,16 +336,19 @@ 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 @@ -380,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;