diff --git a/src/Exceptions/TestClosureMustNotBeStatic.php b/src/Exceptions/TestClosureMustNotBeStatic.php index 498b1913..b1f91e95 100644 --- a/src/Exceptions/TestClosureMustNotBeStatic.php +++ b/src/Exceptions/TestClosureMustNotBeStatic.php @@ -4,13 +4,28 @@ declare(strict_types=1); namespace Pest\Exceptions; +use InvalidArgumentException; use NunoMaduro\Collision\Contracts\RenderlessEditor; use NunoMaduro\Collision\Contracts\RenderlessTrace; +use Pest\Factories\TestCaseMethodFactory; use Symfony\Component\Console\Exception\ExceptionInterface; /** * @internal */ -final class TestClosureMustNotBeStatic extends \InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace +final class TestClosureMustNotBeStatic extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace { + /** + * Creates a new Exception instance. + */ + public function __construct(TestCaseMethodFactory $method) + { + parent::__construct( + sprintf( + 'Test closure must not be static. Please remove the `static` keyword from the `%s` method in `%s`.', + $method->description, + $method->filename + ) + ); + } } diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 9e166a1f..6d4330e8 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -221,7 +221,8 @@ final class TestCaseFactory $method->closure instanceof \Closure && (new \ReflectionFunction($method->closure))->isStatic() ) { - throw new TestClosureMustNotBeStatic("The test `$method->description` closure must not be static in $method->filename."); + + throw new TestClosureMustNotBeStatic($method); } if (! $method->receivesArguments()) { diff --git a/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index 73fde260..ee78587d 100644 --- a/tests/Unit/TestSuite.php +++ b/tests/Unit/TestSuite.php @@ -19,9 +19,7 @@ it('does not allow to add the same test description twice', function () { it('does not allow static closures', function () { $testSuite = new TestSuite(getcwd(), 'tests'); - $method = new TestCaseMethodFactory('foo', 'bar', static function () { - - }); + $method = new TestCaseMethodFactory('foo', 'bar', static function () {}); $testSuite->tests->set($method); })->throws(