Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Move the parsing of the file to the SpecParser #990

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions tests/Scoper/Spec/SpecParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -81,15 +82,19 @@ class SpecParser extends TestCase
/**
* @throws UnparsableFile
*/
public static function parseSpecFile(string $sourceDir, SplFileInfo $file): iterable
{
public static function parseSpecFile(
string $sourceDir,
SplFileInfo $file,
): iterable {
try {
$fixtures = include $file;
$specs = include $file;

self::checkSpecFileSchema($specs);

$meta = $fixtures['meta'];
unset($fixtures['meta']);
$meta = $specs['meta'];
unset($specs['meta']);

foreach ($fixtures as $fixtureTitle => $fixtureSet) {
foreach ($specs as $fixtureTitle => $fixtureSet) {
yield from self::parseSpec(
basename($sourceDir).'/'.$file->getRelativePathname(),
$meta,
Expand All @@ -102,6 +107,21 @@ public static function parseSpecFile(string $sourceDir, SplFileInfo $file): iter
}
}

/**
* @phpstan-assert array{'meta': array, array-key: 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::assertTrue(is_string($spec) || is_array($spec));
}
}

private static function parseSpec(
string $file,
array $meta,
Expand Down
Loading