1212use PHPUnit \Framework \TestListenerDefaultImplementation ;
1313use PHPUnit \Framework \AssertionFailedError ;
1414
15- require_once __DIR__ . '/CallbackTestListener.php ' ;
16-
1715class ParserGrammarTest extends TestCase {
1816 private $ expectedTokensFile ;
1917 private $ expectedDiagnosticsFile ;
2018 private $ tokens ;
2119 private $ diagnostics ;
22- public function run (TestResult $ result = null ) : TestResult {
23- if (!isset ($ GLOBALS ["GIT_CHECKOUT_PARSER " ])) {
24- $ GLOBALS ["GIT_CHECKOUT_PARSER " ] = true ;
25- exec ("git -C " . dirname (self ::FILE_PATTERN ) . " checkout *.php.tree *.php.diag " );
26- }
27-
28- $ result ->addListener (new CallbackTestListener (function (Test $ test ) {
29- if (isset ($ test ->expectedTokensFile ) && isset ($ test ->tokens )) {
30- file_put_contents ($ test ->expectedTokensFile , str_replace ("\r\n" , "\n" , $ test ->tokens ));
31- }
32- if (isset ($ test ->expectedDiagnosticsFile ) && isset ($ test ->diagnostics )) {
33- file_put_contents ($ test ->expectedDiagnosticsFile , str_replace ("\r\n" , "\n" , $ test ->diagnostics ));
34- }
35- }));
36-
37- $ result = parent ::run ($ result );
38- return $ result ;
39- }
4020
4121 /**
4222 * @dataProvider treeProvider
4323 */
4424 public function testOutputTreeClassificationAndLength ($ testCaseFile , $ expectedTokensFile , $ expectedDiagnosticsFile ) {
45- $ this ->expectedTokensFile = $ expectedTokensFile ;
46- $ this ->expectedDiagnosticsFile = $ expectedDiagnosticsFile ;
47-
4825 $ fileContents = file_get_contents ($ testCaseFile );
49- if (!file_exists ($ expectedTokensFile )) {
50- file_put_contents ($ expectedTokensFile , $ fileContents );
51- exec ("git add " . $ expectedTokensFile );
52- }
53-
54- if (!file_exists ($ expectedDiagnosticsFile )) {
55- file_put_contents ($ expectedDiagnosticsFile , $ fileContents );
56- exec ("git add " . $ expectedDiagnosticsFile );
57- }
5826
5927 $ parser = new \Microsoft \PhpParser \Parser ();
6028 $ sourceFileNode = $ parser ->parseSourceFile ($ fileContents );
6129
62- $ expectedTokens = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedTokensFile ));
63- $ expectedDiagnostics = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedDiagnosticsFile ));
64-
6530 $ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = true ;
6631 $ tokens = str_replace ("\r\n" , "\n" , json_encode ($ sourceFileNode , JSON_PRETTY_PRINT ));
6732 $ diagnostics = str_replace ("\r\n" , "\n" , json_encode (\Microsoft \PhpParser \DiagnosticsProvider::getDiagnostics ($ sourceFileNode ), JSON_PRETTY_PRINT ));
6833 $ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = false ;
6934
70- $ this ->tokens = $ tokens ;
71- $ this ->diagnostics = $ diagnostics ;
35+ $ skip = false ;
36+ if (!file_exists ($ expectedTokensFile )) {
37+ file_put_contents ($ expectedTokensFile , $ tokens );
38+ $ skip = true ;
39+ } else {
40+ $ expectedTokens = trim (str_replace ("\r\n" , "\n" , file_get_contents ($ expectedTokensFile )));
41+ }
42+
43+
44+ if (!file_exists ($ expectedDiagnosticsFile )) {
45+ file_put_contents ($ expectedDiagnosticsFile , $ diagnostics );
46+ $ skip = true ;
47+ } else {
48+ $ expectedDiagnostics = trim (str_replace ("\r\n" , "\n" , file_get_contents ($ expectedDiagnosticsFile )));
49+ }
50+
51+ if ($ skip ) {
52+ self ::markTestSkipped ('Snapshot generated ' );
53+ }
7254
7355 $ tokensOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedTokensFile " ;
7456 $ diagnosticsOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedDiagnosticsFile " ;
@@ -81,11 +63,13 @@ public function testOutputTreeClassificationAndLength($testCaseFile, $expectedTo
8163 const PHP74_FILE_PATTERN = __DIR__ . "/cases/parser74/* " ;
8264 const PHP80_FILE_PATTERN = __DIR__ . "/cases/parser80/* " ;
8365 const PHP81_FILE_PATTERN = __DIR__ . "/cases/parser81/* " ;
66+ const PHP84_FILE_PATTERN = __DIR__ . "/cases/parser84/* " ;
8467
8568 const PATTERNS_FOR_MINIMUM_PHP_VERSION = [
8669 [70400 , self ::PHP74_FILE_PATTERN ],
8770 [80000 , self ::PHP80_FILE_PATTERN ],
8871 [80100 , self ::PHP81_FILE_PATTERN ],
72+ [80400 , self ::PHP84_FILE_PATTERN ],
8973 ];
9074
9175 public function treeProvider () {
@@ -100,12 +84,14 @@ public function treeProvider() {
10084 $ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " , $ testCase . ".diag " ];
10185 }
10286
103- foreach (self ::PATTERNS_FOR_MINIMUM_PHP_VERSION as list ($ minVersionId , $ filePattern )) {
104- if (PHP_VERSION_ID >= $ minVersionId ) {
105- $ testCases = glob ($ filePattern . ".php " );
106- foreach ($ testCases as $ testCase ) {
107- $ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " , $ testCase . ".diag " ];
108- }
87+ foreach (self ::PATTERNS_FOR_MINIMUM_PHP_VERSION as [$ minVersionId , $ filePattern ]) {
88+ if (PHP_VERSION_ID < $ minVersionId ) {
89+ continue ;
90+ }
91+
92+ $ testCases = glob ($ filePattern . ".php " );
93+ foreach ($ testCases as $ testCase ) {
94+ $ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " , $ testCase . ".diag " ];
10995 }
11096 }
11197
0 commit comments