diff options
author | Ivan Enderlin <ivan.enderlin@hoa-project.net> | 2013-11-01 17:37:59 +0100 |
---|---|---|
committer | Ivan Enderlin <ivan.enderlin@hoa-project.net> | 2013-11-01 17:44:12 +0100 |
commit | ee1a8491c0972ede8ca58a91ff10503f9e7d2369 (patch) | |
tree | 2792a37fad36da50f54d95efb2baeeb8d9b25576 /Praspel.php | |
parent | 45d9e8856cacae6c8c926f31605ad87ce785dfd4 (diff) | |
download | Praspel-ee1a8491c0972ede8ca58a91ff10503f9e7d2369.zip Praspel-ee1a8491c0972ede8ca58a91ff10503f9e7d2369.tar.gz Praspel-ee1a8491c0972ede8ca58a91ff10503f9e7d2369.tar.bz2 |
Detect a new Failure\Precondition.
If there is not enough data to invoke the callable, the error was
postpone to the checkClause() of a @requires. But in the following
situation, this will not be detected:
/**
* @requires i: 'foo';
* @behavior a {
* @requires j: 'bar';
* }
* @behavior b {
* @requires j: 'baz';
* }
*/
public function f ( $i, $j ) { … }
If $i = 'foo' and $j unset. Because the first @behavior will failed
(silently, which is normal), and the second @behavior will also failed
(and also silently, which is still normal). So the callable will be
invoked with a missing argument.
This patch fixes that!
Diffstat (limited to 'Praspel.php')
-rw-r--r-- | Praspel.php | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/Praspel.php b/Praspel.php index 099961e..64043e8 100644 --- a/Praspel.php +++ b/Praspel.php @@ -207,7 +207,8 @@ class Praspel { 'No data were given. The System Under Test %s needs data ' . 'to be executed.', 1, $callable); - $arguments = array(); + $arguments = array(); + $numberOfRequiredArguments = 0; foreach($reflection->getParameters() as $parameter) { @@ -216,12 +217,20 @@ class Praspel { if(true === array_key_exists($name, $data)) { $arguments[$name] = &$data[$name]; + + if(false === $parameter->isOptional()) + ++$numberOfRequiredArguments; + continue; } - if(false === $parameter->isOptional()) - // Let the error be caught by the @requires clause. + if(false === $parameter->isOptional()) { + + ++$numberOfRequiredArguments; + + // Let the error be caught by a @requires clause. continue; + } $arguments[$name] = $parameter->getDefaultValue(); } @@ -256,6 +265,18 @@ class Praspel { if(0 < count($exceptions)) throw $exceptions; + $numberOfArguments = count($arguments); + + if($numberOfArguments < $numberOfRequiredArguments) { + + $exceptions[] = new Exception\Failure\Precondition( + 'Callable %s needs %d arguments; %d given.', + 2, array($callable, $numberOfRequiredArguments, $numberOfArguments) + ); + + throw $exceptions; + } + try { // Invoke. |