From 0ae08876659e31365fb79216664d7adb1d11a300 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 26 May 2023 20:04:50 +0100 Subject: [PATCH] feat(describe): more refactors --- src/Concerns/Testable.php | 8 ++++---- src/PendingCalls/AfterEachCall.php | 2 +- src/PendingCalls/BeforeEachCall.php | 2 +- src/Support/ChainableClosure.php | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 5c9e8b1f..273dfb75 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -98,7 +98,7 @@ trait Testable } self::$__beforeAll = (self::$__beforeAll instanceof Closure) - ? ChainableClosure::fromStatic(self::$__beforeAll, $hook) + ? ChainableClosure::boundStatically(self::$__beforeAll, $hook) : $hook; } @@ -112,7 +112,7 @@ trait Testable } self::$__afterAll = (self::$__afterAll instanceof Closure) - ? ChainableClosure::fromStatic(self::$__afterAll, $hook) + ? ChainableClosure::boundStatically(self::$__afterAll, $hook) : $hook; } @@ -156,7 +156,7 @@ trait Testable $beforeAll = TestSuite::getInstance()->beforeAll->get(self::$__filename); if (self::$__beforeAll instanceof Closure) { - $beforeAll = ChainableClosure::fromStatic(self::$__beforeAll, $beforeAll); + $beforeAll = ChainableClosure::boundStatically(self::$__beforeAll, $beforeAll); } call_user_func(Closure::bind($beforeAll, null, self::class)); @@ -170,7 +170,7 @@ trait Testable $afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename); if (self::$__afterAll instanceof Closure) { - $afterAll = ChainableClosure::fromStatic(self::$__afterAll, $afterAll); + $afterAll = ChainableClosure::boundStatically(self::$__afterAll, $afterAll); } call_user_func(Closure::bind($afterAll, null, self::class)); diff --git a/src/PendingCalls/AfterEachCall.php b/src/PendingCalls/AfterEachCall.php index 78c5a3c0..9a22258e 100644 --- a/src/PendingCalls/AfterEachCall.php +++ b/src/PendingCalls/AfterEachCall.php @@ -53,7 +53,7 @@ final class AfterEachCall $proxies = $this->proxies; - $afterEachTestCase = ChainableClosure::when( + $afterEachTestCase = ChainableClosure::boundWhen( fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line ChainableClosure::bound(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 b51dafd2..200c4fa9 100644 --- a/src/PendingCalls/BeforeEachCall.php +++ b/src/PendingCalls/BeforeEachCall.php @@ -68,7 +68,7 @@ final class BeforeEachCall $this->testCallProxies->chain($testCall); }; - $beforeEachTestCase = ChainableClosure::when( + $beforeEachTestCase = ChainableClosure::boundWhen( fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line )->bindTo($this, self::class); diff --git a/src/Support/ChainableClosure.php b/src/Support/ChainableClosure.php index b0257d11..b012e907 100644 --- a/src/Support/ChainableClosure.php +++ b/src/Support/ChainableClosure.php @@ -13,9 +13,9 @@ use Pest\Exceptions\ShouldNotHappen; final class ChainableClosure { /** - * Calls the given `$closure` when the given condition is true. + * Calls the given `$closure` when the given condition is true, "bound" to the same object. */ - public static function when(Closure $condition, Closure $next): Closure + public static function boundWhen(Closure $condition, Closure $next): Closure { return function () use ($condition, $next): void { if (! is_object($this)) { // @phpstan-ignore-line @@ -55,9 +55,9 @@ final class ChainableClosure } /** - * Call the given static `$closure` and chains the `$next` closure. + * Call the given static `$closure` and chains the `$next` closure, "bound" to the same object statically. */ - public static function fromStatic(Closure $closure, Closure $next): Closure + public static function boundStatically(Closure $closure, Closure $next): Closure { return static function () use ($closure, $next): void { \Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args());