1818use Humbug \PhpScoper \Container ;
1919use Humbug \PhpScoper \PhpParser \TraverserFactory ;
2020use Humbug \PhpScoper \Scoper \Spec \SpecFinder ;
21- use Humbug \PhpScoper \Scoper \Spec \SpecFormatter ;
2221use Humbug \PhpScoper \Scoper \Spec \SpecNormalizer ;
2322use Humbug \PhpScoper \Scoper \Spec \SpecParser ;
23+ use Humbug \PhpScoper \Scoper \Spec \SpecScenario ;
2424use Humbug \PhpScoper \Scoper \Spec \UnparsableSpec ;
2525use Humbug \PhpScoper \Symbol \EnrichedReflector ;
2626use Humbug \PhpScoper \Symbol \Reflector ;
3838use function implode ;
3939use function sprintf ;
4040use 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}
0 commit comments