Skip to content

Commit

Permalink
RequestEntityMapping test
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Aug 21, 2018
1 parent 39cb21f commit 4bee553
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions tests/cases/Mapping/RequestEntityMapping.phpt
@@ -0,0 +1,96 @@
<?php declare(strict_types = 1);

/**
* Test: Mapping\RequestEntityMapping
*/

require_once __DIR__ . '/../../bootstrap.php';

use Apitte\Core\Exception\Logical\InvalidStateException;
use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Http\RequestAttributes;
use Apitte\Core\Mapping\Request\IRequestEntity;
use Apitte\Core\Mapping\RequestEntityMapping;
use Apitte\Core\Schema\Endpoint;
use Apitte\Core\Schema\EndpointRequestMapper;
use Contributte\Psr7\Psr7ResponseFactory;
use Contributte\Psr7\Psr7ServerRequestFactory;
use Tester\Assert;
use Tests\Fixtures\Mapping\Request\FooEntity;
use Tests\Fixtures\Mapping\Request\InvalidEntity;

// Add entity to request
test(function (): void {
$request = new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal());
$response = Psr7ResponseFactory::fromGlobal();
$mapping = new RequestEntityMapping();

$endpoint = new Endpoint();
$mapper = new EndpointRequestMapper();
$mapper->setEntity(FooEntity::class);
$endpoint->setRequestMapper($mapper);

$request = $request->withAttribute(RequestAttributes::ATTR_ENDPOINT, $endpoint);

Assert::equal(
$request->withAttribute(RequestAttributes::ATTR_REQUEST_ENTITY, new FooEntity()),
$mapping->map($request, $response)
);
});

// Don't modify request by entity - method foo is not supported
test(function (): void {
$request = new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal());
$response = Psr7ResponseFactory::fromGlobal();
$mapping = new RequestEntityMapping();

$endpoint = new Endpoint();
$mapper = new EndpointRequestMapper();
$mapper->setEntity(FooEntity::class);
$endpoint->setRequestMapper($mapper);

$request = $request->withAttribute(RequestAttributes::ATTR_ENDPOINT, $endpoint);
$request = $request->withMethod('foo');

Assert::same($request, $mapping->map($request, $response));
});

// Exception - invalid entity
test(function (): void {
$request = Psr7ServerRequestFactory::fromSuperGlobal();
$response = Psr7ResponseFactory::fromGlobal();
$mapping = new RequestEntityMapping();

$endpoint = new Endpoint();
$mapper = new EndpointRequestMapper();
$mapper->setEntity(InvalidEntity::class);
$endpoint->setRequestMapper($mapper);

$request = $request->withAttribute(RequestAttributes::ATTR_ENDPOINT, $endpoint);

Assert::exception(function () use ($mapping, $request, $response): void {
$request = $mapping->map($request, $response);
}, InvalidStateException::class, sprintf('Instantiated entity "%s" does not implement "%s"', InvalidEntity::class, IRequestEntity::class));
});

// No request mapper, return request
test(function (): void {
$request = Psr7ServerRequestFactory::fromSuperGlobal();
$response = Psr7ResponseFactory::fromGlobal();
$mapping = new RequestEntityMapping();

$request = $request->withAttribute(RequestAttributes::ATTR_ENDPOINT, new Endpoint());

Assert::same($request, $mapping->map($request, $response));
});

// Exception - missing attribute
test(function (): void {
$request = Psr7ServerRequestFactory::fromSuperGlobal();
$response = Psr7ResponseFactory::fromGlobal();
$mapping = new RequestEntityMapping();

Assert::exception(function () use ($mapping, $request, $response): void {
$request = $mapping->map($request, $response);
}, InvalidStateException::class, sprintf('Attribute "%s" is required', RequestAttributes::ATTR_ENDPOINT));
});

0 comments on commit 4bee553

Please sign in to comment.