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 \ReflectionFunction($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/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index f9d02d9c..73fde260 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');