From 168c1cfdf79e5b19b57cb03060fc9a6a79c5f582 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 17 Jan 2023 11:08:49 +0100 Subject: [PATCH] PHPUnit 9.5.28: fix handling of test-like classes (#717) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jáchym Toušek --- src/Parser/Parser.php | 9 ++++++-- test/TestBase.php | 3 +-- test/Unit/Runners/PHPUnit/SuiteLoaderTest.php | 22 ++++++++++++++----- test/fixtures/enum_tests/MyTest.php | 16 ++++++++++++++ test/fixtures/enum_tests/NonTestFileTest.php | 11 ++++++++++ test/fixtures/phpunit-enum.xml | 8 +++++++ 6 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/enum_tests/MyTest.php create mode 100644 test/fixtures/enum_tests/NonTestFileTest.php create mode 100644 test/fixtures/phpunit-enum.xml diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index afe06dc3..475bbd30 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -43,7 +43,12 @@ public function __construct(string $srcPath) if (! isset(self::$alreadyLoadedSources[$srcPath])) { $declaredClasses = get_declared_classes(); try { - self::$alreadyLoadedSources[$srcPath] = (new StandardTestSuiteLoader())->load($srcPath); + $refClass = (new StandardTestSuiteLoader())->load($srcPath); + if (! $refClass->isSubclassOf(TestCase::class)) { + throw new NoClassInFileException($srcPath); + } + + self::$alreadyLoadedSources[$srcPath] = $refClass; self::$externalClassesFound += array_diff( get_declared_classes(), @@ -65,7 +70,7 @@ public function __construct(string $srcPath) } if ($reflFound === null || ! $reflFound->isSubclassOf(TestCase::class) || $reflFound->isAbstract()) { - throw new NoClassInFileException('', 0, $exception); + throw new NoClassInFileException($srcPath, 0, $exception); } self::$alreadyLoadedSources[$srcPath] = $reflFound; diff --git a/test/TestBase.php b/test/TestBase.php index 3dd529fb..1edccbee 100644 --- a/test/TestBase.php +++ b/test/TestBase.php @@ -118,8 +118,7 @@ final protected function fixture(string $fixture): string /** @return mixed */ final protected function getObjectValue(object $object, string $property) { - $refl = new ReflectionObject($object); - $prop = $refl->getProperty($property); + $prop = (new ReflectionObject($object))->getProperty($property); $prop->setAccessible(true); return $prop->getValue($object); diff --git a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php index 9e19eab2..ec2e5344 100644 --- a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php +++ b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php @@ -73,8 +73,7 @@ public function testLoadTestsuiteFileFromConfig(): void $loader = $this->loadSuite(); $files = $this->getObjectValue($loader, 'files'); - $expected = 1; - static::assertCount($expected, $files); + static::assertCount(1, $files); } public function testLoadTestsuiteFilesFromConfigWhileIgnoringExcludeTag(): void @@ -83,8 +82,7 @@ public function testLoadTestsuiteFilesFromConfigWhileIgnoringExcludeTag(): void $loader = $this->loadSuite(); $files = $this->getObjectValue($loader, 'files'); - $expected = 1; - static::assertCount($expected, $files); + static::assertCount(1, $files); } public function testLoadTestsuiteFilesFromDirFromConfigWhileRespectingExcludeTag(): void @@ -93,8 +91,7 @@ public function testLoadTestsuiteFilesFromDirFromConfigWhileRespectingExcludeTag $loader = $this->loadSuite(); $files = $this->getObjectValue($loader, 'files'); - $expected = 2; - static::assertCount($expected, $files); + static::assertCount(2, $files); } public function testLoadTestsuiteFilesFromConfig(): void @@ -393,6 +390,19 @@ public function testUngroupedTestShouldHaveDefaultGroupAssigned(): void static::assertCount(2, $loader->getTestMethods()); } + /** @requires PHP 8.1 */ + public function testLoadTestsuiteWithEnumThatEndsWithTestAndHasAnnotations(): void + { + $this->bareOptions['--configuration'] = $this->fixture('phpunit-enum.xml'); + + $loader = $this->loadSuite(); + $files = $this->getObjectValue($loader, 'files'); + + static::assertCount(2, $files); + static::assertCount(1, $loader->getSuites()); + static::assertCount(1, $loader->getTestMethods()); + } + /** @return string[] */ private function findTests(string $dir): array { diff --git a/test/fixtures/enum_tests/MyTest.php b/test/fixtures/enum_tests/MyTest.php new file mode 100644 index 00000000..23eca28e --- /dev/null +++ b/test/fixtures/enum_tests/MyTest.php @@ -0,0 +1,16 @@ + + + + + ./enum_tests/ + + +