Execute all parent beforeEach and afterEach functions for each test

This commit is contained in:
jshayes
2024-10-11 23:32:45 -04:00
parent 0c57142c03
commit a6cd83665c
13 changed files with 77 additions and 22 deletions

View File

@ -61,8 +61,10 @@ trait Testable
/**
* The test's describing, if any.
*
* @var string[]
*/
public ?string $__describing = null;
public array $__describing = [];
/**
* Whether the test has ran or not.

View File

@ -31,8 +31,10 @@ final class TestCaseMethodFactory
/**
* The test's describing, if any.
*
* @var string[]
*/
public ?string $describing = null;
public array $describing = [];
/**
* The test's description, if any.
@ -201,7 +203,7 @@ final class TestCaseMethodFactory
];
foreach ($this->depends as $depend) {
$depend = Str::evaluable($this->describing !== null ? Str::describe($this->describing, $depend) : $depend);
$depend = Str::evaluable($this->describing === [] ? $depend : Str::describe($this->describing, $depend));
$this->attributes[] = new Attribute(
\PHPUnit\Framework\Attributes\Depends::class,

View File

@ -43,7 +43,7 @@ if (! function_exists('beforeAll')) {
*/
function beforeAll(Closure $closure): void
{
if (! is_null(DescribeCall::describing())) {
if (DescribeCall::describing() !== []) {
$filename = Backtrace::file();
throw new BeforeAllWithinDescribe($filename);
@ -205,7 +205,7 @@ if (! function_exists('afterAll')) {
*/
function afterAll(Closure $closure): void
{
if (! is_null(DescribeCall::describing())) {
if (DescribeCall::describing() !== []) {
$filename = Backtrace::file();
throw new AfterAllWithinDescribe($filename);

View File

@ -54,7 +54,7 @@ final class AfterEachCall
$proxies = $this->proxies;
$afterEachTestCase = ChainableClosure::boundWhen(
fn (): bool => is_null($describing) || $this->__describing === $describing,
fn (): bool => $describing === [] || in_array(end($describing), $this->__describing, true),
ChainableClosure::bound(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
)->bindTo($this, self::class);

View File

@ -63,12 +63,12 @@ final class BeforeEachCall
$beforeEachTestCall = function (TestCall $testCall) use ($describing): void {
if ($this->describing !== null) {
if ($describing !== $this->describing) {
if ($this->describing !== []) {
if (end($describing) !== end($this->describing)) {
return;
}
if ($describing !== $testCall->describing) {
if (! in_array(end($describing), $testCall->describing, true)) {
return;
}
}
@ -77,7 +77,7 @@ final class BeforeEachCall
};
$beforeEachTestCase = ChainableClosure::boundWhen(
fn (): bool => is_null($describing) || $this->__describing === $describing,
fn (): bool => $describing === [] || in_array(end($describing), $this->__describing, true),
ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line
)->bindTo($this, self::class);
@ -96,7 +96,7 @@ final class BeforeEachCall
*/
public function after(Closure $closure): self
{
if ($this->describing === null) {
if ($this->describing === []) {
throw new AfterBeforeTestFunction($this->filename);
}

View File

@ -11,11 +11,15 @@ trait Describable
{
/**
* Note: this is property is not used; however, it gets added automatically by rector php.
*
* @var string[]
*/
public string $__describing;
public array $__describing;
/**
* The describing of the test case.
*
* @var string[]
*/
public ?string $describing = null;
public array $describing = [];
}

View File

@ -39,10 +39,12 @@ final class DescribeCall
/**
* What is the current describing.
*
* @return string[]
*/
public static function describing(): ?string
public static function describing(): array
{
return self::$describing[count(self::$describing) - 1] ?? null;
return self::$describing;
}
/**
@ -73,7 +75,7 @@ final class DescribeCall
if (! $this->currentBeforeEachCall instanceof \Pest\PendingCalls\BeforeEachCall) {
$this->currentBeforeEachCall = new BeforeEachCall(TestSuite::getInstance(), $filename);
$this->currentBeforeEachCall->describing = $this->description;
$this->currentBeforeEachCall->describing[] = $this->description;
}
$this->currentBeforeEachCall->{$name}(...$arguments); // @phpstan-ignore-line

View File

@ -76,7 +76,7 @@ final class TestCall // @phpstan-ignore-line
throw new TestDescriptionMissing($this->filename);
}
$description = is_null($this->describing)
$description = $this->describing === []
? $this->description
: Str::describe($this->describing, $this->description);
@ -683,7 +683,7 @@ final class TestCall // @phpstan-ignore-line
throw new TestDescriptionMissing($this->filename);
}
if (! is_null($this->describing)) {
if ($this->describing !== []) {
$this->testCaseMethod->describing = $this->describing;
$this->testCaseMethod->description = Str::describe($this->describing, $this->description);
} else {

View File

@ -103,10 +103,14 @@ final class Str
/**
* Creates a describe block as `$describeDescription` → `$testDescription` format.
*
* @param string[] $describeDescriptions
*/
public static function describe(string $describeDescription, string $testDescription): string
public static function describe(array $describeDescriptions, string $testDescription): string
{
return sprintf('`%s` → %s', $describeDescription, $testDescription);
$descriptionComponents = [...$describeDescriptions, $testDescription];
return sprintf(str_repeat('`%s` → ', count($describeDescriptions)).'%s', ...$descriptionComponents);
}
/**