feat(describe): improves logic around hooks

This commit is contained in:
Nuno Maduro
2023-05-24 23:21:15 +01:00
parent 9c0e5ddfc6
commit 465c65243d
15 changed files with 433 additions and 35 deletions

View File

@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Pest\PendingCalls;
use Closure;
use Pest\PendingCalls;
use Pest\PendingCalls\Concerns\Describable;
use Pest\Support\Backtrace;
use Pest\Support\ChainableClosure;
use Pest\Support\HigherOrderMessageCollection;
@ -16,6 +18,8 @@ use Pest\TestSuite;
*/
final class BeforeEachCall
{
use Describable;
/**
* Holds the before each closure.
*/
@ -35,7 +39,7 @@ final class BeforeEachCall
* Creates a new Pending Call.
*/
public function __construct(
private readonly TestSuite $testSuite,
public readonly TestSuite $testSuite,
private readonly string $filename,
Closure $closure = null
) {
@ -50,17 +54,29 @@ final class BeforeEachCall
*/
public function __destruct()
{
$testCaseProxies = $this->testCaseProxies;
PendingCalls::beforeEach($this, function (string $describing = null) {
$testCaseProxies = $this->testCaseProxies;
$this->testSuite->beforeEach->set(
$this->filename,
function (TestCall $testCall): void {
$this->testCallProxies->chain($testCall);
},
ChainableClosure::from(function () use ($testCaseProxies): void {
$testCaseProxies->chain($this);
}, $this->closure),
);
$beforeEachTestCall = function (TestCall $testCall) use ($describing): void {
if (is_null($describing) || ($this->describing === $testCall->describing && $testCall->describing === $describing)) {
$this->testCallProxies->chain($testCall);
}
};
$beforeEachTestCase = ChainableClosure::when(
fn () => is_null($describing) || $this->__describeDescription === $describing, // @phpstan-ignore-line
ChainableClosure::from(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
)->bindTo($this, self::class);
assert($beforeEachTestCase instanceof Closure);
$this->testSuite->beforeEach->set(
$this->filename,
$this,
$beforeEachTestCall,
$beforeEachTestCase,
);
});
}
/**
@ -70,6 +86,7 @@ final class BeforeEachCall
*/
public function __call(string $name, array $arguments): self
{
if (method_exists(TestCall::class, $name)) {
$this->testCallProxies->add(Backtrace::file(), Backtrace::line(), $name, $arguments);