feat(describe): more refactor

This commit is contained in:
Nuno Maduro
2023-05-26 20:01:55 +01:00
parent 68ea2c7d7e
commit 551fa01415
7 changed files with 14 additions and 32 deletions

View File

@ -142,7 +142,7 @@ trait Testable
} }
$this->{$property} = ($this->{$property} instanceof Closure) $this->{$property} = ($this->{$property} instanceof Closure)
? ChainableClosure::fromSameObject($this->{$property}, $hook) ? ChainableClosure::bound($this->{$property}, $hook)
: $hook; : $hook;
} }
@ -190,7 +190,7 @@ trait Testable
$beforeEach = TestSuite::getInstance()->beforeEach->get(self::$__filename)[1]; $beforeEach = TestSuite::getInstance()->beforeEach->get(self::$__filename)[1];
if ($this->__beforeEach instanceof Closure) { if ($this->__beforeEach instanceof Closure) {
$beforeEach = ChainableClosure::fromSameObject($this->__beforeEach, $beforeEach); $beforeEach = ChainableClosure::bound($this->__beforeEach, $beforeEach);
} }
$this->__callClosure($beforeEach, func_get_args()); $this->__callClosure($beforeEach, func_get_args());
@ -204,7 +204,7 @@ trait Testable
$afterEach = TestSuite::getInstance()->afterEach->get(self::$__filename); $afterEach = TestSuite::getInstance()->afterEach->get(self::$__filename);
if ($this->__afterEach instanceof Closure) { if ($this->__afterEach instanceof Closure) {
$afterEach = ChainableClosure::fromSameObject($this->__afterEach, $afterEach); $afterEach = ChainableClosure::bound($this->__afterEach, $afterEach);
} }
$this->__callClosure($afterEach, func_get_args()); $this->__callClosure($afterEach, func_get_args());

View File

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\PendingCalls\DescribeCall;
/**
* @internal
*/
final class PendingCalls
{
/**
* The current describe call.
*/
public static ?string $describing = null;
}

View File

@ -55,7 +55,7 @@ final class AfterEachCall
$afterEachTestCase = ChainableClosure::when( $afterEachTestCase = ChainableClosure::when(
fn (): bool => is_null($describing) || $this->__describing === $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::bound(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
)->bindTo($this, self::class); )->bindTo($this, self::class);
assert($afterEachTestCase instanceof Closure); assert($afterEachTestCase instanceof Closure);

View File

@ -70,7 +70,7 @@ final class BeforeEachCall
$beforeEachTestCase = ChainableClosure::when( $beforeEachTestCase = ChainableClosure::when(
fn (): bool => is_null($describing) || $this->__describing === $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::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
)->bindTo($this, self::class); )->bindTo($this, self::class);
assert($beforeEachTestCase instanceof Closure); assert($beforeEachTestCase instanceof Closure);

View File

@ -28,7 +28,7 @@ final class AfterEachRepository
if (array_key_exists($filename, $this->state)) { if (array_key_exists($filename, $this->state)) {
$fromAfterEachTestCase = $this->state[$filename]; $fromAfterEachTestCase = $this->state[$filename];
$afterEachTestCase = ChainableClosure::fromSameObject($fromAfterEachTestCase, $afterEachTestCase) $afterEachTestCase = ChainableClosure::bound($fromAfterEachTestCase, $afterEachTestCase)
->bindTo($afterEachCall, $afterEachCall::class); ->bindTo($afterEachCall, $afterEachCall::class);
} }
@ -44,7 +44,7 @@ final class AfterEachRepository
{ {
$afterEach = $this->state[$filename] ?? NullClosure::create(); $afterEach = $this->state[$filename] ?? NullClosure::create();
return ChainableClosure::fromSameObject(function (): void { return ChainableClosure::bound(function (): void {
if (class_exists(Mockery::class)) { if (class_exists(Mockery::class)) {
if ($container = Mockery::getContainer()) { if ($container = Mockery::getContainer()) {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */

View File

@ -27,8 +27,8 @@ final class BeforeEachRepository
if (array_key_exists($filename, $this->state)) { if (array_key_exists($filename, $this->state)) {
[$fromBeforeEachTestCall, $fromBeforeEachTestCase] = $this->state[$filename]; [$fromBeforeEachTestCall, $fromBeforeEachTestCase] = $this->state[$filename];
$beforeEachTestCall = ChainableClosure::fromDifferentObjects($fromBeforeEachTestCall, $beforeEachTestCall); $beforeEachTestCall = ChainableClosure::unbound($fromBeforeEachTestCall, $beforeEachTestCall);
$beforeEachTestCase = ChainableClosure::fromSameObject($fromBeforeEachTestCase, $beforeEachTestCase)->bindTo($beforeEachCall, $beforeEachCall::class); $beforeEachTestCase = ChainableClosure::bound($fromBeforeEachTestCase, $beforeEachTestCase)->bindTo($beforeEachCall, $beforeEachCall::class);
} }
assert($beforeEachTestCall instanceof Closure); assert($beforeEachTestCall instanceof Closure);

View File

@ -29,9 +29,9 @@ final class ChainableClosure
} }
/** /**
* Calls the given `$closure` and chains the `$next` closure. * Calls the given `$closure` and chains the `$next` closure, "bound" to the same object.
*/ */
public static function fromSameObject(Closure $closure, Closure $next): Closure public static function bound(Closure $closure, Closure $next): Closure
{ {
return function () use ($closure, $next): void { return function () use ($closure, $next): void {
if (! is_object($this)) { // @phpstan-ignore-line if (! is_object($this)) { // @phpstan-ignore-line
@ -44,9 +44,9 @@ final class ChainableClosure
} }
/** /**
* Calls the given `$closure` and chains the `$next` closure. * Calls the given `$closure` and chains the `$next` closure, "unbound" of any object.
*/ */
public static function fromDifferentObjects(Closure $closure, Closure $next): Closure public static function unbound(Closure $closure, Closure $next): Closure
{ {
return function () use ($closure, $next): void { return function () use ($closure, $next): void {
$closure(...func_get_args()); $closure(...func_get_args());