mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Merge pull request #1117 from peterfox/bug/catch-static-closures
[Bug] provided explaination for static closures
This commit is contained in:
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
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ use Pest\Contracts\HasPrintableTestCaseName;
|
|||||||
use Pest\Exceptions\DatasetMissing;
|
use Pest\Exceptions\DatasetMissing;
|
||||||
use Pest\Exceptions\ShouldNotHappen;
|
use Pest\Exceptions\ShouldNotHappen;
|
||||||
use Pest\Exceptions\TestAlreadyExist;
|
use Pest\Exceptions\TestAlreadyExist;
|
||||||
|
use Pest\Exceptions\TestClosureMustNotBeStatic;
|
||||||
use Pest\Exceptions\TestDescriptionMissing;
|
use Pest\Exceptions\TestDescriptionMissing;
|
||||||
use Pest\Factories\Concerns\HigherOrderable;
|
use Pest\Factories\Concerns\HigherOrderable;
|
||||||
use Pest\Support\Reflection;
|
use Pest\Support\Reflection;
|
||||||
@ -216,6 +217,13 @@ final class TestCaseFactory
|
|||||||
throw new TestAlreadyExist($method->filename, $method->description);
|
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->receivesArguments()) {
|
||||||
if (! $method->closure instanceof \Closure) {
|
if (! $method->closure instanceof \Closure) {
|
||||||
throw ShouldNotHappen::fromMessage('The test closure may not be empty.');
|
throw ShouldNotHappen::fromMessage('The test closure may not be empty.');
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use Pest\Exceptions\DatasetMissing;
|
use Pest\Exceptions\DatasetMissing;
|
||||||
use Pest\Exceptions\TestAlreadyExist;
|
use Pest\Exceptions\TestAlreadyExist;
|
||||||
|
use Pest\Exceptions\TestClosureMustNotBeStatic;
|
||||||
use Pest\Factories\TestCaseMethodFactory;
|
use Pest\Factories\TestCaseMethodFactory;
|
||||||
use Pest\TestSuite;
|
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'),
|
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 () {
|
it('alerts users about tests with arguments but no input', function () {
|
||||||
$testSuite = new TestSuite(getcwd(), 'tests');
|
$testSuite = new TestSuite(getcwd(), 'tests');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user