Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Throw NotFoundHttpException when debug is disabled (#13)
Browse files Browse the repository at this point in the history
* Throw NotFoundHttpException when debug is disabled

Throw NotFoundHttpException when debug is disabled and pass string in
URL

for example `http://site.com/user/1844379023` work find but
`http://site.com/user/test` throw InvalidArgumentException

* Add test unit

* support old php version

* Use call instead of get

* Fix errors with legacy version of laravel
  • Loading branch information
salkhwlani authored and Propaganistas committed Apr 12, 2017
1 parent b3d16ee commit 13cecfd
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/FakeIdServiceProvider.php
Expand Up @@ -69,7 +69,11 @@ protected function registerRouterMacro()

// Decode FakeId first if applicable.
if (in_array('Propaganistas\LaravelFakeId\FakeIdTrait', class_uses_recursive($class))) {
$value = $this->container->make('fakeid')->decode($value);
try {
$value = $this->container->make('fakeid')->decode($value);
} catch (\InvalidArgumentException $e) {
throw config('app.debug') ? $e : new NotFoundHttpException;
}
}

if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Entities/Fake.php
@@ -0,0 +1,10 @@
<?php
namespace Propaganistas\LaravelFakeId\Tests\Entities;

use Illuminate\Database\Eloquent\Model;
use Propaganistas\LaravelFakeId\FakeIdTrait;

class Fake extends Model
{
use FakeIdTrait;
}
9 changes: 9 additions & 0 deletions tests/Entities/Real.php
@@ -0,0 +1,9 @@
<?php
namespace Propaganistas\LaravelFakeId\Tests\Entities;

use Illuminate\Database\Eloquent\Model;

class Real extends Model
{

}
115 changes: 92 additions & 23 deletions tests/FakeIdTest.php
@@ -1,37 +1,36 @@
<?php namespace Propaganistas\LaravelFakeId\Tests;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;
use Propaganistas\LaravelFakeId\Facades\FakeId;
use Propaganistas\LaravelFakeId\FakeIdTrait;
use Propaganistas\LaravelFakeId\Tests\Entities\Fake;
use Propaganistas\LaravelFakeId\Tests\Entities\Real;

class FakeIdTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
'Propaganistas\LaravelFakeId\FakeIdServiceProvider',
];
}

/**
*
*/
public function setUp()
{
parent::setUp();

$this->configureDatabase();

Route::model('real', 'Propaganistas\LaravelFakeId\Tests\Real');
Route::fakeIdModel('fake', 'Propaganistas\LaravelFakeId\Tests\Fake');
// add middleware for support version laravel >= 5.3
$middlewareBindings = version_compare($this->app->version(), '5.3.0') >= 0 ? 'Illuminate\Routing\Middleware\SubstituteBindings' : null;

Route::model('real', 'Propaganistas\LaravelFakeId\Tests\Entities\Real');
Route::fakeIdModel('fake', 'Propaganistas\LaravelFakeId\Tests\Entities\Fake');

Route::get('real/{real}', ['as' => 'real', function ($real) {
return 'real';
}]);
return $real->id;
}, 'middleware' => $middlewareBindings]);

Route::get('fake/{fake}', ['as' => 'fake', function ($fake) {
return 'fake';
}]);
return $fake->id;
}, 'middleware' => $middlewareBindings]);
}

protected function configureDatabase()
Expand Down Expand Up @@ -90,13 +89,83 @@ public function testFakeIdModelBindingWorks()

$this->assertEquals($expected, $actual);
}
}

class Real extends Model
{
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testResponseNotFoundWhenDecodeFailAndDebugOff()
{
$this->app['config']->set('app.debug', false);

class Fake extends Model
{
use FakeIdTrait;
}
$response = $this->call('get', route('fake', ['fake' => 'not-number']));

$this->throwErrorFromResponse($response);

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

/**
* @expectedException \InvalidArgumentException
*/
public function testResponseErrorWhenDecodeFailDebugOn()
{
$this->app['config']->set('app.debug', true);

$response = $this->call('get', route('fake', ['fake' => 'not-number']));

$this->app['config']->set('app.debug', false);

$this->throwErrorFromResponse($response);

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

public function testResponseFineWhenPassFakeModel()
{
$model = Fake::create([]);

$response = $this->call('get', route('fake', ['fake' => $model]));

$this->assertContains((string)$model->id, $response->getContent());
$this->assertEquals(200, $response->getStatusCode());
}

public function testResponseFineWhenPassNormalModel()
{
$model = Real::create([]);

$response = $this->call('get', route('real', ['real' => $model]));

$this->assertContains((string)$model->id, $response->getContent());

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

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testResponseFailWhenPassModelId()
{
$model = Fake::create([]);

$response = $this->call('get', route('fake', ['fake' => $model->id]));

$this->throwErrorFromResponse($response);

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

protected function throwErrorFromResponse($response)
{
if (isset($response->exception)) { // throw error manual if app use error handler of laravel to render error as html content
throw $response->exception;
}
}

protected function getPackageProviders($app)
{
return [
'Propaganistas\LaravelFakeId\FakeIdServiceProvider',
];
}
}

0 comments on commit 13cecfd

Please sign in to comment.