fix: usage of named arguments

This commit is contained in:
Nuno Maduro
2024-02-01 13:45:06 +00:00
parent 3dffdf7cb8
commit 7b9bae0415
10 changed files with 213 additions and 70 deletions

View File

@ -18,13 +18,13 @@ final class BootOverrides implements Bootstrapper
* @var array<string, string>
*/
public const FILES = [
'c7b9c8a96006dea314204a8f09a8764e51ce0b9b79aadd58da52e8c328db4870' => 'Runner/Filter/NameFilterIterator.php',
'ec723a9efae521dd6576d2e7d746cc12d3e27f271c49c46420fba8a0e161a61f' => 'Runner/Filter/NameFilterIterator.php',
'52b2574e96269aca1bb2d41bbf418c3bcf23dd21d14c66f90789025c309e39df' => 'Runner/ResultCache/DefaultResultCache.php',
'bc8718c89264f65800beabc23e51c6d3bcff87dfc764a12179ef5dbfde272c8b' => 'Runner/TestSuiteLoader.php',
'f41e48d6cb546772a7de4f8e66b6b7ce894a5318d063eb52e354d206e96c701c' => 'TextUI/Command/Commands/WarmCodeCoverageCacheCommand.php',
'cb7519f2d82893640b694492cf7ec9528da80773cc1d259634181b5d393528b5' => 'TextUI/Output/Default/ProgressPrinter/Subscriber/TestSkippedSubscriber.php',
'6db25ee539e9b12b1fb4e044a0a93410e015bc983ecdd3909cd394fe44ae8c95' => 'TextUI/TestSuiteFilterProcessor.php',
'ef64a657ed9c0067791483784944107827bf227c7e3200f212b6751876b99e25' => 'Event/Value/ThrowableBuilder.php',
'2ef8e21dbb27cf6597dd9bb0f941c063dcc98b5af2c35d10b1c2d77721582e8f' => 'TextUI/Command/Commands/WarmCodeCoverageCacheCommand.php',
'badc88c79c2a47d768be3925051999b158d08b64e57ccf4ce560f1610cbcc1e8' => 'TextUI/Output/Default/ProgressPrinter/Subscriber/TestSkippedSubscriber.php',
'8607a62825a762735721c39e5d7d59f6efee1409d85cd3b1a976d0d83a308be0' => 'TextUI/TestSuiteFilterProcessor.php',
'a01a02eadd18146f12731c7adb8cd56cf76f3f6bda2bae06ff4fd6573789b0f4' => 'Event/Value/ThrowableBuilder.php',
'c78f96e34b98ed01dd8106539d59b8aa8d67f733274118b827c01c5c4111c033' => 'Logging/JUnit/JunitXmlLogger.php',
];

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Pest\Concerns;
use Closure;
use Pest\Exceptions\DatasetArgsCountMismatch;
use Pest\Exceptions\DatasetArgumentsMismatch;
use Pest\Support\ChainableClosure;
use Pest\Support\ExceptionTrace;
use Pest\Support\Reflection;
@ -14,6 +14,7 @@ use PHPUnit\Framework\Attributes\PostCondition;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use Throwable;
/**
@ -187,7 +188,7 @@ trait Testable
/**
* Gets executed before the Test Case.
*/
protected function setUp(): void
protected function setUp(...$arguments): void
{
TestSuite::getInstance()->test = $this;
@ -221,13 +222,13 @@ trait Testable
$beforeEach = ChainableClosure::bound($this->__beforeEach, $beforeEach);
}
$this->__callClosure($beforeEach, func_get_args());
$this->__callClosure($beforeEach, $arguments);
}
/**
* Gets executed after the Test Case.
*/
protected function tearDown(): void
protected function tearDown(...$arguments): void
{
$afterEach = TestSuite::getInstance()->afterEach->get(self::$__filename);
@ -235,7 +236,7 @@ trait Testable
$afterEach = ChainableClosure::bound($this->__afterEach, $afterEach);
}
$this->__callClosure($afterEach, func_get_args());
$this->__callClosure($afterEach, $arguments);
parent::tearDown();
@ -250,7 +251,7 @@ trait Testable
private function __runTest(Closure $closure, ...$args): mixed
{
$arguments = $this->__resolveTestArguments($args);
$this->__ensureDatasetArgumentNumberMatches($arguments);
$this->__ensureDatasetArgumentNameAndNumberMatches($arguments);
return $this->__callClosure($closure, $arguments);
}
@ -315,9 +316,9 @@ trait Testable
* Ensures dataset items count matches underlying test case required parameters
*
* @throws ReflectionException
* @throws DatasetArgsCountMismatch
* @throws DatasetArgumentsMismatch
*/
private function __ensureDatasetArgumentNumberMatches(array $arguments): void
private function __ensureDatasetArgumentNameAndNumberMatches(array $arguments): void
{
if ($arguments === []) {
return;
@ -328,11 +329,19 @@ trait Testable
$requiredParametersCount = $testReflection->getNumberOfRequiredParameters();
$suppliedParametersCount = count($arguments);
if ($suppliedParametersCount >= $requiredParametersCount) {
$datasetParameterNames = array_keys($arguments);
$testParameterNames = array_map(
fn (ReflectionParameter $reflectionParameter): string => $reflectionParameter->getName(),
array_filter($testReflection->getParameters(), fn (ReflectionParameter $reflectionParameter): bool => ! $reflectionParameter->isOptional()),
);
if (
(count(array_diff($testParameterNames, $datasetParameterNames)) === 0) || isset($testParameterNames[0])
&& $suppliedParametersCount >= $requiredParametersCount) {
return;
}
throw new DatasetArgsCountMismatch($requiredParametersCount, $suppliedParametersCount);
throw new DatasetArgumentsMismatch($requiredParametersCount, $suppliedParametersCount);
}
/**

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use Exception;
final class DatasetArgsCountMismatch extends Exception
{
public function __construct(int $requiredCount, int $suppliedCount)
{
parent::__construct(sprintf('Test expects %d arguments but dataset only provides %d', $requiredCount, $suppliedCount));
}
}

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use Exception;
final class DatasetArgumentsMismatch extends Exception
{
public function __construct(int $requiredCount, int $suppliedCount)
{
if ($requiredCount <= $suppliedCount) {
parent::__construct(sprintf('Test argument names and dataset keys do not match'));
} else {
parent::__construct(sprintf('Test expects %d arguments but dataset only provides %d', $requiredCount, $suppliedCount));
}
}
//
}

View File

@ -100,7 +100,7 @@ final class TestCaseMethodFactory
$method = $this;
return function () use ($testCase, $method, $closure): mixed { // @phpstan-ignore-line
return function (...$arguments) use ($testCase, $method, $closure): mixed { // @phpstan-ignore-line
/* @var TestCase $this */
$testCase->proxies->proxy($this);
$method->proxies->proxy($this);
@ -108,7 +108,7 @@ final class TestCaseMethodFactory
$testCase->chains->chain($this);
$method->chains->chain($this);
return \Pest\Support\Closure::bind($closure, $this, self::class)(...func_get_args());
return \Pest\Support\Closure::bind($closure, $this, self::class)(...$arguments);
};
}
@ -168,13 +168,13 @@ final class TestCaseMethodFactory
return <<<PHP
$attributesCode
public function $methodName()
public function $methodName(...\$arguments)
{
\$test = \Pest\TestSuite::getInstance()->tests->get(self::\$__filename)->getMethod(\$this->name())->getClosure(\$this);
return \$this->__runTest(
\$test,
...func_get_args(),
...\$arguments,
);
}
$datasetsCode

