Skip to content

Commit e76c65d

Browse files
authored
test: Introduce a SpecScenario object (#999)
1 parent 2deb977 commit e76c65d

File tree

4 files changed

+181
-110
lines changed

4 files changed

+181
-110
lines changed

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 21 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
use Humbug\PhpScoper\Container;
1919
use Humbug\PhpScoper\PhpParser\TraverserFactory;
2020
use Humbug\PhpScoper\Scoper\Spec\SpecFinder;
21-
use Humbug\PhpScoper\Scoper\Spec\SpecFormatter;
2221
use Humbug\PhpScoper\Scoper\Spec\SpecNormalizer;
2322
use Humbug\PhpScoper\Scoper\Spec\SpecParser;
23+
use Humbug\PhpScoper\Scoper\Spec\SpecScenario;
2424
use Humbug\PhpScoper\Scoper\Spec\UnparsableSpec;
2525
use Humbug\PhpScoper\Symbol\EnrichedReflector;
2626
use Humbug\PhpScoper\Symbol\Reflector;
@@ -38,8 +38,6 @@
3838
use function implode;
3939
use function sprintf;
4040
use function str_starts_with;
41-
use function usort;
42-
use const PHP_VERSION_ID;
4341

4442
/**
4543
* @internal
@@ -59,88 +57,52 @@ public function test_it_uses_the_right_specs_directory(): void
5957
}
6058

6159
#[DataProvider('provideValidFiles')]
62-
public function test_can_scope_valid_files(
63-
string $file,
64-
string $spec,
65-
string $contents,
66-
string $prefix,
67-
SymbolsConfiguration $symbolsConfiguration,
68-
?string $expected,
69-
array $expectedRegisteredClasses,
70-
array $expectedRegisteredFunctions,
71-
?int $minPhpVersion,
72-
?int $maxPhpVersion
73-
): void {
74-
self::checkPHPVersionRequirements(
75-
$spec,
76-
$minPhpVersion,
77-
$maxPhpVersion,
78-
);
60+
public function test_can_scope_valid_files(SpecScenario $scenario): void
61+
{
62+
$scenario->checkPHPVersionRequirements();
7963

8064
$filePath = 'file.php';
8165
$symbolsRegistry = new SymbolsRegistry();
8266

8367
$scoper = self::createScoper(
84-
$prefix,
85-
$symbolsConfiguration,
68+
$scenario->prefix,
69+
$scenario->symbolsConfiguration,
8670
$symbolsRegistry,
8771
);
8872

8973
try {
9074
$actual = SpecNormalizer::trimTrailingSpaces(
91-
$scoper->scope($filePath, $contents),
75+
$scoper->scope($filePath, $scenario->inputCode),
9276
);
9377

94-
if (null === $expected) {
95-
self::fail('Expected exception to be thrown.');
96-
}
78+
$scenario->failIfExpectedFailure($this);
9779
} catch (UnexpectedValueException $exception) {
98-
if (null !== $expected) {
99-
throw $exception;
100-
}
101-
102-
self::assertTrue(true);
80+
$scenario->assertExpectedFailure($this, $exception);
10381

10482
return;
10583
} catch (PhpParserError $error) {
106-
self::handlePhpParserError(
107-
$spec,
108-
$contents,
109-
$error,
110-
);
84+
self::handlePhpParserError($scenario, $error);
11185
} catch (Throwable $throwable) {
112-
throw UnparsableSpec::create($spec, $throwable);
86+
throw UnparsableSpec::create($scenario->title, $throwable);
11387
}
11488

115-
$specMessage = SpecFormatter::createSpecMessage(
116-
$file,
117-
$spec,
118-
$contents,
119-
$symbolsConfiguration,
89+
$scenario->assertExpectedResult(
90+
$this,
12091
$symbolsRegistry,
121-
$expected,
12292
$actual,
123-
$expectedRegisteredClasses,
124-
$expectedRegisteredFunctions,
12593
);
126-
127-
self::assertSame($expected, $actual, $specMessage);
128-
129-
$actualRecordedExposedClasses = $symbolsRegistry->getRecordedClasses();
130-
131-
self::assertSameRecordedSymbols($expectedRegisteredClasses, $actualRecordedExposedClasses, $specMessage);
132-
133-
$actualRecordedExposedFunctions = $symbolsRegistry->getRecordedFunctions();
134-
135-
self::assertSameRecordedSymbols($expectedRegisteredFunctions, $actualRecordedExposedFunctions, $specMessage);
13694
}
13795

13896
public static function provideValidFiles(): iterable
13997
{
14098
[$sourceDir, $files] = SpecFinder::findSpecFiles();
14199

142100
foreach ($files as $file) {
143-
yield from SpecParser::parseSpecFile($sourceDir, $file);
101+
$scenarios = SpecParser::parseSpecFile($sourceDir, $file);
102+
103+
foreach ($scenarios as $scenario) {
104+
yield [$scenario];
105+
}
144106
}
145107
}
146108

@@ -176,30 +138,15 @@ private static function createScoper(
176138
);
177139
}
178140

179-
private static function checkPHPVersionRequirements(
180-
string $spec,
181-
?int $minPhpVersion,
182-
?int $maxPhpVersion,
183-
): void {
184-
if (null !== $minPhpVersion && $minPhpVersion > PHP_VERSION_ID) {
185-
self::markTestSkipped(sprintf('Min PHP version not matched for spec %s', $spec));
186-
}
187-
188-
if (null !== $maxPhpVersion && $maxPhpVersion <= PHP_VERSION_ID) {
189-
self::markTestSkipped(sprintf('Max PHP version not matched for spec %s', $spec));
190-
}
191-
}
192-
193141
private static function handlePhpParserError(
194-
string $spec,
195-
string $contents,
142+
SpecScenario $scenario,
196143
PhpParserError $error,
197144
): never {
198145
if (!str_starts_with($error->getMessage(), 'Syntax error,')) {
199-
throw UnparsableSpec::create($spec, $error);
146+
throw UnparsableSpec::create($scenario->title, $error);
200147
}
201148

202-
$lines = array_values(array_filter(explode("\n", $contents)));
149+
$lines = array_values(array_filter(explode("\n", $scenario->inputCode)));
203150

204151
$startLine = $error->getAttributes()['startLine'] - 1;
205152
$endLine = $error->getAttributes()['endLine'] + 1;
@@ -216,18 +163,4 @@ private static function handlePhpParserError(
216163
),
217164
);
218165
}
219-
220-
/**
221-
* @param string[][] $expected
222-
* @param string[][] $actual
223-
*/
224-
private static function assertSameRecordedSymbols(array $expected, array $actual, string $message): void
225-
{
226-
$sort = static fn (array $a, array $b) => $a[0] <=> $b[0];
227-
228-
usort($expected, $sort);
229-
usort($actual, $sort);
230-
231-
self::assertSame($expected, $actual, $message);
232-
}
233166
}

