From efca71f1e7e45bbed4b590ce7fda50033556f2a0 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 17 Feb 2023 20:56:10 +0000 Subject: [PATCH] fix: test suite loader --- overrides/Runner/TestSuiteLoader.php | 27 +++++++++++++------ tests/.snapshots/success.txt | 6 ++--- .../SubFolder2/UsesPerFile.php | 2 +- tests/Visual/Parallel.php | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/overrides/Runner/TestSuiteLoader.php b/overrides/Runner/TestSuiteLoader.php index 2b272e1e..055ace22 100644 --- a/overrides/Runner/TestSuiteLoader.php +++ b/overrides/Runner/TestSuiteLoader.php @@ -41,6 +41,7 @@ use function array_values; use function basename; use function class_exists; use function get_declared_classes; +use Exception; use Pest\Contracts\HasPrintableTestCaseName; use Pest\TestCases\IgnorableTestCase; use Pest\TestSuite; @@ -101,11 +102,27 @@ final class TestSuiteLoader } $testCaseFound = false; + $class = false; foreach (array_reverse($loadedClasses) as $loadedClass) { - if (is_subclass_of($loadedClass, HasPrintableTestCaseName::class) || is_subclass_of($loadedClass, TestCase::class)) { - $suiteClassName = $loadedClass; + if ( + is_subclass_of($loadedClass, HasPrintableTestCaseName::class) + || is_subclass_of($loadedClass, TestCase::class)) { + try { + $class = new ReflectionClass($loadedClass); + // @codeCoverageIgnoreStart + } catch (ReflectionException) { + continue; + } + + if ($class->isAbstract() || ($class->getFileName() !== $suiteClassFile)) { + if (! str_contains($class->getFileName(), 'TestCaseFactory.php')) { + continue; + } + } + + $suiteClassName = $loadedClass; $testCaseFound = true; break; @@ -120,12 +137,6 @@ final class TestSuiteLoader return $this->exceptionFor($suiteClassName, $suiteClassFile); } - try { - $class = new ReflectionClass($suiteClassName); - // @codeCoverageIgnoreStart - } catch (ReflectionException $e) { - throw new Exception($e->getMessage(), (int) $e->getCode(), $e); - } // @codeCoverageIgnoreEnd if ($class->isSubclassOf(TestCase::class) && ! $class->isAbstract()) { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 7af47ab2..25d6aac3 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -884,9 +884,9 @@ ✓ it gets property values PASS Tests\Unit\Support\Str - ✓ it evaluates the code with ('version()', 'version__') - ✓ it evaluates the code with ('version__ ', 'version___') - ✓ it evaluates the code with ('version\', 'version_') + ✓ it evaluates the code with ('version()', '__pest_evaluable_version__') + ✓ it evaluates the code with ('version__ ', '__pest_evaluable_version___') + ✓ it evaluates the code with ('version\', '__pest_evaluable_version_') PASS Tests\Unit\TestSuite ✓ it does not allow to add the same test description twice diff --git a/tests/PHPUnit/CustomTestCaseInSubFolders/SubFolder2/UsesPerFile.php b/tests/PHPUnit/CustomTestCaseInSubFolders/SubFolder2/UsesPerFile.php index 8e582bcf..2aec8877 100644 --- a/tests/PHPUnit/CustomTestCaseInSubFolders/SubFolder2/UsesPerFile.php +++ b/tests/PHPUnit/CustomTestCaseInSubFolders/SubFolder2/UsesPerFile.php @@ -8,7 +8,7 @@ trait MyCustomTrait } } -class MyCustomClass extends PHPUnit\Framework\TestCase +abstract class MyCustomClass extends PHPUnit\Framework\TestCase { public function assertTrueIsTrue() { diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 5b622870..539d2fad 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -14,5 +14,5 @@ $run = function () { test('parallel', function () use ($run) { expect($run())->toContain('Running 650 tests using 3 processes') - ->toContain('Tests: 4 incomplete, 4 todos, 15 skipped, 627 passed (1546 assertions)'); + ->toContain('Tests: 4 incomplete, 4 todos, 15 skipped, 626 passed (1545 assertions)'); });