mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(describe): refactor
This commit is contained in:
@ -23,42 +23,42 @@ use Throwable;
|
|||||||
trait Testable
|
trait Testable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Test method's test description.
|
* The test's description.
|
||||||
*/
|
*/
|
||||||
private string $__testDescription;
|
private string $__description;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method's describe description, if any.
|
* The test's latest description.
|
||||||
*/
|
*/
|
||||||
public ?string $__describeDescription = null;
|
private static string $__latestDescription;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test "latest" method description.
|
* The test's describing, if any.
|
||||||
*/
|
*/
|
||||||
private static string $__latestTestDescription;
|
public ?string $__describing = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case "test" closure.
|
* The test's test closure.
|
||||||
*/
|
*/
|
||||||
private Closure $__test;
|
private Closure $__test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case "setUp" closure.
|
* The test's before each closure.
|
||||||
*/
|
*/
|
||||||
private ?Closure $__beforeEach = null;
|
private ?Closure $__beforeEach = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case "tearDown" closure.
|
* The test's after each closure.
|
||||||
*/
|
*/
|
||||||
private ?Closure $__afterEach = null;
|
private ?Closure $__afterEach = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case "setUpBeforeClass" closure.
|
* The test's before all closure.
|
||||||
*/
|
*/
|
||||||
private static ?Closure $__beforeAll = null;
|
private static ?Closure $__beforeAll = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The test "tearDownAfterClass" closure.
|
* The test's after all closure.
|
||||||
*/
|
*/
|
||||||
private static ?Closure $__afterAll = null;
|
private static ?Closure $__afterAll = null;
|
||||||
|
|
||||||
@ -82,7 +82,8 @@ trait Testable
|
|||||||
|
|
||||||
if ($test->hasMethod($name)) {
|
if ($test->hasMethod($name)) {
|
||||||
$method = $test->getMethod($name);
|
$method = $test->getMethod($name);
|
||||||
$this->__testDescription = self::$__latestTestDescription = $method->description;
|
$this->__description = self::$__latestDescription = $method->description;
|
||||||
|
$this->__describing = $method->describing;
|
||||||
$this->__test = $method->getClosure($this);
|
$this->__test = $method->getClosure($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,7 +236,7 @@ trait Testable
|
|||||||
{
|
{
|
||||||
$method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name());
|
$method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name());
|
||||||
|
|
||||||
$this->__testDescription = self::$__latestTestDescription = $this->dataName() ? $method->description.' with '.$this->dataName() : $method->description;
|
$this->__description = self::$__latestDescription = $this->dataName() ? $method->description.' with '.$this->dataName() : $method->description;
|
||||||
|
|
||||||
$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
|
$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
|
||||||
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
|
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
|
||||||
@ -320,7 +321,7 @@ trait Testable
|
|||||||
*/
|
*/
|
||||||
public function getPrintableTestCaseMethodName(): string
|
public function getPrintableTestCaseMethodName(): string
|
||||||
{
|
{
|
||||||
return $this->__testDescription;
|
return $this->__description;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -328,6 +329,6 @@ trait Testable
|
|||||||
*/
|
*/
|
||||||
public static function getLatestPrintableTestCaseMethodName(): string
|
public static function getLatestPrintableTestCaseMethodName(): string
|
||||||
{
|
{
|
||||||
return self::$__latestTestDescription;
|
return self::$__latestDescription;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Pest\Exceptions;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use NunoMaduro\Collision\Contracts\RenderlessEditor;
|
|
||||||
use NunoMaduro\Collision\Contracts\RenderlessTrace;
|
|
||||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
final class AfterEachAlreadyExist extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Creates a new Exception instance.
|
|
||||||
*/
|
|
||||||
public function __construct(string $filename)
|
|
||||||
{
|
|
||||||
parent::__construct(sprintf('The afterEach already exists in the filename `%s`.', $filename));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Pest\Exceptions;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use NunoMaduro\Collision\Contracts\RenderlessEditor;
|
|
||||||
use NunoMaduro\Collision\Contracts\RenderlessTrace;
|
|
||||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
final class BeforeEachAlreadyExist extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Creates a new Exception instance.
|
|
||||||
*/
|
|
||||||
public function __construct(string $filename)
|
|
||||||
{
|
|
||||||
parent::__construct(sprintf('The beforeEach already exists in the filename `%s`.', $filename));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -22,45 +22,45 @@ final class TestCaseMethodFactory
|
|||||||
use HigherOrderable;
|
use HigherOrderable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* D=fghjkl
|
* The test's describing, if any.
|
||||||
*/
|
*/
|
||||||
public ?string $describing = null;
|
public ?string $describing = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if the Test Case Method is a "todo".
|
* Determines if the test is a "todo".
|
||||||
*/
|
*/
|
||||||
public bool $todo = false;
|
public bool $todo = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case Dataset, if any.
|
* The test's datasets.
|
||||||
*
|
*
|
||||||
* @var array<Closure|iterable<int|string, mixed>|string>
|
* @var array<Closure|iterable<int|string, mixed>|string>
|
||||||
*/
|
*/
|
||||||
public array $datasets = [];
|
public array $datasets = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case depends, if any.
|
* The test's dependencies.
|
||||||
*
|
*
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
public array $depends = [];
|
public array $depends = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Test Case groups, if any.
|
* The test's groups.
|
||||||
*
|
*
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
public array $groups = [];
|
public array $groups = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The covered classes and functions, if any.
|
* The covered classes and functions.
|
||||||
*
|
*
|
||||||
* @var array<int, \Pest\Factories\Covers\CoversClass|\Pest\Factories\Covers\CoversFunction|\Pest\Factories\Covers\CoversNothing>
|
* @var array<int, \Pest\Factories\Covers\CoversClass|\Pest\Factories\Covers\CoversFunction|\Pest\Factories\Covers\CoversNothing>
|
||||||
*/
|
*/
|
||||||
public array $covers = [];
|
public array $covers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Factory instance.
|
* Creates a new test case method factory instance.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $filename,
|
public string $filename,
|
||||||
@ -75,7 +75,7 @@ final class TestCaseMethodFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes the Test Case classes.
|
* Creates the test's closure.
|
||||||
*/
|
*/
|
||||||
public function getClosure(TestCase $concrete): Closure
|
public function getClosure(TestCase $concrete): Closure
|
||||||
{
|
{
|
||||||
@ -89,7 +89,6 @@ final class TestCaseMethodFactory
|
|||||||
|
|
||||||
$testCase = TestSuite::getInstance()->tests->get($this->filename);
|
$testCase = TestSuite::getInstance()->tests->get($this->filename);
|
||||||
|
|
||||||
$concrete->__describeDescription = $this->describing; // @phpstan-ignore-line
|
|
||||||
$testCase->factoryProxies->proxy($concrete);
|
$testCase->factoryProxies->proxy($concrete);
|
||||||
$this->factoryProxies->proxy($concrete);
|
$this->factoryProxies->proxy($concrete);
|
||||||
|
|
||||||
|
|||||||
@ -15,12 +15,4 @@ final class PendingCalls
|
|||||||
* The current describe call.
|
* The current describe call.
|
||||||
*/
|
*/
|
||||||
public static ?string $describing = null;
|
public static ?string $describing = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the current describe call.
|
|
||||||
*/
|
|
||||||
public static function describe(DescribeCall $describeCall): void
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,7 +54,7 @@ final class AfterEachCall
|
|||||||
$proxies = $this->proxies;
|
$proxies = $this->proxies;
|
||||||
|
|
||||||
$afterEachTestCase = ChainableClosure::when(
|
$afterEachTestCase = ChainableClosure::when(
|
||||||
fn () => is_null($describing) || $this->__describeDescription === $describing, // @phpstan-ignore-line
|
fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line
|
||||||
ChainableClosure::fromSameObject(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
ChainableClosure::fromSameObject(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
||||||
)->bindTo($this, self::class);
|
)->bindTo($this, self::class);
|
||||||
|
|
||||||
|
|||||||
@ -58,14 +58,18 @@ final class BeforeEachCall
|
|||||||
$describing = $this->describing;
|
$describing = $this->describing;
|
||||||
$testCaseProxies = $this->testCaseProxies;
|
$testCaseProxies = $this->testCaseProxies;
|
||||||
|
|
||||||
$beforeEachTestCall = function (TestCall $testCall) use ($describing): void {
|
$beforeEachTestCall = function (TestCall $testCall) use ($describing) : void {
|
||||||
if ($describing === $this->describing && $describing === $testCall->describing) {
|
if ($describing !== $this->describing) {
|
||||||
$this->testCallProxies->chain($testCall);
|
return;
|
||||||
}
|
}
|
||||||
|
if ($describing !== $testCall->describing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->testCallProxies->chain($testCall);
|
||||||
};
|
};
|
||||||
|
|
||||||
$beforeEachTestCase = ChainableClosure::when(
|
$beforeEachTestCase = ChainableClosure::when(
|
||||||
fn () => is_null($describing) || $this->__describeDescription === $describing, // @phpstan-ignore-line
|
fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line
|
||||||
ChainableClosure::fromSameObject(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
ChainableClosure::fromSameObject(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
||||||
)->bindTo($this, self::class);
|
)->bindTo($this, self::class);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user