diff --git a/composer.json b/composer.json index db105652..7fec7cf4 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "^8.1.0", "brianium/paratest": "^7.3.1", + "laravel/serializable-closure": "^1.0", "nunomaduro/collision": "^7.10.0|^8.1.1", "nunomaduro/termwind": "^1.15.1|^2.0.1", "pestphp/pest-plugin": "^2.1.1", diff --git a/src/Exceptions/TestClosureMustNotBeStatic.php b/src/Exceptions/TestClosureMustNotBeStatic.php new file mode 100644 index 00000000..498b1913 --- /dev/null +++ b/src/Exceptions/TestClosureMustNotBeStatic.php @@ -0,0 +1,16 @@ +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.'); diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index 4cbc8530..341ec902 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -90,11 +90,6 @@ final class TestCaseMethodFactory throw ShouldNotHappen::fromMessage('Description can not be empty.'); } - if (($reflection = new ReflectionClosure($this->closure))->isStatic()) { - $fileAndLine = $reflection->getFileName() . ':' . $reflection->getStartLine(); - throw ShouldNotHappen::fromMessage("The test `$this->description` closure must not be static in $fileAndLine."); - } - $closure = $this->closure; $testCase = TestSuite::getInstance()->tests->get($this->filename); diff --git a/tests/Arch.php b/tests/Arch.php index 7c508cf2..d9bcea4c 100644 --- a/tests/Arch.php +++ b/tests/Arch.php @@ -22,6 +22,7 @@ arch('dependencies') 'Whoops', 'Symfony\Component\Console', 'Symfony\Component\Process', + 'Laravel\SerializableClosure\Support\ReflectionClosure', ])->ignoring(['Composer', 'PHPUnit', 'SebastianBergmann']); arch('contracts') diff --git a/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index eb033d0c..3efd964c 100644 --- a/tests/Unit/TestSuite.php +++ b/tests/Unit/TestSuite.php @@ -2,6 +2,7 @@ use Pest\Exceptions\DatasetMissing; use Pest\Exceptions\TestAlreadyExist; +use Pest\Exceptions\TestClosureMustNotBeStatic; use Pest\Factories\TestCaseMethodFactory; use Pest\TestSuite; @@ -16,6 +17,22 @@ it('does not allow to add the same test description twice', function () { sprintf('A test with the description `%s` already exists in the filename `%s`.', 'bar', 'foo'), ); +it('does not allow static closures', function () { + $testSuite = new TestSuite(getcwd(), 'tests'); + $method = new TestCaseMethodFactory('foo', 'bar', static function () { + + }); + + $testSuite->tests->set($method); +})->throws( + TestClosureMustNotBeStatic::class, + sprintf( + 'The test `%s` closure must not be static in %s.', + 'bar', + 'foo', + ) +); + it('alerts users about tests with arguments but no input', function () { $testSuite = new TestSuite(getcwd(), 'tests');