mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
better dataset arguments mismatch message
This commit is contained in:
@ -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
|
||||
*/
|
||||
|
||||
15
src/Exceptions/DatasetArgsCountMismatch.php
Normal file
15
src/Exceptions/DatasetArgsCountMismatch.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class DatasetArgsCountMismatch extends Exception
|
||||
{
|
||||
public function __construct(string $dataName)
|
||||
{
|
||||
parent::__construct(sprintf('Number of arguments mismatch between test and dataset [%s]', $dataName));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user