Skip to content

Commit

Permalink
ComponentReflection::combineArgs() throws Nette\InvalidArgumentExcept…
Browse files Browse the repository at this point in the history
…ion and is converted to BadRequestException on higher level

Also reverts "ComponentReflection::combineArgs() throws InvalidArgumentException instead BadRequestException when incompatible type is object" 212ec43.
  • Loading branch information
dg committed Feb 2, 2017
1 parent f6a15f3 commit 88ef0bd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 61 deletions.
6 changes: 5 additions & 1 deletion src/Application/MicroPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public function run(Application\Request $request): Application\IResponse
}
}
$params['presenter'] = $this;
$params = Application\UI\ComponentReflection::combineArgs($reflection, $params);
try {
$params = Application\UI\ComponentReflection::combineArgs($reflection, $params);
} catch (Nette\InvalidArgumentException $e) {
$this->error($e->getMessage());
}

$response = $callback(...array_values($params));

Expand Down
7 changes: 6 additions & 1 deletion src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ protected function tryCall(string $method, array $params): bool
$rm = $rc->getMethod($method);
if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
$this->checkRequirements($rm);
$rm->invokeArgs($this, $rc->combineArgs($rm, $params));
try {
$args = $rc->combineArgs($rm, $params);
} catch (Nette\InvalidArgumentException $e) {
throw new Nette\Application\BadRequestException($e->getMessage());
}
$rm->invokeArgs($this, $args);
return TRUE;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Nette\Application\UI;

use Nette;
use Nette\Application\BadRequestException;


/**
Expand Down Expand Up @@ -116,7 +115,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args):
if (isset($args[$name])) {
$res[$i] = $args[$name];
if (!self::convertType($res[$i], $type, $isClass)) {
throw new BadRequestException(sprintf(
throw new Nette\InvalidArgumentException(sprintf(
'Argument $%s passed to %s() must be %s, %s given.',
$name,
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(),
Expand All @@ -131,7 +130,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args):
} elseif ($type === 'array') {
$res[$i] = [];
} else {
throw new BadRequestException(sprintf(
throw new Nette\InvalidArgumentException(sprintf(
'Missing parameter $%s required by %s()',
$name,
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName()
Expand Down
55 changes: 27 additions & 28 deletions tests/UI/ComponentReflection.combineArgs.php7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
declare(strict_types=1);

use Nette\Application\UI\ComponentReflection as Reflection;
use Nette\Application\BadRequestException;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
Expand Down Expand Up @@ -54,7 +53,7 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => []]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::params() must be scalar, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::params() must be scalar, array given.');
});


Expand All @@ -66,31 +65,31 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, []);
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
}, Nette\InvalidArgumentException::class, 'Missing parameter $int required by MyPresenter::hints()');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '']);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => NULL]);
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
}, Nette\InvalidArgumentException::class, 'Missing parameter $int required by MyPresenter::hints()');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => new stdClass]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => []]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, array given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hints() must be bool, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hints() must be bool, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hints() must be array, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hints() must be array, string given.');
});


Expand All @@ -104,23 +103,23 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '']);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => new stdClass]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, stdClass given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, stdClass given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => []]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, array given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsNulls() must be bool, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hintsNulls() must be bool, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsNulls() must be array, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hintsNulls() must be array, string given.');
});


Expand All @@ -134,23 +133,23 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '']);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => new stdClass]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, stdClass given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, stdClass given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => []]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, array given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsDefaults() must be bool, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hintsDefaults() must be bool, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsDefaults() must be array, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hintsDefaults() must be array, string given.');
});


Expand All @@ -164,23 +163,23 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '']);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => new stdClass]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, stdClass given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, stdClass given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => []]);
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, array given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::defaults() must be boolean, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::defaults() must be boolean, string given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::defaults() must be array, string given.');
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::defaults() must be array, string given.');
});


Expand All @@ -191,17 +190,17 @@ test(function () {

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, []);
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
}, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['req' => NULL, 'opt' => NULL]);
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
}, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['req' => $method, 'opt' => NULL]);
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, ReflectionMethod given.');
}, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, ReflectionMethod given.');

Assert::exception(function () use ($method) {
Reflection::combineArgs($method, ['req' => [], 'opt' => NULL]);
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, array given.');
}, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, array given.');
});
Loading

0 comments on commit 88ef0bd

Please sign in to comment.