From 68ea2c7d7e7980d19939cde0202844c7021b067c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 26 May 2023 19:56:10 +0100 Subject: [PATCH] feat(describe): refactor --- src/Concerns/Testable.php | 31 ++++++++++++----------- src/Exceptions/AfterEachAlreadyExist.php | 24 ------------------ src/Exceptions/BeforeEachAlreadyExist.php | 24 ------------------ src/Factories/TestCaseMethodFactory.php | 17 ++++++------- src/PendingCalls.php | 8 ------ src/PendingCalls/AfterEachCall.php | 2 +- src/PendingCalls/BeforeEachCall.php | 12 ++++++--- 7 files changed, 33 insertions(+), 85 deletions(-) delete mode 100644 src/Exceptions/AfterEachAlreadyExist.php delete mode 100644 src/Exceptions/BeforeEachAlreadyExist.php diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 9c1f716d..1b248cc1 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -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; } } diff --git a/src/Exceptions/AfterEachAlreadyExist.php b/src/Exceptions/AfterEachAlreadyExist.php deleted file mode 100644 index cbdd4a7c..00000000 --- a/src/Exceptions/AfterEachAlreadyExist.php +++ /dev/null @@ -1,24 +0,0 @@ -|string> */ public array $datasets = []; /** - * The Test Case depends, if any. + * The test's dependencies. * * @var array */ public array $depends = []; /** - * The Test Case groups, if any. + * The test's groups. * * @var array */ public array $groups = []; /** - * The covered classes and functions, if any. + * The covered classes and functions. * * @var array */ 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); diff --git a/src/PendingCalls.php b/src/PendingCalls.php index 886166bf..b5cbf04d 100644 --- a/src/PendingCalls.php +++ b/src/PendingCalls.php @@ -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 - { - - } } diff --git a/src/PendingCalls/AfterEachCall.php b/src/PendingCalls/AfterEachCall.php index 5388eedf..32ec937d 100644 --- a/src/PendingCalls/AfterEachCall.php +++ b/src/PendingCalls/AfterEachCall.php @@ -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); diff --git a/src/PendingCalls/BeforeEachCall.php b/src/PendingCalls/BeforeEachCall.php index b9c88f8f..7543cf39 100644 --- a/src/PendingCalls/BeforeEachCall.php +++ b/src/PendingCalls/BeforeEachCall.php @@ -58,14 +58,18 @@ final class BeforeEachCall $describing = $this->describing; $testCaseProxies = $this->testCaseProxies; - $beforeEachTestCall = function (TestCall $testCall) use ($describing): void { - if ($describing === $this->describing && $describing === $testCall->describing) { - $this->testCallProxies->chain($testCall); + $beforeEachTestCall = function (TestCall $testCall) use ($describing) : void { + 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);