Skip to content

Commit

Permalink
fixed JsonResponder to respect status code set in domainpayload
Browse files Browse the repository at this point in the history
fixed TwigResponder to respect status code set in domainpayload
added unittests for fixes
fixes bitExpert#2
  • Loading branch information
dropdevcoding committed Jan 8, 2017
1 parent 6dae35c commit 9cb75c5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/bitExpert/Adrenaline/Responder/JsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function __invoke(Payload $payload, ResponseInterface $response)
$response = $response->withHeader($header, $value);
}

return $response->withStatus(200);
/** @var \bitExpert\Adrenaline\Domain\DomainPayload $payload */
$status = $payload->getStatus() ?: 200;

return $response->withStatus($status);
}
}
5 changes: 4 additions & 1 deletion src/bitExpert/Adrenaline/Responder/TwigResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function __invoke(Payload $payload, ResponseInterface $response)
$response = $response->withHeader($header, $value);
}

return $response->withStatus(200);
/** @var \bitExpert\Adrenaline\Domain\DomainPayload $payload */
$status = $payload->getStatus() ?: 200;

return $response->withStatus($status);
}
}
27 changes: 27 additions & 0 deletions tests/bitExpert/Adrenaline/Responder/JsonResponderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,35 @@ public function contentTypeCantBeChanged()
$domainPayload = new DomainPayload('test');
$this->responder->setHeaders(['Content-Type' => 'my/type']);
$responder = $this->responder;
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(['application/json'], $response->getHeader('Content-Type'));
}

/**
* @test
*/
public function respectsStatusCodeSetInPayload()
{
$domainPayload = (new DomainPayload('test'))->withStatus(400);
$responder = $this->responder;
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(400, $response->getStatusCode());
}

/**
* @test
*/
public function usesOkStatusCodeIfNoneSetInPayload()
{
$domainPayload = (new DomainPayload('test'));
$responder = $this->responder;
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(200, $response->getStatusCode());
}
}
28 changes: 28 additions & 0 deletions tests/bitExpert/Adrenaline/Responder/TwigResponderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,32 @@ public function contentTypeCantBeChanged()

$this->assertEquals(['text/html'], $response->getHeader('Content-Type'));
}

/**
* @test
*/
public function respectsStatusCodeSetInPayload()
{
$domainPayload = (new DomainPayload('test'))->withStatus(400);
$responder = $this->responder;
$responder->setTemplate('mytemplate.twig');
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(400, $response->getStatusCode());
}

/**
* @test
*/
public function usesOkStatusCodeIfNoneSetInPayload()
{
$domainPayload = (new DomainPayload('test'));
$responder = $this->responder;
$responder->setTemplate('mytemplate.twig');
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(200, $response->getStatusCode());
}
}

0 comments on commit 9cb75c5

Please sign in to comment.