From 7baa48e0683056c02a9d306e3329a7e5cd3e6cee Mon Sep 17 00:00:00 2001 From: Dylan Bridgman Date: Sat, 5 Aug 2023 12:24:43 +0200 Subject: [PATCH] TestSuiteLoader will always consider classes from the current file --- overrides/Runner/TestSuiteLoader.php | 16 +++++++++++++++ tests/.snapshots/success.txt | 8 +++++++- tests/.snapshots/todo.txt | 8 +++++++- tests/PHPUnit/CustomTestCase/ChildTest.php | 13 ++++++++++++ tests/PHPUnit/CustomTestCase/ParentTest.php | 22 +++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/PHPUnit/CustomTestCase/ChildTest.php create mode 100644 tests/PHPUnit/CustomTestCase/ParentTest.php diff --git a/overrides/Runner/TestSuiteLoader.php b/overrides/Runner/TestSuiteLoader.php index d997aeb0..b6a5e754 100644 --- a/overrides/Runner/TestSuiteLoader.php +++ b/overrides/Runner/TestSuiteLoader.php @@ -60,6 +60,11 @@ final class TestSuiteLoader */ private static array $loadedClasses = []; + /** + * @psalm-var array> + */ + private static array $loadedClassesByFilename = []; + /** * @psalm-var list */ @@ -97,6 +102,17 @@ final class TestSuiteLoader self::$loadedClasses = array_merge($loadedClasses, self::$loadedClasses); + foreach ($loadedClasses as $loadedClass) { + $reflection = new ReflectionClass($loadedClass); + $filename = $reflection->getFileName(); + self::$loadedClassesByFilename[$filename] = [ + $loadedClass, + ...self::$loadedClassesByFilename[$filename] ?? [], + ]; + } + + $loadedClasses = array_merge(self::$loadedClassesByFilename[$suiteClassFile] ?? [], $loadedClasses); + if (empty($loadedClasses)) { return $this->exceptionFor($suiteClassName, $suiteClassFile); } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 66c8c499..99001dfb 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1015,9 +1015,15 @@ PASS Tests\PHPUnit\CustomAffixes\snakecasespec ✓ it runs file names like snake_case_spec.php + PASS Tests\CustomTestCase\ChildTest + ✓ override method + PASS Tests\CustomTestCase\ExecutedTest ✓ that gets executed + PASS Tests\CustomTestCase\ParentTest + ✓ override method + PASS Tests\PHPUnit\CustomTestCase\UsesPerDirectory ✓ closure was bound to CustomTestCase @@ -1210,4 +1216,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 860 passed (1975 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 862 passed (1977 assertions) diff --git a/tests/.snapshots/todo.txt b/tests/.snapshots/todo.txt index 1d3687a5..414d1b98 100644 --- a/tests/.snapshots/todo.txt +++ b/tests/.snapshots/todo.txt @@ -19,7 +19,13 @@ ↓ something todo later chained ↓ something todo later chained and with function body + PASS Tests\CustomTestCase\ChildTest + ✓ override method + PASS Tests\CustomTestCase\ExecutedTest ✓ that gets executed - Tests: 13 todos, 1 passed (1 assertions) + PASS Tests\CustomTestCase\ParentTest + ✓ override method + + Tests: 13 todos, 3 passed (3 assertions) diff --git a/tests/PHPUnit/CustomTestCase/ChildTest.php b/tests/PHPUnit/CustomTestCase/ChildTest.php new file mode 100644 index 00000000..15ad37cc --- /dev/null +++ b/tests/PHPUnit/CustomTestCase/ChildTest.php @@ -0,0 +1,13 @@ +getEntity() || true); + } +} diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index fcb8d7df..d5f32b93 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 849 passed (1960 assertions)') + ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 851 passed (1962 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();