mirror of
https://github.com/pestphp/pest.git
synced 2026-03-05 23:37:22 +01:00
Handles tests where a static closure is provided
This commit is contained in:
@ -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",
|
||||
|
||||
16
src/Exceptions/TestClosureMustNotBeStatic.php
Normal file
16
src/Exceptions/TestClosureMustNotBeStatic.php
Normal 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
|
||||
{
|
||||
}
|
||||
@ -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.');
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -22,6 +22,7 @@ arch('dependencies')
|
||||
'Whoops',
|
||||
'Symfony\Component\Console',
|
||||
'Symfony\Component\Process',
|
||||
'Laravel\SerializableClosure\Support\ReflectionClosure',
|
||||
])->ignoring(['Composer', 'PHPUnit', 'SebastianBergmann']);
|
||||
|
||||
arch('contracts')
|
||||
|
||||
@ -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');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user