feat(describe): refactor

This commit is contained in:
Nuno Maduro
2023-05-26 19:56:10 +01:00
parent 3e8616ec64
commit 68ea2c7d7e
7 changed files with 33 additions and 85 deletions

View File

@ -23,42 +23,42 @@ use Throwable;
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;
/**
* The Test Case "setUp" closure.
* The test's before each closure.
*/
private ?Closure $__beforeEach = null;
/**
* The Test Case "tearDown" closure.
* The test's after each closure.
*/
private ?Closure $__afterEach = null;
/**
* The Test Case "setUpBeforeClass" closure.
* The test's before all closure.
*/
private static ?Closure $__beforeAll = null;
/**
* The test "tearDownAfterClass" closure.
* The test's after all closure.
*/
private static ?Closure $__afterAll = null;
@ -82,7 +82,8 @@ trait Testable
if ($test->hasMethod($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);
}
}
@ -235,7 +236,7 @@ trait Testable
{
$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');
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
@ -320,7 +321,7 @@ trait Testable
*/
public function getPrintableTestCaseMethodName(): string
{
return $this->__testDescription;
return $this->__description;
}
/**
@ -328,6 +329,6 @@ trait Testable
*/
public static function getLatestPrintableTestCaseMethodName(): string
{
return self::$__latestTestDescription;
return self::$__latestDescription;
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -22,45 +22,45 @@ final class TestCaseMethodFactory
use HigherOrderable;
/**
* D=fghjkl
* The test's describing, if any.
*/
public ?string $describing = null;
/**
* Determines if the Test Case Method is a "todo".
* Determines if the test is a "todo".
*/
public bool $todo = false;
/**
* The Test Case Dataset, if any.
* The test's datasets.
*
* @var array<Closure|iterable<int|string, mixed>|string>
*/
public array $datasets = [];
/**
* The Test Case depends, if any.
* The test's dependencies.
*
* @var array<int, string>
*/
public array $depends = [];
/**
* The Test Case groups, if any.
* The test's groups.
*
* @var array<int, string>
*/
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>
*/
public array $covers = [];
/**
* Creates a new Factory instance.
* Creates a new test case method factory instance.
*/
public function __construct(
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
{
@ -89,7 +89,6 @@ final class TestCaseMethodFactory
$testCase = TestSuite::getInstance()->tests->get($this->filename);
$concrete->__describeDescription = $this->describing; // @phpstan-ignore-line
$testCase->factoryProxies->proxy($concrete);
$this->factoryProxies->proxy($concrete);

View File

@ -15,12 +15,4 @@ final class PendingCalls
* The current describe call.
*/
public static ?string $describing = null;
/**
* Sets the current describe call.
*/
public static function describe(DescribeCall $describeCall): void
{
}
}

View File

@ -54,7 +54,7 @@ final class AfterEachCall
$proxies = $this->proxies;
$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
)->bindTo($this, self::class);

View File

@ -59,13 +59,17 @@ final class BeforeEachCall
$testCaseProxies = $this->testCaseProxies;
$beforeEachTestCall = function (TestCall $testCall) use ($describing) : void {
if ($describing === $this->describing && $describing === $testCall->describing) {
$this->testCallProxies->chain($testCall);
if ($describing !== $this->describing) {
return;
}
if ($describing !== $testCall->describing) {
return;
}
$this->testCallProxies->chain($testCall);
};
$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
)->bindTo($this, self::class);