Skip to content

Commit

Permalink
Merge pull request #9 from equip/feature/response-codes
Browse files Browse the repository at this point in the history
Make AbstractFormatter use new Payload status codes
  • Loading branch information
elazar committed Feb 11, 2016
2 parents 118e9ae + 86b1888 commit c92c25e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 24 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"php": ">=5.5",
"arbiter/arbiter": "^0.1",
"destrukt/destrukt": "^0.8.1",
"equip/adr": "^1.0",
"equip/adr": "^1.1",
"filp/whoops": "^1.1",
"lukasoppermann/http-status": "^2",
"nikic/fast-route": "^0.7",
"rdlowrey/auryn": "^1.1",
"relay/relay": "^1.1",
Expand Down
39 changes: 28 additions & 11 deletions src/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
namespace Equip\Formatter;

use Equip\Adr\PayloadInterface;
use Lukasoppermann\Httpstatus\Httpstatus;

abstract class AbstractFormatter
{
/**
* @var Httpstatus
*/
private $http_status;

/**
* @param Httpstatus $http_status
*/
public function __construct(Httpstatus $http_status)
{
$this->http_status = $http_status;
}

/**
* Get the content types this formatter can satisfy.
*
Expand Down Expand Up @@ -46,18 +60,21 @@ public function status(PayloadInterface $payload)
{
$status = $payload->getStatus();

if ($status >= PayloadInterface::OK && $status < PayloadInterface::ERROR) {
return 200;
}

if ($status >= PayloadInterface::ERROR && $status < PayloadInterface::INVALID) {
return 500;
}

if ($status >= PayloadInterface::INVALID && $status < PayloadInterface::UNKNOWN) {
return 400;
// Legacy logic
// @todo Remove this in 2.0
if (is_int($status)) {
if ($status >= PayloadInterface::OK && $status < PayloadInterface::ERROR) {
return 200;
}
if ($status >= PayloadInterface::ERROR && $status < PayloadInterface::INVALID) {
return 500;
}
if ($status >= PayloadInterface::INVALID && $status < PayloadInterface::UNKNOWN) {
return 400;
}
return 520;
}

return 520;
return $this->http_status->getStatusCode($status);
}
}
11 changes: 8 additions & 3 deletions src/Formatter/PlatesFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Equip\Formatter;

use Equip\Adr\PayloadInterface;
use League\Plates\Engine;
use League\Plates\Template\Template;
use Equip\Adr\PayloadInterface;
use Lukasoppermann\Httpstatus\Httpstatus;

class PlatesFormatter extends HtmlFormatter
{
Expand All @@ -15,10 +16,14 @@ class PlatesFormatter extends HtmlFormatter

/**
* @param Engine $engine
* @param Httpstatus $http_status
*/
public function __construct(Engine $engine)
{
public function __construct(
Engine $engine,
Httpstatus $http_status
) {
$this->engine = $engine;
parent::__construct($http_status);
}

/**
Expand Down
19 changes: 17 additions & 2 deletions tests/Formatter/AbstractFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

namespace EquipTests\Formatter;

use Equip\Payload;
use Equip\Formatter\AbstractFormatter;
use Equip\Payload;
use Lukasoppermann\Httpstatus\Httpstatus;

class AbstractFormatterTest extends \PHPUnit_Framework_TestCase
{
public function statusCodeProvider()
{
return [
[Payload::STATUS_OK, 200],
[Payload::STATUS_CREATED, 201],
[Payload::STATUS_ACCEPTED, 202],
[Payload::STATUS_NO_CONTENT, 204],
[Payload::STATUS_MOVED_PERMANENTLY, 301],
[Payload::STATUS_FOUND, 302],
[Payload::STATUS_NOT_MODIFIED, 304],
[Payload::STATUS_BAD_REQUEST, 400],
[Payload::STATUS_UNAUTHORIZED, 401],
[Payload::STATUS_FORBIDDEN, 403],
[Payload::STATUS_NOT_FOUND, 404],

// Legacy results
// @todo Remove these in 2.0
[Payload::OK, 200],
[Payload::ERROR, 500],
[Payload::INVALID, 400],
Expand All @@ -24,7 +39,7 @@ public function testStatusCode($status, $expected)
{
$payload = (new Payload)->withStatus($status);

$formatter = $this->getMockForAbstractClass(AbstractFormatter::class);
$formatter = $this->getMockForAbstractClass(AbstractFormatter::class, [new Httpstatus]);

$this->assertEquals($expected, $formatter->status($payload));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Formatter/HtmlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Equip\Payload;
use Equip\Formatter\HtmlFormatter;
use Lukasoppermann\Httpstatus\Httpstatus;

class HtmlFormatterTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -14,7 +15,7 @@ public function testAccepts()

public function testType()
{
$formatter = $this->getMockForAbstractClass(HtmlFormatter::class);
$formatter = $this->getMockForAbstractClass(HtmlFormatter::class, [new Httpstatus]);
$this->assertEquals('text/html', $formatter->type());
}
}
15 changes: 13 additions & 2 deletions tests/Formatter/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

use Equip\Payload;
use Equip\Formatter\JsonFormatter;
use Lukasoppermann\Httpstatus\Httpstatus;

class JsonFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JsonFormatter
*/
private $formatter;

protected function setUp()
{
$this->formatter = new JsonFormatter(new Httpstatus);
}

public function testAccepts()
{
$this->assertEquals(['application/json'], JsonFormatter::accepts());
}

public function testType()
{
$this->assertEquals('application/json', (new JsonFormatter)->type());
$this->assertEquals('application/json', $this->formatter->type());
}

public function testBody()
Expand All @@ -23,7 +34,7 @@ public function testBody()
'success' => true,
]);

