From 9de85175db24b495ee60590cdf4c98142af9886a Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Sat, 18 Mar 2023 23:58:09 +0100 Subject: [PATCH 1/4] better dataset arguments mismatch message --- src/Concerns/Testable.php | 31 ++++++++++++++++++++- src/Exceptions/DatasetArgsCountMismatch.php | 15 ++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/Exceptions/DatasetArgsCountMismatch.php diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index e6e44488..d43a31fd 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -5,11 +5,14 @@ declare(strict_types=1); namespace Pest\Concerns; use Closure; +use Pest\Exceptions\DatasetArgsCountMismatch; use Pest\Support\ChainableClosure; use Pest\Support\ExceptionTrace; use Pest\Support\Reflection; use Pest\TestSuite; use PHPUnit\Framework\TestCase; +use ReflectionException; +use ReflectionFunction; use Throwable; /** @@ -212,7 +215,10 @@ trait Testable */ private function __runTest(Closure $closure, ...$args): mixed { - return $this->__callClosure($closure, $this->__resolveTestArguments($args)); + $arguments = $this->__resolveTestArguments($args); + $this->__ensureDatasetArgumentNumberMatches($arguments); + + return $this->__callClosure($closure, $arguments); } /** @@ -264,6 +270,29 @@ trait Testable return array_values($boundDatasetResult); } + /** + * Ensures dataset items count matches underlying test case required parameters + * + * @throws ReflectionException + * @throws DatasetArgsCountMismatch + */ + private function __ensureDatasetArgumentNumberMatches(array $arguments): void + { + if ($arguments === []) { + return; + } + + $underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure'); + $testReflection = new ReflectionFunction($underlyingTest); + $requiredParametersCount = $testReflection->getNumberOfRequiredParameters(); + + if (count($arguments) >= $requiredParametersCount) { + return; + } + + throw new DatasetArgsCountMismatch($this->dataName()); + } + /** * @throws Throwable */ diff --git a/src/Exceptions/DatasetArgsCountMismatch.php b/src/Exceptions/DatasetArgsCountMismatch.php new file mode 100644 index 00000000..3b34839c --- /dev/null +++ b/src/Exceptions/DatasetArgsCountMismatch.php @@ -0,0 +1,15 @@ + Date: Sun, 19 Mar 2023 00:14:18 +0100 Subject: [PATCH 2/4] fix failure message --- src/Concerns/Testable.php | 5 +++-- src/Exceptions/DatasetArgsCountMismatch.php | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index d43a31fd..1b164b04 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -285,12 +285,13 @@ trait Testable $underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure'); $testReflection = new ReflectionFunction($underlyingTest); $requiredParametersCount = $testReflection->getNumberOfRequiredParameters(); + $suppliedParametersCount = count($arguments); - if (count($arguments) >= $requiredParametersCount) { + if ($suppliedParametersCount >= $requiredParametersCount) { return; } - throw new DatasetArgsCountMismatch($this->dataName()); + throw new DatasetArgsCountMismatch($this->dataName(), $requiredParametersCount, $suppliedParametersCount); } /** diff --git a/src/Exceptions/DatasetArgsCountMismatch.php b/src/Exceptions/DatasetArgsCountMismatch.php index 3b34839c..86fcd3f1 100644 --- a/src/Exceptions/DatasetArgsCountMismatch.php +++ b/src/Exceptions/DatasetArgsCountMismatch.php @@ -8,8 +8,8 @@ use Exception; final class DatasetArgsCountMismatch extends Exception { - public function __construct(string $dataName) + public function __construct(string $dataName, int $requiredCount, int $suppliedCount) { - parent::__construct(sprintf('Number of arguments mismatch between test and dataset [%s]', $dataName)); + parent::__construct(sprintf('Test expects %d arguments but dataset [%s] only provides %d', $requiredCount, $dataName, $suppliedCount)); } } From 7d89d3546e3a33f3be465d50bfdcf31617312a18 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Sun, 19 Mar 2023 00:22:31 +0100 Subject: [PATCH 3/4] update snapshots --- tests/.snapshots/Failure.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/.snapshots/Failure.php.inc b/tests/.snapshots/Failure.php.inc index 419c3b5d..16db9d25 100644 --- a/tests/.snapshots/Failure.php.inc +++ b/tests/.snapshots/Failure.php.inc @@ -9,7 +9,7 @@ ##teamcity[testIgnored name='it can be ignored because it is skipped' message='This test was ignored.' details='' flowId='1234'] ##teamcity[testFinished name='it can be ignored because it is skipped' duration='100000' flowId='1234'] ##teamcity[testStarted name='it can fail' locationHint='pest_qn://tests/.tests/Failure.php::it can fail' flowId='1234'] -##teamcity[testFailed name='it can fail' message='oh noo' details='at tests/.tests/Failure.php:18|nat src/Factories/TestCaseMethodFactory.php:100|nat src/Concerns/Testable.php:272|nat src/Support/ExceptionTrace.php:28|nat src/Concerns/Testable.php:272|nat src/Concerns/Testable.php:215|nat src/Kernel.php:84' flowId='1234'] +##teamcity[testFailed name='it can fail' message='oh noo' details='at tests/.tests/Failure.php:18|nat src/Factories/TestCaseMethodFactory.php:100|nat src/Concerns/Testable.php:302|nat src/Support/ExceptionTrace.php:28|nat src/Concerns/Testable.php:302|nat src/Concerns/Testable.php:221|nat src/Kernel.php:84' flowId='1234'] ##teamcity[testFinished name='it can fail' duration='100000' flowId='1234'] ##teamcity[testStarted name='it is not done yet' locationHint='pest_qn://tests/.tests/Failure.php::it is not done yet' flowId='1234'] ##teamcity[testIgnored name='it is not done yet' message='This test was ignored.' details='' flowId='1234'] From da4bf7f5c3caefa10e720d23cbc6ad99963b85a5 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 20 Mar 2023 16:49:15 +0100 Subject: [PATCH 4/4] update snapshots --- tests/.snapshots/Failure.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/.snapshots/Failure.php.inc b/tests/.snapshots/Failure.php.inc index 16db9d25..c31b63a1 100644 --- a/tests/.snapshots/Failure.php.inc +++ b/tests/.snapshots/Failure.php.inc @@ -1,6 +1,6 @@ ##teamcity[testSuiteStarted name='Tests/tests/Failure' locationHint='file://tests/.tests/Failure.php' flowId='1234'] ##teamcity[testStarted name='it can fail with comparison' locationHint='pest_qn://tests/.tests/Failure.php::it can fail with comparison' flowId='1234'] -##teamcity[testFailed name='it can fail with comparison' message='Failed asserting that true matches expected false.' details='at src/Mixins/Expectation.php:342|nat src/Support/ExpectationPipeline.php:75|nat src/Support/ExpectationPipeline.php:79|nat src/Expectation.php:300|nat tests/.tests/Failure.php:6|nat src/Factories/TestCaseMethodFactory.php:100|nat src/Concerns/Testable.php:272|nat src/Support/ExceptionTrace.php:28|nat src/Concerns/Testable.php:272|nat src/Concerns/Testable.php:215|nat src/Kernel.php:84' type='comparisonFailure' actual='true' expected='false' flowId='1234'] +##teamcity[testFailed name='it can fail with comparison' message='Failed asserting that true matches expected false.' details='at src/Mixins/Expectation.php:342|nat src/Support/ExpectationPipeline.php:75|nat src/Support/ExpectationPipeline.php:79|nat src/Expectation.php:300|nat tests/.tests/Failure.php:6|nat src/Factories/TestCaseMethodFactory.php:100|nat src/Concerns/Testable.php:302|nat src/Support/ExceptionTrace.php:28|nat src/Concerns/Testable.php:302|nat src/Concerns/Testable.php:221|nat src/Kernel.php:84' type='comparisonFailure' actual='true' expected='false' flowId='1234'] ##teamcity[testFinished name='it can fail with comparison' duration='100000' flowId='1234'] ##teamcity[testStarted name='it can be ignored because of no assertions' locationHint='pest_qn://tests/.tests/Failure.php::it can be ignored because of no assertions' flowId='1234'] ##teamcity[testIgnored name='it can be ignored because of no assertions' message='This test did not perform any assertions' details='' flowId='1234']