Skip to content

Commit

Permalink
Merge pull request #25 from chimeraphp/prevent-generated-id-override
Browse files Browse the repository at this point in the history
Prevent generated id override
  • Loading branch information
lcobucci committed Dec 19, 2019
1 parent 7a8346a commit b57816c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Handler/CreateAndFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$request = $request->withAttribute(
IdentifierGenerator::class,
$this->identifierGenerator->generate()
$request->getAttribute(IdentifierGenerator::class, $this->identifierGenerator->generate())
);

$input = new HttpRequest($request);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CreateOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$request = $request->withAttribute(
IdentifierGenerator::class,
$this->identifierGenerator->generate()
$request->getAttribute(IdentifierGenerator::class, $this->identifierGenerator->generate())
);

$this->action->execute(new HttpRequest($request));
Expand Down
68 changes: 58 additions & 10 deletions tests/Handler/CreateAndFetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequest;

Expand Down Expand Up @@ -65,15 +66,6 @@ public function createDependencies(): void
*/
public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse(): void
{
$handler = new CreateAndFetch(
new ExecuteCommand($this->bus, $this->creator, 'command'),
new ExecuteQuery($this->bus, $this->creator, 'query'),
new ResponseFactory(),
'info',
$this->uriGenerator,
$this->idGenerator
);

$request = new ServerRequest();
$command = (object) ['a' => 'b'];
$query = (object) ['c' => 'd'];
Expand All @@ -96,12 +88,68 @@ public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse(): void
->willReturn('/testing/1');

/** @var ResponseInterface|UnformattedResponse $response */
$response = $handler->handle($request);
$response = $this->handleRequest($request);

self::assertInstanceOf(UnformattedResponse::class, $response);
self::assertSame(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
self::assertSame('/testing/1', $response->getHeaderLine('Location'));
self::assertSame([ExecuteQuery::class => 'query'], $response->getAttributes());
self::assertSame('result', $response->getUnformattedContent());
}

/**
* @test
*
* @covers ::__construct()
* @covers ::handle()
* @covers ::generateResponse()
*
* @uses \Chimera\Routing\HttpRequest
*/
public function handleShouldPreserveTheRequestGeneratedIdIfAlreadyPresent(): void
{
$request = (new ServerRequest())->withAttribute(IdentifierGenerator::class, 2);
$command = (object) ['a' => 'b'];
$query = (object) ['c' => 'd'];

$this->creator->expects(self::exactly(2))
->method('create')
->willReturn($command, $query);

$this->bus->expects(self::exactly(2))
->method('handle')
->withConsecutive([$command], [$query])
->willReturn(null, 'result');

$this->idGenerator->method('generate')
->willReturn(1);

$this->uriGenerator->expects(self::once())
->method('generateRelativePath')
->with($request, 'info')
->willReturn('/testing/2');

/** @var ResponseInterface|UnformattedResponse $response */
$response = $this->handleRequest($request);

self::assertInstanceOf(UnformattedResponse::class, $response);
self::assertSame(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
self::assertSame('/testing/2', $response->getHeaderLine('Location'));
self::assertSame([ExecuteQuery::class => 'query'], $response->getAttributes());
self::assertSame('result', $response->getUnformattedContent());
}

private function handleRequest(ServerRequestInterface $request): ResponseInterface
{
$handler = new CreateAndFetch(
new ExecuteCommand($this->bus, $this->creator, 'command'),
new ExecuteQuery($this->bus, $this->creator, 'query'),
new ResponseFactory(),
'info',
$this->uriGenerator,
$this->idGenerator
);

return $handler->handle($request);
}
}
64 changes: 54 additions & 10 deletions tests/Handler/CreateOnlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Lcobucci\ContentNegotiation\UnformattedResponse;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequest;

Expand Down Expand Up @@ -63,15 +65,6 @@ public function createDependencies(): void
*/
public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse(): void
{
$handler = new CreateOnly(
new ExecuteCommand($this->bus, $this->creator, 'command'),
new ResponseFactory(),
'info',
$this->uriGenerator,
$this->idGenerator,
StatusCodeInterface::STATUS_CREATED
);

$request = new ServerRequest();
$command = (object) ['a' => 'b'];

Expand All @@ -91,10 +84,61 @@ public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse(): void
->with($request->withAttribute(IdentifierGenerator::class, 1), 'info')
->willReturn('/testing/1');

$response = $handler->handle($request);
$response = $this->handleRequest($request);

self::assertNotInstanceOf(UnformattedResponse::class, $response);
self::assertSame(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
self::assertSame('/testing/1', $response->getHeaderLine('Location'));
}

/**
* @test
*
* @covers ::__construct()
* @covers ::handle()
* @covers ::generateResponse()
*
* @uses \Chimera\Routing\HttpRequest
*/
public function handleShouldPreserveTheRequestGeneratedIdIfAlreadyPresent(): void
{
$request = (new ServerRequest())->withAttribute(IdentifierGenerator::class, 2);
$command = (object) ['a' => 'b'];

$this->creator->expects(self::once())
->method('create')
->willReturn($command);

$this->bus->expects(self::once())
->method('handle')
->with($command);

$this->idGenerator->method('generate')
->willReturn(1);

$this->uriGenerator->expects(self::once())
->method('generateRelativePath')
->with($request, 'info')
->willReturn('/testing/2');

$response = $this->handleRequest($request);

self::assertNotInstanceOf(UnformattedResponse::class, $response);
self::assertSame(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
self::assertSame('/testing/2', $response->getHeaderLine('Location'));
}

private function handleRequest(ServerRequestInterface $request): ResponseInterface
{
$handler = new CreateOnly(
new ExecuteCommand($this->bus, $this->creator, 'command'),
new ResponseFactory(),
'info',
$this->uriGenerator,
$this->idGenerator,
StatusCodeInterface::STATUS_CREATED
);

return $handler->handle($request);
}
}

0 comments on commit b57816c

Please sign in to comment.