View File

@ -17,13 +17,13 @@ final class ChainableClosure
*/
public static function boundWhen(Closure $condition, Closure $next): Closure
{
return function () use ($condition, $next): void {
return function (...$arguments) use ($condition, $next): void {
if (! is_object($this)) { // @phpstan-ignore-line
throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.');
}
if (\Pest\Support\Closure::bind($condition, $this, self::class)(...func_get_args())) {
\Pest\Support\Closure::bind($next, $this, self::class)(...func_get_args());
if (\Pest\Support\Closure::bind($condition, $this, self::class)(...$arguments)) {
\Pest\Support\Closure::bind($next, $this, self::class)(...$arguments);
}
};
}
@ -33,13 +33,13 @@ final class ChainableClosure
*/
public static function bound(Closure $closure, Closure $next): Closure
{
return function () use ($closure, $next): void {
return function (...$arguments) use ($closure, $next): void {
if (! is_object($this)) { // @phpstan-ignore-line
throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.');
}
\Pest\Support\Closure::bind($closure, $this, self::class)(...func_get_args());
\Pest\Support\Closure::bind($next, $this, self::class)(...func_get_args());
\Pest\Support\Closure::bind($closure, $this, self::class)(...$arguments);
\Pest\Support\Closure::bind($next, $this, self::class)(...$arguments);
};
}
@ -48,9 +48,9 @@ final class ChainableClosure
*/
public static function unbound(Closure $closure, Closure $next): Closure
{
return function () use ($closure, $next): void {
$closure(...func_get_args());
$next(...func_get_args());
return function (...$arguments) use ($closure, $next): void {
$closure(...$arguments);
$next(...$arguments);
};
}
@ -59,9 +59,9 @@ final class ChainableClosure
*/
public static function boundStatically(Closure $closure, Closure $next): Closure
{
return static function () use ($closure, $next): void {
\Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args());
\Pest\Support\Closure::bind($next, null, self::class)(...func_get_args());
return static function (...$arguments) use ($closure, $next): void {
\Pest\Support\Closure::bind($closure, null, self::class)(...$arguments);
\Pest\Support\Closure::bind($next, null, self::class)(...$arguments);
};
}
}