diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 59dd5db4..af642d43 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -161,7 +161,11 @@ trait Testable $afterAll = ChainableClosure::boundStatically(self::$__afterAll, $afterAll); } - call_user_func(Closure::bind($afterAll, null, self::class)); + try { + call_user_func(Closure::bind($afterAll, null, self::class)); + } finally { + self::flush(); + } parent::tearDownAfterClass(); } @@ -292,9 +296,6 @@ trait Testable parent::tearDown(); TestSuite::getInstance()->test = null; - - $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); - $method->tearDown($this); } } diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index 8808cf36..f94bd12e 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -110,11 +110,6 @@ final class TestCaseMethodFactory $this->factoryProxies->proxy($concrete); } - public function tearDown(TestCase $concrete): void - { - $concrete::flush(); // @phpstan-ignore-line - } - public function getClosure(): Closure { $closure = $this->closure; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 871b52df..e08e2e2f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1715,6 +1715,11 @@ PASS Tests\Helpers\TestInHelpers ✓ it executes tests in the Helpers directory + PASS Tests\Hooks\AfterAllTest + ✓ it does not get called before all tests 1 @ repetition 1 of 2 + ✓ it does not get called before all tests 1 @ repetition 2 of 2 + ✓ it does not get called before all tests 2 + PASS Tests\Hooks\AfterEachTest ✓ nested → nested afterEach execution order ✓ global afterEach execution order @@ -2265,6 +2270,9 @@ ✓ it can look up registered test methods by evaluable name ✓ it can return an array of all test suite filenames + PASS Tests\Visual\AfterAll + ✓ afterAll hooks run once after all tests of the file + PASS Tests\Visual\BeforeEachTestName ✓ description ✓ latest description @@ -2330,4 +2338,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1655 passed (3663 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1659 passed (3677 assertions) \ No newline at end of file diff --git a/tests/Fixtures/Suites/AfterAll/AfterAllTest.php b/tests/Fixtures/Suites/AfterAll/AfterAllTest.php new file mode 100644 index 00000000..f89496e2 --- /dev/null +++ b/tests/Fixtures/Suites/AfterAll/AfterAllTest.php @@ -0,0 +1,15 @@ +toBeTrue(); +}); + +it('runs the second test', function (): void { + expect(true)->toBeTrue(); +})->repeat(2); diff --git a/tests/Fixtures/Suites/AfterAll/Pest.php b/tests/Fixtures/Suites/AfterAll/Pest.php new file mode 100644 index 00000000..0d771dc1 --- /dev/null +++ b/tests/Fixtures/Suites/AfterAll/Pest.php @@ -0,0 +1,7 @@ +afterAll(function (): void { + file_put_contents((string) getenv('PEST_AFTER_ALL_MARKER'), 'pest()->afterAll'.PHP_EOL, FILE_APPEND); +}); diff --git a/tests/Hooks/AfterAllTest.php b/tests/Hooks/AfterAllTest.php new file mode 100644 index 00000000..729f701a --- /dev/null +++ b/tests/Hooks/AfterAllTest.php @@ -0,0 +1,28 @@ +calls->afterAllInFile = 0; +$_SERVER['globalHook']->calls->afterAllTestsRan = 0; + +pest()->afterAll(function (): void { + $_SERVER['globalHook']->calls->afterAllInFile++; +}); + +it('does not get called before all tests 1', function (): void { + $_SERVER['globalHook']->calls->afterAllTestsRan++; + expect($_SERVER['globalHook']->calls->afterAllInFile)->toBe(0); +})->repeat(2); + +it('does not get called before all tests 2', function (): void { + $_SERVER['globalHook']->calls->afterAllTestsRan++; + expect($_SERVER['globalHook']->calls->afterAllInFile)->toBe(0); +}); + +register_shutdown_function(function (): void { + if ($_SERVER['globalHook']->calls->afterAllTestsRan === 0) { + return; + } + + assertSame(1, $_SERVER['globalHook']->calls->afterAllInFile, 'Expected the [pest()->afterAll()] hook to have been called exactly once.'); +}); diff --git a/tests/Visual/AfterAll.php b/tests/Visual/AfterAll.php new file mode 100644 index 00000000..9d1a0f64 --- /dev/null +++ b/tests/Visual/AfterAll.php @@ -0,0 +1,31 @@ + 'DefaultPrinter', + 'COLLISION_IGNORE_DURATION' => 'true', + 'PAO_DISABLE' => '1', + 'PEST_AFTER_ALL_MARKER' => $marker, + ]); + + $process->run(); + + $calls = file_get_contents($marker); + + unlink($marker); + + expect($process->getExitCode())->toBe(0) + ->and($calls)->toBe('pest()->afterAll'.PHP_EOL.'afterAll'.PHP_EOL); +})->skipOnWindows(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index c4dce3fb..b1b9c1c9 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ test('parallel', function () use ($run): void { $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 29 skipped, 1627 passed (3587 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 29 skipped, 1630 passed (3599 assertions)'; expect($output) ->toContain("Tests: {$expected}")