Skip to content

Commit

Permalink
Merge pull request #21 from spiral/feature/http-stream-body
Browse files Browse the repository at this point in the history
Adds an ability to send Stream via fake HTTP requests
  • Loading branch information
butschster authored May 30, 2022
2 parents a062e10 + ef5d3c8 commit 7e48146
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
88 changes: 65 additions & 23 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 = []
Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -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.');
}
}
}
2 changes: 1 addition & 1 deletion src/Http/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/InteractsWithStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7e48146

Please sign in to comment.