$body = (new JsonFormatter)->body($payload);
$body = $this->formatter->body($payload);

$this->assertEquals('{"success":true}', $body);
}
Expand Down
21 changes: 17 additions & 4 deletions tests/Formatter/PlatesFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@

namespace EquipTests\Formatter;

use League\Plates\Engine;
use Equip\Payload;
use Equip\Formatter\PlatesFormatter;
use Equip\Payload;
use League\Plates\Engine;
use Lukasoppermann\Httpstatus\Httpstatus;

class PlatesFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Engine
*/
protected $templates;

/**
* @var PlatesFormatter
*/
private $formatter;

public function setUp()
{
if (!class_exists('League\Plates\Engine')) {
$this->markTestSkipped('Plates is not installed');
}

$this->templates = new Engine(__DIR__ . '/../_templates');
$this->formatter = new PlatesFormatter(
$this->templates,
new HttpStatus
);
}

public function testAccepts()
Expand All @@ -26,7 +39,7 @@ public function testAccepts()

public function testType()
{
$this->assertEquals('text/html', (new PlatesFormatter($this->templates))->type());
$this->assertEquals('text/html', $this->formatter->type());
}

public function testResponse()
Expand All @@ -38,7 +51,7 @@ public function testResponse()
'footer' => 'footer',
]);

$body = (string) (new PlatesFormatter($this->templates))->body($payload);
$body = (string) $this->formatter->body($payload);

$this->assertEquals("<h1>header</h1>\n<p>body</p>\n<span>footer</span>\n", $body);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Responder/FormattedResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Equip\Payload;
use Equip\Responder\FormattedResponder;
use EquipTests\Configuration\ConfigurationTestCase;
use Lukasoppermann\Httpstatus\Httpstatus;
use Negotiation\Negotiator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -56,12 +57,16 @@ public function testFormatters()

public function testSorting()
{
$args = [new Httpstatus];

$a = $this->getMockBuilder(AbstractFormatter::class)
->setMockClassName('FooFormatter')
->setConstructorArgs($args)
->getMockForAbstractClass();

$b = $this->getMockBuilder(AbstractFormatter::class)
->setMockClassName('BarFormatter')
->setConstructorArgs($args)
->getMockForAbstractClass();

$values = [
Expand Down

0 comments on commit c92c25e

Please sign in to comment.