Merge pull request #1117 from peterfox/bug/catch-static-closures

[Bug] provided explaination for static closures
This commit is contained in:
Nuno Maduro
2024-08-22 20:33:58 +01:00
committed by GitHub
3 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use NunoMaduro\Collision\Contracts\RenderlessEditor;
use NunoMaduro\Collision\Contracts\RenderlessTrace;
use Symfony\Component\Console\Exception\ExceptionInterface;
/**
* @internal
*/
final class TestClosureMustNotBeStatic extends \InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
{
}

View File

@ -11,6 +11,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 +217,13 @@ final class TestCaseFactory
throw new TestAlreadyExist($method->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.');