Skip to content

Commit

Permalink
Prevent PHP errors using more strict filters
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed Sep 10, 2016
1 parent 00bd689 commit f5f8171
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 26 deletions.
5 changes: 5 additions & 0 deletions src/FilterIn/BooleanFilterIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Arachne\EntityLoader\FilterIn;

use Arachne\EntityLoader\FilterInInterface;
use Nette\Application\BadRequestException;

/**
* @author Jáchym Toušek <[email protected]>
Expand All @@ -22,6 +23,10 @@ class BooleanFilterIn implements FilterInInterface
*/
public function filterIn($value)
{
if (!is_string($value)) {
throw new BadRequestException();
}

return (bool) $value;
}
}
5 changes: 5 additions & 0 deletions src/FilterIn/FloatFilterIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Arachne\EntityLoader\FilterIn;

use Arachne\EntityLoader\FilterInInterface;
use Nette\Application\BadRequestException;

/**
* @author Jáchym Toušek <[email protected]>
Expand All @@ -22,6 +23,10 @@ class FloatFilterIn implements FilterInInterface
*/
public function filterIn($value)
{
if (!is_string($value)) {
throw new BadRequestException();
}

return (float) $value;
}
}
5 changes: 5 additions & 0 deletions src/FilterIn/IntegerFilterIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Arachne\EntityLoader\FilterIn;

use Arachne\EntityLoader\FilterInInterface;
use Nette\Application\BadRequestException;

/**
* @author Jáchym Toušek <[email protected]>
Expand All @@ -22,6 +23,10 @@ class IntegerFilterIn implements FilterInInterface
*/
public function filterIn($value)
{
if (!is_string($value)) {
throw new BadRequestException();
}

return (int) $value;
}
}
7 changes: 6 additions & 1 deletion src/FilterIn/StringFilterIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Arachne\EntityLoader\FilterIn;

use Arachne\EntityLoader\FilterInInterface;
use Nette\Application\BadRequestException;

/**
* @author Jáchym Toušek <[email protected]>
Expand All @@ -22,6 +23,10 @@ class StringFilterIn implements FilterInInterface
*/
public function filterIn($value)
{
return (string) $value;
if (!is_string($value)) {
throw new BadRequestException();
}

return $value;
}
}
2 changes: 1 addition & 1 deletion tests/functional/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extensions:
oops.cachefactory: Oops\CacheFactory\DI\CacheFactoryExtension

nette.application:
catchExceptions: null
catchExceptions: false
mapping:
*: Tests\Functional\Fixtures\*Presenter

Expand Down
23 changes: 22 additions & 1 deletion tests/functional/src/Fixtures/ArticlePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,35 @@ public function actionUntyped($parameter)
{
}

public function actionEntity(Article $parameter)
{
}

/**
* @param int $parameter
*/
public function actionInt($parameter = 1)
{
}

public function actionEntity(Article $parameter)
/**
* @param bool $parameter
*/
public function actionBool($parameter)
{
}

/**
* @param float $parameter
*/
public function actionFloat($parameter)
{
}

/**
* @param string $parameter
*/
public function actionString($parameter)
{
}
}
9 changes: 6 additions & 3 deletions tests/functional/src/Fixtures/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public function __construct(RequestEntityUnloader $unloader)
public function create()
{
$router = new RouteList();
$router[] = new Route('/<action>[/<parameter>]', [
'presenter' => 'Article',
]);
$router[] = new Route(
'/<action>',
[
'presenter' => 'Article',
]
);

return new RouterWrapper($router, $this->unloader);
}
Expand Down
130 changes: 111 additions & 19 deletions tests/functional/src/RequestEntityLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Codeception\TestCase\Test;
use Nette\Application\Application;
use Nette\Application\UI\Presenter;
use Tests\Functional\Fixtures\Article;

/**
Expand All @@ -17,36 +18,127 @@ class RequestEntityLoaderTest extends Test
*/
public function testUntyped()
{
$this->tester->amOnPage('/untyped/5');
$this->tester->amOnPage('/untyped?parameter=5');
}

public function testEntity()
{
$this->tester->amOnPage('/entity?parameter=5');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertEquals(
[
'action' => 'entity',
'parameter' => new Article(5),
],
$request->getParameters()
);
}

public function testInt()
{
$this->tester->amOnPage('/int/5');
$request = $this->tester->grabService(Application::class)->getPresenter()->getRequest();
$this->assertSame([
'action' => 'int',
'parameter' => 5,
], $request->getParameters());
$this->tester->amOnPage('/int?parameter=5');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertSame(
[
'action' => 'int',
'parameter' => 5,
],
$request->getParameters()
);
}

public function testIntWithDefault()
{
$this->tester->amOnPage('/int');
$request = $this->tester->grabService(Application::class)->getPresenter()->getRequest();
$this->assertSame([
'action' => 'int',
'parameter' => null,
], $request->getParameters());
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertSame(
[
'action' => 'int',
],
$request->getParameters()
);
}

public function testEntity()
/**
* @expectedException Nette\Application\BadRequestException
*/
public function testIntError()
{
$this->tester->amOnPage('/int?parameter[]=0');
}

public function testBool()
{
$this->tester->amOnPage('/bool?parameter=1');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertSame(
[
'action' => 'bool',
'parameter' => true,
],
$request->getParameters()
);
}

/**
* @expectedException Nette\Application\BadRequestException
*/
public function testBoolError()
{
$this->tester->amOnPage('/bool?parameter[]=0');
}

public function testFloat()
{
$this->tester->amOnPage('/float?parameter=1');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertSame(
[
'action' => 'float',
'parameter' => 1.0,
],
$request->getParameters()
);
}

/**
* @expectedException Nette\Application\BadRequestException
*/
public function testFloatError()
{
$this->tester->amOnPage('/float?parameter[]=0');
}

public function testString()
{
$this->tester->amOnPage('/string?parameter=1');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
self::assertInstanceOf(Presenter::class, $presenter);
$request = $presenter->getRequest();
self::assertSame(
[
'action' => 'string',
'parameter' => '1',
],
$request->getParameters()
);
}

/**
* @expectedException Nette\Application\BadRequestException
*/
public function testStringError()
{
$this->tester->amOnPage('/entity/5');
$request = $this->tester->grabService(Application::class)->getPresenter()->getRequest();
$this->assertEquals([
'action' => 'entity',
'parameter' => new Article(5),
], $request->getParameters());
$this->tester->amOnPage('/string?parameter[]=0');
}
}
2 changes: 1 addition & 1 deletion tests/functional/src/RequestEntityUnloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function testLink()
{
$this->tester->amOnPage('/default');
$presenter = $this->tester->grabService(Application::class)->getPresenter();
$this->assertSame('/entity/5', $presenter->link('Article:entity', new Article(5)));
$this->assertSame('/entity?parameter=5', $presenter->link('Article:entity', new Article(5)));
}
}

0 comments on commit f5f8171

Please sign in to comment.