Skip to content

Commit

Permalink
PHPUnit 9.5.28: fix handling of test-like classes (#717)
Browse files Browse the repository at this point in the history
Co-authored-by: Jáchym Toušek <enumag@gmail.com>
  • Loading branch information
Slamdunk and enumag committed Jan 17, 2023
1 parent cc75e7b commit 168c1cf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 16 additions & 6 deletions test/Unit/Runners/PHPUnit/SuiteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/enum_tests/MyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\enum_tests;

use PHPUnit\Framework\TestCase;

/** @coversNothing */
final class MyTest extends TestCase
{
public function testMe(): void
{
self::assertTrue(true);
}
}
11 changes: 11 additions & 0 deletions test/fixtures/enum_tests/NonTestFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\enum_tests;

/** @coversNothing */
enum NonTestFileTest: int
{
case BAR_BAZ = 987;
}
8 changes: 8 additions & 0 deletions test/fixtures/phpunit-enum.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="ParaTest Fixtures">
<directory>./enum_tests/</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 168c1cf

Please sign in to comment.