Handles tests where a static closure is provided

This commit is contained in:
Peter Fox
2024-03-17 16:48:43 +00:00
parent 6094682158
commit a4f8ae1a12
6 changed files with 44 additions and 5 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest\Factories;
use Laravel\SerializableClosure\Support\ReflectionClosure;
use ParseError;
use Pest\Concerns;
use Pest\Contracts\AddsAnnotations;
@ -11,6 +12,7 @@ use Pest\Contracts\HasPrintableTestCaseName;
use Pest\Exceptions\DatasetMissing;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Exceptions\TestAlreadyExist;
use Pest\Exceptions\TestClosureMustNotBeStatic;
use Pest\Exceptions\TestDescriptionMissing;
use Pest\Factories\Concerns\HigherOrderable;
use Pest\Support\Reflection;
@ -216,6 +218,13 @@ final class TestCaseFactory
throw new TestAlreadyExist($method->filename, $method->description);
}
if (
$method->closure instanceof \Closure &&
(new ReflectionClosure($method->closure))->isStatic()
) {
throw new TestClosureMustNotBeStatic("The test `$method->description` closure must not be static in $method->filename.");
}
if (! $method->receivesArguments()) {
if (! $method->closure instanceof \Closure) {
throw ShouldNotHappen::fromMessage('The test closure may not be empty.');