Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-alpha.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Jan 21, 2015
2 parents 9cbf2a0 + b7a9afa commit 740c7d5
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 59 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"BEAR\\Sunday\\": "src/"
}
},
"autoload-dev": {
"files": ["tests/Fake/functions.php"]
},
"extra": {
"branch-alias": {
"dev-develop-2": "1.0.x-dev"
Expand Down
5 changes: 1 addition & 4 deletions docs/demo/MyVendor/HelloWorld/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@
$page()->transfer($app->responder, $_SERVER);

} catch (\Exception $e) {
$code = $e->getCode() ?: 500;
http_response_code($code);
echo $code;
error_log($e);
$app->error->handle($e, $request)->transfer();
}
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<description>The BEAR coding standard.</description>
<!-- 2. General -->
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
<rule ref="Generic.Strings.UnnecessaryStringConcat"/>
<!-- Include the whole PSR-2 standard -->
<rule ref="PSR2">
Expand Down
10 changes: 8 additions & 2 deletions src/Extension/Error/ErrorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
*/
namespace BEAR\Sunday\Extension\Error;

use BEAR\Resource\ResourceObject;
use BEAR\Sunday\Extension\Router\RouterMatch as Request;

interface ErrorInterface
{
/**
* Handle exception
*
* @param \Exception $e
* @param Request $request
*
* @return ResourceObject
* @return $this
*/
public function handle(\Exception $e, Request $request);

/**
* Error page transfer
*/
public function transfer();
}
8 changes: 8 additions & 0 deletions src/Extension/Router/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ interface RouterInterface extends ExtensionInterface
* @return RouterMatch
*/
public function match(array $globals, array $server);

/**
* @param string $name The route name to look up.
* @param array $data The data to interpolate into the URI; data keys map to param tokens in the path.
*
* @return mixed Returns a URI when it finds a name, or boolean false if there is no route name.
*/
public function generate($name, $data);
}
68 changes: 28 additions & 40 deletions src/Provide/Error/VndError.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,54 @@
use BEAR\Sunday\Extension\Router\RouterMatch as Request;

/**
* vnd.error
* Vnd.Error media type error
*
* @see https://github.com/blongden/vnd.error
*/
class VndError implements ErrorInterface
{
/**
* @var ErrorPage
* @var int
*/
private $error;
private $code;

public function __construct()
{
$this->error = new ErrorPage;
$this->error->headers['Content-Type'] = 'application/vnd.error+json';
$this->error->headers = [];
}
/**
* @var string
*/
const HEADER = 'Content-Type: application/vnd.error+json';

/**
* {@inheritdoc}
* @var array
*/
public function handle(\Exception $e, Request $request)
{
if ($e instanceof NotFound || $e instanceof BadRequest) {
return $this->codeError($e);
}
return $this->serverError($e, $request);
}
private $body = [];

/**
* @param \Exception $e
*
* @return ErrorPage
* {@inheritdoc}
*/
private function codeError(\Exception $e)
public function handle(\Exception $e, Request $request)
{
$code = $e->getCode();
$this->error->code = $code;
$message = $code . ' ' . (new Code)->statusText[$code];
$this->error->body = ['message' => $message];
$isCodeError = ($e instanceof NotFound || $e instanceof BadRequest || $e instanceof ServerErrorException);
if ($isCodeError) {
$this->code = $e->getCode();
$this->body = ['message' => (new Code)->statusText[$this->code]];

return $this;
}
$this->code = 500;
$this->body = ['message' => '500 Server Error'];
error_log($request);
error_log($e);

return $this->error;
return $this;
}

/**
* @param \Exception $e
* @param Request $request
*
* @return ErrorPage
* {@inheritdoc}
*/
private function serverError(\Exception $e, Request $request)
public function transfer()
{
if ($e instanceof ServerErrorException) {
return $this->codeError($e);
}
$this->error->code = 500;
$this->error->body = ['message' => '500 Server Error'];
error_log((string) $request);
error_log($e);

return $this->error;
http_response_code($this->code);
header(self::HEADER);
echo json_encode($this->body);
}
}
8 changes: 8 additions & 0 deletions src/Provide/Router/WebRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public function match(array $globals, array $server)

return $request;
}

/**
* {@inheritdoc}
*/
public function generate($name, $data)
{
return false;
}
}
33 changes: 33 additions & 0 deletions tests/Fake/Provide/Error/FakeVndError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* test header function is taken from Aura.Web
* @see https://github.com/auraphp/Aura.Web/blob/a1a4e45d14b21d40d716d341b78a050e1905cc05/tests/unit/src/FakeResponseSender.php
*/
namespace BEAR\Sunday\Provide\Error;

use BEAR\Resource\ResourceObject;

require_once __DIR__ . '/header.php';

class FakeVndError extends VndError
{
public static $code = [];
public static $headers = [];
public static $content;

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

public function transfer()
{
ob_start();
parent::transfer();
$body = ob_get_clean();
self::$content = $body;
}
}
11 changes: 11 additions & 0 deletions tests/Fake/Provide/Error/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BEAR\Sunday\Provide\Error;

function header(
$string,
$replace = true,
$http_response_code = null
) {
FakeVndError::$headers = func_get_args();
}
8 changes: 8 additions & 0 deletions tests/Fake/Provide/Error/http_response_code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BEAR\Sunday\Provide\Error;


function http_response_code($int) {
FakeVndError::$code = func_get_args();
}
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/Fake/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require __DIR__ . '/Provide/Transfer/header.php';
require __DIR__ . '/Provide/Error/header.php';
require __DIR__ . '/Provide/Error/http_response_code.php';
32 changes: 20 additions & 12 deletions tests/Provide/Error/VndErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,48 @@

class VndErrorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FakeVndError
*/
private $vndError;

public function setUp()
{
FakeVndError::reset();
$this->vndError = new FakeVndError;
ini_set('error_log', '/dev/null');
}

public function testNotFound()
{
$error = new VndError;
$e = new ResourceNotFoundException('', 404);
$errorPage = $error->handle($e, new RouterMatch);
$this->assertSame(404, $errorPage->code);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([404], FakeVndError::$code);
$this->assertSame(['Content-Type: application/vnd.error+json'], FakeVndError::$headers);
$this->assertSame('{"message":"Not Found"}', FakeVndError::$content);
}

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

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

public function testServerErrorNot50X()
{
$error = new VndError;
$e = new \RuntimeException('message', 0);
$errorPage = $error->handle($e, new RouterMatch);
$this->assertSame(500, $errorPage->code);
$this->vndError->handle($e, new RouterMatch)->transfer();
$this->assertSame([500], FakeVndError::$code);
$this->assertSame('{"message":"500 Server Error"}', FakeVndError::$content);
}
}
6 changes: 6 additions & 0 deletions tests/Provide/Router/WebRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public function testMatchWithQuery()
$this->assertSame('page://self/', $request->path);
$this->assertSame(['id' => 1], $request->query);
}

public function testGenerate()
{
$actual = $this->router->generate('', []);
$this->assertFalse($actual);
}
}

0 comments on commit 740c7d5

Please sign in to comment.