tests/Scoper/Spec/SpecParser.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class SpecParser extends TestCase
8484

8585
/**
8686
* @throws UnparsableFile
87+
*
88+
* @return iterable<SpecScenario>
8789
*/
8890
public static function parseSpecFile(
8991
string $sourceDir,
@@ -134,7 +136,7 @@ private static function parseSpec(
134136
array $meta,
135137
int|string $title,
136138
array|string $fixtureSet,
137-
): array {
139+
): SpecScenario {
138140
static $specMetaKeys;
139141
static $specKeys;
140142

@@ -190,10 +192,12 @@ private static function parseSpec(
190192
);
191193
}
192194

193-
return [
195+
return new SpecScenario(
196+
$fixtureSet['minPhpVersion'] ?? $meta['minPhpVersion'] ?? null,
197+
$fixtureSet['maxPhpVersion'] ?? $meta['maxPhpVersion'] ?? null,
194198
$file,
195199
$completeTitle,
196-
$payloadParts[0], // Input
200+
$payloadParts[0],
197201
$fixtureSet[ConfigurationKeys::PREFIX_KEYWORD] ?? $meta[ConfigurationKeys::PREFIX_KEYWORD],
198202
self::createSymbolsConfiguration(
199203
$file,
@@ -203,9 +207,7 @@ private static function parseSpec(
203207
'' === $payloadParts[1] ? null : $payloadParts[1], // Expected output; null means an exception is expected,
204208
$fixtureSet['expected-recorded-classes'] ?? $meta['expected-recorded-classes'],
205209
$fixtureSet['expected-recorded-functions'] ?? $meta['expected-recorded-functions'],
206-
$fixtureSet['minPhpVersion'] ?? $meta['minPhpVersion'] ?? null,
207-
$fixtureSet['maxPhpVersion'] ?? $meta['maxPhpVersion'] ?? null,
208-
];
210+
);
209211
}
210212

211213
private static function createSymbolsConfiguration(

tests/Scoper/Spec/SpecParserTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public static function specProvider(): iterable
6565
yield [
6666
self::FIXTURE_DIR.'/simple-spec-file.php',
6767
[
68-
[
68+
new SpecScenario(
69+
null,
70+
null,
6971
'Fixtures/simple-spec-file.php',
7072
'[Example of simple spec file] spec #0',
7173
$specCode,
@@ -78,10 +80,10 @@ public static function specProvider(): iterable
7880
$expectedCode,
7981
[],
8082
[],
83+
),
84+
new SpecScenario(
8185
null,
8286
null,
83-
],
84-
[
8587
'Fixtures/simple-spec-file.php',
8688
'[Example of simple spec file] A spec with a title',
8789
$specCode,
@@ -94,16 +96,16 @@ public static function specProvider(): iterable
9496
$expectedCode,
9597
[],
9698
[],
97-
null,
98-
null,
99-
],
99+
),
100100
],
101101
];
102102

103103
yield [
104104
self::FIXTURE_DIR.'/complete-spec-file.php',
105105
[
106-
[
106+
new SpecScenario(
107+
72_000,
108+
83_000,
107109
'Fixtures/complete-spec-file.php',
108110
'[Example of simple spec file] Spec with default meta values',
109111
$specCode,
@@ -124,10 +126,10 @@ public static function specProvider(): iterable
124126
$expectedCode,
125127
['Acme\RecordedClass', 'Humbug\Acme\RecordedClass'],
126128
['Acme\recorded_function', 'Humbug\Acme\recorded_function'],
129+
),
130+
new SpecScenario(
127131
72_000,
128132
83_000,
129-
],
130-
[
131133
'Fixtures/complete-spec-file.php',
132134
'[Example of simple spec file] Spec with the more verbose form',
133135
$specCode,
@@ -148,10 +150,10 @@ public static function specProvider(): iterable
148150
$expectedCode,
149151
['Acme\RecordedClass', 'Humbug\Acme\RecordedClass'],
150152
['Acme\recorded_function', 'Humbug\Acme\recorded_function'],
151-
72_000,
152-
83_000,
153-
],
154-
[
153+
),
154+
new SpecScenario(
155+
73_000,
156+
82_000,
155157
'Fixtures/complete-spec-file.php',
156158
'[Example of simple spec file] Spec with overridden meta values',
157159
$specCode,
@@ -172,9 +174,7 @@ public static function specProvider(): iterable
172174
$expectedCode,
173175
['AnotherRecordedClass'],
174176
['AnotherRecordedFunction'],
175-
73_000,
176-
82_000,
177-
],
177+
),
178178
],
179179
];
180180
}

0 commit comments

Comments
 (0)