Skip to content

Commit

Permalink
keep dump for reflection golden test out of repository
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Oct 14, 2023
1 parent c76d3a6 commit 53b9ab9
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 93,057 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
!.idea/icon.png
/tests/tmp
/tests/.phpunit.result.cache
/tests/PHPStan/Reflection/data/golden/*.test
tmp/.memory_limit
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ tests-levels:
tests-coverage:
php vendor/bin/paratest --runner WrapperRunner

tests-golden-reflection:
php vendor/bin/paratest --runner WrapperRunner --no-coverage tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

lint:
php vendor/bin/parallel-lint --colors \
--exclude tests/PHPStan/Analyser/data \
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<testsuites>
<testsuite name="PHPStan">
<directory suffix="Test.php">tests/PHPStan</directory>
<exclude>tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php</exclude>
</testsuite>
</testsuites>

Expand Down
121 changes: 112 additions & 9 deletions tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace PHPStan\Reflection;

use PhpParser\Node\Name;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\VerbosityLevel;
use function array_keys;
use function count;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function floor;
use function getenv;
use function implode;
use function ksort;
use function sort;
use function substr;
use function trim;
use const PHP_VERSION_ID;

Expand All @@ -20,13 +26,11 @@ class ReflectionProviderGoldenTest extends PHPStanTestCase
/** @return iterable<string, array<string>> */
public static function data(): iterable
{
$first = (int) floor(PHP_VERSION_ID / 10000);
$second = (int) (floor(PHP_VERSION_ID % 10000) / 100);
$currentVersion = $first . '.' . $second;
$contents = file_get_contents(__DIR__ . '/data/golden/reflection-' . $currentVersion . '.test');
$inputFile = self::getTestInputFile();
$contents = file_get_contents($inputFile);

if ($contents === false) {
self::fail('Reflection test data is missing for PHP ' . $currentVersion);
self::fail('Input file \'' . $inputFile . '\' is missing.');
}

$parts = explode('-----', $contents);
Expand Down Expand Up @@ -59,7 +63,106 @@ public function test(string $input, string $expectedOutput): void
$this->assertSame($expectedOutput, $output);
}

public static function generateFunctionDescription(string $functionName): string
public static function dumpOutput(): void

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Syntax error, unexpected ':' on line 66

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Syntax error, unexpected ':' on line 66

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Syntax error, unexpected ':' on line 66

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Syntax error, unexpected ':' on line 66

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Syntax error, unexpected ':' on line 66

Check failure on line 66 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Syntax error, unexpected ':' on line 66
{
$symbols = require_once __DIR__ . '/data/golden/phpSymbols.php';

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Syntax error, unexpected ';' on line 68

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Syntax error, unexpected ';' on line 68

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Syntax error, unexpected ';' on line 68

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Syntax error, unexpected ';' on line 68

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Syntax error, unexpected ';' on line 68

Check failure on line 68 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Syntax error, unexpected ';' on line 68

$functions = [];
$classes = [];

foreach ($symbols as $symbol) {
$parts = explode('::', $symbol);
$partsCount = count($parts);

if ($partsCount === 1) {
$functions[] = $parts[0];
} elseif ($partsCount === 2) {
[$class, $method] = $parts;
$classes[$class] ??= [
'methods' => [],
'properties' => [],
];

if (substr($method, 0, 1) === '$') {
$classes[$class]['properties'][] = substr($method, 1);
} else {
$classes[$class]['methods'][] = $method;
}
}
}

sort($functions);
ksort($classes);

foreach ($classes as &$class) {
sort($class['properties']);
sort($class['methods']);
}

unset($class);
$container = PHPStanTestCase::getContainer();
$reflectionProvider = $container->getByType(ReflectionProvider::class);

$separator = '-----';
$contents = '';

foreach ($functions as $function) {
$contents .= $separator . "\n";
$contents .= 'FUNCTION ' . $function . "\n";
$contents .= $separator . "\n";
$contents .= self::generateFunctionDescription($function);
}

foreach ($classes as $className => $class) {
$contents .= $separator . "\n";
$contents .= 'CLASS ' . $className . "\n";
$contents .= $separator . "\n";
$contents .= self::generateClassDescription($className);

if (! $reflectionProvider->hasClass($className)) {
continue;
}

foreach ($class['properties'] as $property) {
$contents .= $separator . "\n";
$contents .= 'PROPERTY ' . $className . '::' . $property . "\n";
$contents .= $separator . "\n";
$contents .= self::generateClassPropertyDescription($className . '::' . $property);
}

foreach ($class['methods'] as $method) {
$contents .= $separator . "\n";
$contents .= 'METHOD ' . $className . '::' . $method . "\n";
$contents .= $separator . "\n";
$contents .= self::generateClassMethodDescription($className . '::' . $method);
}
}

$result = file_put_contents(self::getTestInputFile(), $contents);

if ($result !== false) {
return;
}

throw new ShouldNotHappenException('Failed write dump for reflection golden test.');
}

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Syntax error, unexpected '}', expecting EOF on line 148

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Syntax error, unexpected '}', expecting EOF on line 148

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Syntax error, unexpected '}', expecting EOF on line 148

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Syntax error, unexpected '}', expecting EOF on line 148

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Syntax error, unexpected '}', expecting EOF on line 148

Check failure on line 148 in tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Syntax error, unexpected '}', expecting EOF on line 148

private static function getTestInputFile(): string
{
$fileFromEnv = getenv('REFLECTION_GOLDEN_TEST_FILE');

if ($fileFromEnv !== false) {
return $fileFromEnv;
}

$first = (int) floor(PHP_VERSION_ID / 10000);
$second = (int) (floor(PHP_VERSION_ID % 10000) / 100);
$currentVersion = $first . '.' . $second;

return __DIR__ . '/data/golden/reflection-' . $currentVersion . '.test';
}

private static function generateFunctionDescription(string $functionName): string
{
$nameNode = new Name($functionName);
$reflectionProvider = self::getContainer()->getByType(ReflectionProvider::class);
Expand All @@ -80,7 +183,7 @@ public static function generateFunctionDescription(string $functionName): string
return $result;
}

public static function generateClassDescription(string $className): string
private static function generateClassDescription(string $className): string
{
$reflectionProvider = self::getContainer()->getByType(ReflectionProvider::class);

Expand Down Expand Up @@ -164,7 +267,7 @@ public static function generateClassDescription(string $className): string
return $result;
}

public static function generateClassMethodDescription(string $classMethodName): string
private static function generateClassMethodDescription(string $classMethodName): string
{
[$className, $methodName] = explode('::', $classMethodName);

Expand Down Expand Up @@ -290,7 +393,7 @@ private static function generateVariantsDescription(string $name, array $variant
return $result;
}

public static function generateClassPropertyDescription(string $propertyName): string
private static function generateClassPropertyDescription(string $propertyName): string
{
[$className, $propertyName] = explode('::', $propertyName);

Expand Down
File renamed without changes.

0 comments on commit 53b9ab9

Please sign in to comment.