mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(describe): more refactors
This commit is contained in:
@ -98,7 +98,7 @@ trait Testable
|
|||||||
}
|
}
|
||||||
|
|
||||||
self::$__beforeAll = (self::$__beforeAll instanceof Closure)
|
self::$__beforeAll = (self::$__beforeAll instanceof Closure)
|
||||||
? ChainableClosure::fromStatic(self::$__beforeAll, $hook)
|
? ChainableClosure::boundStatically(self::$__beforeAll, $hook)
|
||||||
: $hook;
|
: $hook;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ trait Testable
|
|||||||
}
|
}
|
||||||
|
|
||||||
self::$__afterAll = (self::$__afterAll instanceof Closure)
|
self::$__afterAll = (self::$__afterAll instanceof Closure)
|
||||||
? ChainableClosure::fromStatic(self::$__afterAll, $hook)
|
? ChainableClosure::boundStatically(self::$__afterAll, $hook)
|
||||||
: $hook;
|
: $hook;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ trait Testable
|
|||||||
$beforeAll = TestSuite::getInstance()->beforeAll->get(self::$__filename);
|
$beforeAll = TestSuite::getInstance()->beforeAll->get(self::$__filename);
|
||||||
|
|
||||||
if (self::$__beforeAll instanceof Closure) {
|
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));
|
call_user_func(Closure::bind($beforeAll, null, self::class));
|
||||||
@ -170,7 +170,7 @@ trait Testable
|
|||||||
$afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename);
|
$afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename);
|
||||||
|
|
||||||
if (self::$__afterAll instanceof Closure) {
|
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));
|
call_user_func(Closure::bind($afterAll, null, self::class));
|
||||||
|
|||||||
@ -53,7 +53,7 @@ final class AfterEachCall
|
|||||||
|
|
||||||
$proxies = $this->proxies;
|
$proxies = $this->proxies;
|
||||||
|
|
||||||
$afterEachTestCase = ChainableClosure::when(
|
$afterEachTestCase = ChainableClosure::boundWhen(
|
||||||
fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line
|
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
|
ChainableClosure::bound(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
||||||
)->bindTo($this, self::class);
|
)->bindTo($this, self::class);
|
||||||
|
|||||||
@ -68,7 +68,7 @@ final class BeforeEachCall
|
|||||||
$this->testCallProxies->chain($testCall);
|
$this->testCallProxies->chain($testCall);
|
||||||
};
|
};
|
||||||
|
|
||||||
$beforeEachTestCase = ChainableClosure::when(
|
$beforeEachTestCase = ChainableClosure::boundWhen(
|
||||||
fn (): bool => is_null($describing) || $this->__describing === $describing, // @phpstan-ignore-line
|
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
|
ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
|
||||||
)->bindTo($this, self::class);
|
)->bindTo($this, self::class);
|
||||||
|
|||||||
@ -13,9 +13,9 @@ use Pest\Exceptions\ShouldNotHappen;
|
|||||||
final class ChainableClosure
|
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 {
|
return function () use ($condition, $next): void {
|
||||||
if (! is_object($this)) { // @phpstan-ignore-line
|
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 {
|
return static function () use ($closure, $next): void {
|
||||||
\Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args());
|
\Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args());
|
||||||
|
|||||||
Reference in New Issue
Block a user