fix: before all

This commit is contained in:
Nuno Maduro
2025-07-25 20:54:37 -06:00
parent 6d6e4e040f
commit de4409e368
6 changed files with 34 additions and 7 deletions

View File

@ -52,6 +52,8 @@ use PHPUnit\Util\Filter;
use PHPUnit\Util\ThrowableToStringMapper; use PHPUnit\Util\ThrowableToStringMapper;
/** /**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit * @internal This class is not covered by the backward compatibility promise for PHPUnit
*/ */
final readonly class ThrowableBuilder final readonly class ThrowableBuilder
@ -82,7 +84,7 @@ final readonly class ThrowableBuilder
$t->getMessage(), $t->getMessage(),
ThrowableToStringMapper::map($t), ThrowableToStringMapper::map($t),
$trace, $trace,
$previous $previous,
); );
} }
} }

View File

@ -6,6 +6,7 @@ namespace Pest\Concerns;
use Closure; use Closure;
use Pest\Exceptions\DatasetArgumentsMismatch; use Pest\Exceptions\DatasetArgumentsMismatch;
use Pest\Panic;
use Pest\Preset; use Pest\Preset;
use Pest\Support\ChainableClosure; use Pest\Support\ChainableClosure;
use Pest\Support\ExceptionTrace; use Pest\Support\ExceptionTrace;
@ -194,7 +195,11 @@ trait Testable
$beforeAll = ChainableClosure::boundStatically(self::$__beforeAll, $beforeAll); $beforeAll = ChainableClosure::boundStatically(self::$__beforeAll, $beforeAll);
} }
try {
call_user_func(Closure::bind($beforeAll, null, self::class)); call_user_func(Closure::bind($beforeAll, null, self::class));
} catch (Throwable $e) {
Panic::with($e);
}
} }
/** /**
@ -222,8 +227,6 @@ trait Testable
$method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name());
$method->setUp($this);
$description = $method->description; $description = $method->description;
if ($this->dataName()) { if ($this->dataName()) {
$description = str_contains((string) $description, ':dataset') $description = str_contains((string) $description, ':dataset')
@ -287,6 +290,8 @@ trait Testable
self::$__latestPrs = $method->prs; self::$__latestPrs = $method->prs;
$this->__describing = $method->describing; $this->__describing = $method->describing;
$this->__test = $method->getClosure(); $this->__test = $method->getClosure();
$method->setUp($this);
} }
} }

View File

@ -1453,6 +1453,11 @@
✓ nested → nested afterEach execution order ✓ nested → nested afterEach execution order
✓ global afterEach execution order ✓ global afterEach execution order
PASS Tests\Hooks\BeforeAllTest
✓ it gets called before all tests 1 @ repetition 1 of 2
✓ it gets called before all tests 1 @ repetition 2 of 2
✓ it gets called before all tests 2
PASS Tests\Hooks\BeforeEachTest PASS Tests\Hooks\BeforeEachTest
✓ global beforeEach execution order ✓ global beforeEach execution order
@ -1726,4 +1731,4 @@
WARN Tests\Visual\Version WARN Tests\Visual\Version
- visual snapshot of help command output - visual snapshot of help command output
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1154 passed (2754 assertions) Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1157 passed (2766 assertions)

View File

@ -0,0 +1,16 @@
<?php
pest()->beforeAll(function () {
expect($_SERVER['globalHook']->calls->beforeAll)
->toBe(0);
$_SERVER['globalHook']->calls->beforeAll++;
});
it('gets called before all tests 1', function () {
expect($_SERVER['globalHook']->calls->beforeAll)->toBe(1);
})->repeat(2);
it('gets called before all tests 2', function () {
expect($_SERVER['globalHook']->calls->beforeAll)->toBe(1);
});

View File

@ -29,7 +29,6 @@ pest()
}) })
->beforeAll(function () { ->beforeAll(function () {
$_SERVER['globalHook']->beforeAll = 0; $_SERVER['globalHook']->beforeAll = 0;
$_SERVER['globalHook']->calls->beforeAll++;
}) })
->afterEach(function () { ->afterEach(function () {
if (! isset($this->ith)) { if (! isset($this->ith)) {

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) { test('parallel', function () use ($run) {
expect($run('--exclude-group=integration')) expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1144 passed (2730 assertions)') ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1147 passed (2742 assertions)')
->toContain('Parallel: 3 processes'); ->toContain('Parallel: 3 processes');
})->skipOnWindows(); })->skipOnWindows();