Skip to content

Commit

Permalink
Implemented debug mode. Throws exceptions and does not cache mappings…
Browse files Browse the repository at this point in the history
… if debug = true
  • Loading branch information
nilportugues committed Apr 4, 2016
1 parent 7aa4043 commit 41915d4
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CreateResource extends \NilPortugues\Api\JsonApi\Server\Actions\CreateReso
*/
public function getErrorResponse(Exception $e, ErrorBag $errorBag)
{
if (env('APP_DEBUG')) {
if (config('app.debug')) {
throw $e;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NilPortugues/Laravel5/JsonApi/Actions/GetResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GetResource extends \NilPortugues\Api\JsonApi\Server\Actions\GetResource
*/
public function getErrorResponse(Exception $e)
{
if (env('APP_DEBUG')) {
if (config('app.debug')) {
throw $e;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NilPortugues/Laravel5/JsonApi/Actions/ListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ListResource extends \NilPortugues\Api\JsonApi\Server\Actions\ListResource
*/
public function getErrorResponse(\Exception $e)
{
if (env('APP_DEBUG')) {
if (config('app.debug')) {
throw $e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PatchResource extends \NilPortugues\Api\JsonApi\Server\Actions\PatchResour
*/
public function getErrorResponse(\Exception $e)
{
if (env('APP_DEBUG')) {
if (config('app.debug')) {
throw $e;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NilPortugues/Laravel5/JsonApi/Actions/PutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PutResource extends \NilPortugues\Api\JsonApi\Server\Actions\PutResource
*/
public function getErrorResponse(\Exception $e)
{
if (env('APP_DEBUG')) {
if (config('app.debug')) {
throw $e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class Laravel51Provider
public function provider()
{
return function ($app) {
$parsedRoutes = Cache::rememberForever('jsonapi.mapping', function () use ($app) {
return $this->parseRoutes(new Mapper($app['config']->get('jsonapi')));
});
if (config('app.debug')) {
$parsedRoutes = $this->parseRoutes(new Mapper($app['config']->get('jsonapi')));
} else {
$parsedRoutes = Cache::rememberForever('jsonapi.mapping', function () use ($app) {
return $this->parseRoutes(new Mapper($app['config']->get('jsonapi')));
});
}

return new JsonApiSerializer(new JsonApiTransformer($parsedRoutes));
};
Expand Down
48 changes: 48 additions & 0 deletions tests/NilPortugues/App/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace NilPortugues\Tests\App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
];

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
*/
public function report(Exception $e)
{
parent::report($e);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
}
15 changes: 15 additions & 0 deletions tests/NilPortugues/Laravel5/JsonApi/JsonApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private function createNewEmployee()

public function testGetActionWhenEmployeeDoesNotExist()
{
//error need to be tested as production or exception will be thrown in debug mode.
$this->app['config']->set('app.debug', false);

$response = $this->call('GET', 'http://localhost/employees/1000');

$this->assertEquals(404, $response->getStatusCode());
Expand All @@ -121,6 +124,9 @@ public function testPostAction()

public function testPostActionCreateNonexistentTypeAndReturnErrors()
{
//error need to be tested as production or exception will be thrown in debug mode.
$this->app['config']->set('app.debug', false);

$content = <<<JSON
{
"data": {
Expand All @@ -137,6 +143,9 @@ public function testPostActionCreateNonexistentTypeAndReturnErrors()

public function testPostActionReturnsErrorBecauseAttributesAreMissing()
{
//error need to be tested as production or exception will be thrown in debug mode.
$this->app['config']->set('app.debug', false);

$content = <<<JSON
{
"data": {
Expand Down Expand Up @@ -166,6 +175,9 @@ public function testPostActionReturnsErrorBecauseAttributesAreMissing()

public function testPatchActionWhenEmployeeDoesNotExistReturns404()
{
//error need to be tested as production or exception will be thrown in debug mode.
$this->app['config']->set('app.debug', false);

$content = <<<JSON
{
"data": {
Expand All @@ -192,6 +204,9 @@ public function testPatchActionWhenEmployeeDoesNotExistReturns404()

public function testPutActionWhenEmployeeDoesNotExistReturns404()
{
//error need to be tested as production or exception will be thrown in debug mode.
$this->app['config']->set('app.debug', false);

$content = <<<JSON
{
"data": {
Expand Down
2 changes: 2 additions & 0 deletions tests/NilPortugues/Laravel5/JsonApi/LaravelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Routing\Router;
use NilPortugues\Tests\App\Transformers\EmployeesTransformer;
use NilPortugues\Tests\App\Transformers\OrdersTransformer;
use Psr\Log\NullLogger;

/**
* Class LaravelTestCase.
Expand Down Expand Up @@ -95,6 +96,7 @@ private function setUpHttpKernel($app)
{
$app->instance('request', (new \Illuminate\Http\Request())->instance());
$app->make('Illuminate\Foundation\Http\Kernel', [$app, $this->getRouter()])->bootstrap();
$app->bind('Illuminate\Contracts\Debug\ExceptionHandler', new \NilPortugues\Tests\App\Exceptions\Handler(new NullLogger()));
}

/**
Expand Down

0 comments on commit 41915d4

Please sign in to comment.