Skip to content

Commit

Permalink
Merge pull request #118 from koriym/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
koriym authored Oct 15, 2018
2 parents fd01405 + 3fb17d9 commit 2593ab1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
21 changes: 21 additions & 0 deletions src/Extension/Transfer/NullTransfer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);
/**
* This file is part of the BEAR.Sunday package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\Sunday\Extension\Transfer;

use BEAR\Resource\ResourceObject;

final class NullTransfer implements TransferInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(ResourceObject $ro, array $server)
{
}
}
12 changes: 6 additions & 6 deletions src/Provide/Transfer/HttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ class HttpResponder implements TransferInterface
/**
* {@inheritdoc}
*/
public function __invoke(ResourceObject $resourceObject, array $server)
public function __invoke(ResourceObject $ro, array $server)
{
unset($server);
// render
if (! $resourceObject->view) {
$resourceObject->toString();
if (! $ro->view) {
$ro->toString();
}

// header
foreach ($resourceObject->headers as $label => $value) {
foreach ($ro->headers as $label => $value) {
header("{$label}: {$value}", false);
}

// code
http_response_code($resourceObject->code);
http_response_code($ro->code);

// body
echo $resourceObject->view;
echo $ro->view;
}
}
21 changes: 13 additions & 8 deletions tests/Fake/Provide/Transfer/FakeHttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@
namespace BEAR\Sunday\Provide\Transfer;

use BEAR\Resource\ResourceObject;
use BEAR\Sunday\Extension\Transfer\TransferInterface;

class FakeHttpResponder extends HttpResponder
class FakeHttpResponder implements TransferInterface
{
public static $code;
public static $headers = [];
public static $content;
public static $body;

public static function reset()
{
static::$headers = [];
static::$content = null;
static::$body = null;
}

public function __invoke(ResourceObject $resourceObject, array $server)
public function __invoke(ResourceObject $ro, array $server)
{
ob_start();
parent::__invoke($resourceObject, $server);
$body = ob_get_clean();
self::$content = $body;
if (! $ro->view) {
self::$body = $ro->toString();
}
// header
foreach ($ro->headers as $label => $value) {
header("{$label}: {$value}", false);
}
self::$code = $ro->code;
}
}
20 changes: 10 additions & 10 deletions tests/Provide/Error/VndErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,40 @@ public function testNotFound()
{
$e = new ResourceNotFoundException('', 404);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([404], FakeHttpResponder::$code);
$this->assertSame(404, FakeHttpResponder::$code);
$this->assertSame([['content-type: application/vnd.error+json', false]], FakeHttpResponder::$headers);
$this->assertSame('{"message":"Not Found"}', FakeHttpResponder::$content);
$this->assertSame('{"message":"Not Found"}', FakeHttpResponder::$body);
}

public function testBadRequest()
{
$e = new BadRequestException('invalid-method', 400);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([400], FakeHttpResponder::$code);
$this->assertSame('{"message":"Bad Request"}', FakeHttpResponder::$content);
$this->assertSame(400, FakeHttpResponder::$code);
$this->assertSame('{"message":"Bad Request"}', FakeHttpResponder::$body);
}

public function testServerError()
{
$e = new ServerErrorException('message', 501);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([501], FakeHttpResponder::$code);
$this->assertSame('{"message":"Not Implemented"}', FakeHttpResponder::$content);
$this->assertSame(501, FakeHttpResponder::$code);
$this->assertSame('{"message":"Not Implemented"}', FakeHttpResponder::$body);
}

public function testRuntimeError()
{
$e = new \RuntimeException('message', 0);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([500], FakeHttpResponder::$code);
$this->assertSame('{"message":"500 Server Error"}', FakeHttpResponder::$content);
$this->assertSame(500, FakeHttpResponder::$code);
$this->assertSame('{"message":"500 Server Error"}', FakeHttpResponder::$body);
}

public function testServerErrorNot50X()
{
$e = new ServerErrorException('message', 0);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([500], FakeHttpResponder::$code);
$this->assertSame('{"message":"500 Server Error"}', FakeHttpResponder::$content);
$this->assertSame(500, FakeHttpResponder::$code);
$this->assertSame('{"message":"500 Server Error"}', FakeHttpResponder::$body);
}
}
6 changes: 3 additions & 3 deletions tests/Provide/Transfer/HttpResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class HttpResponderTest extends TestCase
{
/**
* @var HttpResponder
* @var FakeHttpResponder
*/
private $responder;

Expand All @@ -35,7 +35,7 @@ public function testTransfer()
];
$this->assertSame($expectedArgs, FakeHttpResponder::$headers);
$expect = '{"greeting":"hello world"}';
$actual = FakeHttpResponder::$content;
$actual = FakeHttpResponder::$body;
$this->assertSame($expect, $actual);
}

Expand All @@ -56,7 +56,7 @@ public function __toString()
];
$this->assertSame($expectedArgs, FakeHttpResponder::$headers);
$expect = '{"greeting":"hello world"}';
$actual = FakeHttpResponder::$content;
$actual = FakeHttpResponder::$body;
$this->assertSame($expect, $actual);
}
}

0 comments on commit 2593ab1

Please sign in to comment.