Skip to content

Commit

Permalink
test: ensure we do not forget to test any short_open_tag test (PHP-CS…
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Jan 9, 2024
1 parent fc00ff1 commit 27ad94d
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ final class ProjectCodeTest extends TestCase
private static ?array $srcClassCases = null;

/**
* @var array<string, Tokens>
* @var array<class-string, Tokens>
*/
private static array $fileTokensCache = [];
private static array $tokensCache = [];

/**
* This structure contains older classes that are not yet covered by tests.
Expand All @@ -94,7 +94,7 @@ public static function tearDownAfterClass(): void
{
self::$srcClassCases = null;
self::$testClassCases = null;
self::$fileTokensCache = [];
self::$tokensCache = [];
}

public function testThatClassesWithoutTestsVarIsProper(): void
Expand Down Expand Up @@ -737,6 +737,25 @@ static function (string $className): bool {
);
}

public function testAllTestsForShortOpenTagAreHandled(): void
{
$testClassesWithShortOpenTag = array_filter(
self::getTestClasses(),
fn (string $className): bool => str_contains($this->getFileContentForClass($className), 'short_open_tag') && self::class !== $className
);
$testFilesWithShortOpenTag = array_map(
fn (string $className): string => './'.$this->getFilePathForClass($className),
$testClassesWithShortOpenTag
);

$phpunitXmlContent = file_get_contents(__DIR__.'/../../phpunit.xml.dist');
$phpunitFiles = (array) simplexml_load_string($phpunitXmlContent)->xpath('testsuites/testsuite[@name="short-open-tag"]')[0]->file;

sort($testFilesWithShortOpenTag);
sort($phpunitFiles);
self::assertSame($testFilesWithShortOpenTag, $phpunitFiles);
}

/**
* @return iterable<string, array{class-string<TestCase>}>
*/
Expand Down Expand Up @@ -840,18 +859,36 @@ private function extractFunctionNamesCalledInClass(string $className): array
return array_unique($strings);
}

private function createTokensForClass(string $className): Tokens
/**
* @param class-string $className
*/
private function getFilePathForClass(string $className): string
{
$file = $className;
$file = preg_replace('#^PhpCsFixer\\\Tests\\\#', 'tests\\', $file);
$file = preg_replace('#^PhpCsFixer\\\#', 'src\\', $file);
$file = str_replace('\\', \DIRECTORY_SEPARATOR, $file).'.php';

if (!isset(self::$fileTokensCache[$file])) {
self::$fileTokensCache[$file] = Tokens::fromCode(file_get_contents($file));
return str_replace('\\', \DIRECTORY_SEPARATOR, $file).'.php';
}

/**
* @param class-string $className
*/
private function getFileContentForClass(string $className): string
{
return file_get_contents($this->getFilePathForClass($className));
}

/**
* @param class-string $className
*/
private function createTokensForClass(string $className): Tokens
{
if (!isset(self::$tokensCache[$className])) {
self::$tokensCache[$className] = Tokens::fromCode(self::getFileContentForClass($className));
}

return self::$fileTokensCache[$file];
return self::$tokensCache[$className];
}

/**
Expand Down

0 comments on commit 27ad94d

Please sign in to comment.