From b37244e02ba87b4595c1f2c7ad994fe6538f43ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 14 Apr 2024 11:14:42 +0200 Subject: [PATCH 1/3] test: Move the parsing of the file to the SpecParser --- tests/Scoper/PhpScoperSpecTest.php | 23 +++------------ tests/Scoper/Spec/SpecFinder.php | 3 ++ tests/Scoper/Spec/SpecParser.php | 47 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/tests/Scoper/PhpScoperSpecTest.php b/tests/Scoper/PhpScoperSpecTest.php index b43d79f6..fc209871 100644 --- a/tests/Scoper/PhpScoperSpecTest.php +++ b/tests/Scoper/PhpScoperSpecTest.php @@ -21,7 +21,6 @@ use Humbug\PhpScoper\Scoper\Spec\SpecFinder; use Humbug\PhpScoper\Scoper\Spec\SpecFormatter; use Humbug\PhpScoper\Scoper\Spec\SpecParser; -use Humbug\PhpScoper\Scoper\Spec\UnparsableFile; use Humbug\PhpScoper\Symbol\EnrichedReflector; use Humbug\PhpScoper\Symbol\Reflector; use Humbug\PhpScoper\Symbol\SymbolsRegistry; @@ -35,7 +34,6 @@ use function array_map; use function array_slice; use function array_values; -use function basename; use function explode; use function implode; use function rtrim; @@ -177,23 +175,10 @@ public static function provideValidFiles(): iterable [$sourceDir, $files] = SpecFinder::findSpecFiles(); foreach ($files as $file) { - try { - $fixtures = include $file; - - $meta = $fixtures['meta']; - unset($fixtures['meta']); - - foreach ($fixtures as $fixtureTitle => $fixtureSet) { - yield from SpecParser::parseSpecFile( - basename($sourceDir).'/'.$file->getRelativePathname(), - $meta, - $fixtureTitle, - $fixtureSet, - ); - } - } catch (Throwable $throwable) { - throw UnparsableFile::create($file, $throwable); - } + yield SpecParser::parseSpecFile( + $sourceDir, + $file, + ); } } diff --git a/tests/Scoper/Spec/SpecFinder.php b/tests/Scoper/Spec/SpecFinder.php index 94a85879..3bdfe5f0 100644 --- a/tests/Scoper/Spec/SpecFinder.php +++ b/tests/Scoper/Spec/SpecFinder.php @@ -14,11 +14,14 @@ namespace Humbug\PhpScoper\Scoper\Spec; +use Humbug\PhpScoper\NotInstantiable; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; final class SpecFinder { + use NotInstantiable; + public const SPECS_PATH = __DIR__.'/../../../specs'; public const TMP_SPECS_PATH = __DIR__.'/../../../_specs'; diff --git a/tests/Scoper/Spec/SpecParser.php b/tests/Scoper/Spec/SpecParser.php index 3ac9e833..7e4d63b4 100644 --- a/tests/Scoper/Spec/SpecParser.php +++ b/tests/Scoper/Spec/SpecParser.php @@ -20,8 +20,11 @@ use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory; use Humbug\PhpScoper\NotInstantiable; use InvalidArgumentException; +use PHPUnit\Framework\Assert; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; +use Symfony\Component\Finder\SplFileInfo; +use Throwable; use function array_diff; use function array_key_exists; use function array_keys; @@ -76,7 +79,51 @@ class SpecParser extends TestCase ConfigurationKeys::CONSTANTS_INTERNAL_SYMBOLS_KEYWORD, ]; + /** + * @throws UnparsableFile + */ public static function parseSpecFile( + string $sourceDir, + SplFileInfo $file, + ): iterable { + try { + $specs = include $file; + + self::checkSpecFileSchema($specs); + + $meta = $specs['meta']; + unset($specs['meta']); + + foreach ($specs as $fixtureTitle => $fixtureSet) { + yield from self::parseSpec( + basename($sourceDir).'/'.$file->getRelativePathname(), + $meta, + $fixtureTitle, + $fixtureSet, + ); + } + } catch (Throwable $throwable) { + throw UnparsableFile::create($file, $throwable); + } + } + + /** + * @phpstan-assert array{'meta': array, string: array} $specs + */ + private static function checkSpecFileSchema(mixed $specs): void + { + Assert::assertIsArray($specs); + + Assert::assertArrayHasKey('meta', $specs); + Assert::assertIsArray($specs['meta']); + + foreach ($specs as $title => $spec) { + Assert::assertIsString($title); + Assert::assertIsArray($spec); + } + } + + private static function parseSpec( string $file, array $meta, int|string $fixtureTitle, From 09e7d87a95395ced0e975375e35a786295317033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 14 Apr 2024 11:24:31 +0200 Subject: [PATCH 2/3] test: Add more checks against the spec file --- tests/Scoper/PhpScoperSpecTest.php | 5 +---- tests/Scoper/Spec/SpecParser.php | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/Scoper/PhpScoperSpecTest.php b/tests/Scoper/PhpScoperSpecTest.php index fc209871..f6256c72 100644 --- a/tests/Scoper/PhpScoperSpecTest.php +++ b/tests/Scoper/PhpScoperSpecTest.php @@ -175,10 +175,7 @@ public static function provideValidFiles(): iterable [$sourceDir, $files] = SpecFinder::findSpecFiles(); foreach ($files as $file) { - yield SpecParser::parseSpecFile( - $sourceDir, - $file, - ); + yield from SpecParser::parseSpecFile($sourceDir, $file); } } diff --git a/tests/Scoper/Spec/SpecParser.php b/tests/Scoper/Spec/SpecParser.php index 7e4d63b4..09e7cf7d 100644 --- a/tests/Scoper/Spec/SpecParser.php +++ b/tests/Scoper/Spec/SpecParser.php @@ -31,6 +31,7 @@ use function array_merge; use function implode; use function is_array; +use function is_int; use function is_string; use function Safe\preg_split; use function sprintf; @@ -108,7 +109,7 @@ public static function parseSpecFile( } /** - * @phpstan-assert array{'meta': array, string: array} $specs + * @phpstan-assert array{'meta': array, array-key: string|array} $specs */ private static function checkSpecFileSchema(mixed $specs): void { @@ -118,8 +119,7 @@ private static function checkSpecFileSchema(mixed $specs): void Assert::assertIsArray($specs['meta']); foreach ($specs as $title => $spec) { - Assert::assertIsString($title); - Assert::assertIsArray($spec); + Assert::assertTrue(is_string($spec) || is_array($spec)); } } From bd824be481f911fa47106962db89cf82b16cb18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 14 Apr 2024 11:31:42 +0200 Subject: [PATCH 3/3] fix cs --- tests/Scoper/Spec/SpecParser.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Scoper/Spec/SpecParser.php b/tests/Scoper/Spec/SpecParser.php index 09e7cf7d..92d4a605 100644 --- a/tests/Scoper/Spec/SpecParser.php +++ b/tests/Scoper/Spec/SpecParser.php @@ -31,7 +31,6 @@ use function array_merge; use function implode; use function is_array; -use function is_int; use function is_string; use function Safe\preg_split; use function sprintf;