12
12
use PHPUnit \Framework \TestListenerDefaultImplementation ;
13
13
use PHPUnit \Framework \AssertionFailedError ;
14
14
15
- require_once __DIR__ . '/CallbackTestListener.php ' ;
16
-
17
15
class ParserGrammarTest extends TestCase {
18
16
private $ expectedTokensFile ;
19
17
private $ expectedDiagnosticsFile ;
20
18
private $ tokens ;
21
19
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
- }
40
20
41
21
/**
42
22
* @dataProvider treeProvider
43
23
*/
44
24
public function testOutputTreeClassificationAndLength ($ testCaseFile , $ expectedTokensFile , $ expectedDiagnosticsFile ) {
45
- $ this ->expectedTokensFile = $ expectedTokensFile ;
46
- $ this ->expectedDiagnosticsFile = $ expectedDiagnosticsFile ;
47
-
48
25
$ 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
- }
58
26
59
27
$ parser = new \Microsoft \PhpParser \Parser ();
60
28
$ sourceFileNode = $ parser ->parseSourceFile ($ fileContents );
61
29
62
- $ expectedTokens = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedTokensFile ));
63
- $ expectedDiagnostics = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedDiagnosticsFile ));
64
-
65
30
$ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = true ;
66
31
$ tokens = str_replace ("\r\n" , "\n" , json_encode ($ sourceFileNode , JSON_PRETTY_PRINT ));
67
32
$ diagnostics = str_replace ("\r\n" , "\n" , json_encode (\Microsoft \PhpParser \DiagnosticsProvider::getDiagnostics ($ sourceFileNode ), JSON_PRETTY_PRINT ));
68
33
$ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = false ;
69
34
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
+ }
72
54
73
55
$ tokensOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedTokensFile " ;
74
56
$ diagnosticsOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedDiagnosticsFile " ;
@@ -81,11 +63,13 @@ public function testOutputTreeClassificationAndLength($testCaseFile, $expectedTo
81
63
const PHP74_FILE_PATTERN = __DIR__ . "/cases/parser74/* " ;
82
64
const PHP80_FILE_PATTERN = __DIR__ . "/cases/parser80/* " ;
83
65
const PHP81_FILE_PATTERN = __DIR__ . "/cases/parser81/* " ;
66
+ const PHP84_FILE_PATTERN = __DIR__ . "/cases/parser84/* " ;
84
67
85
68
const PATTERNS_FOR_MINIMUM_PHP_VERSION = [
86
69
[70400 , self ::PHP74_FILE_PATTERN ],
87
70
[80000 , self ::PHP80_FILE_PATTERN ],
88
71
[80100 , self ::PHP81_FILE_PATTERN ],
72
+ [80400 , self ::PHP84_FILE_PATTERN ],
89
73
];
90
74
91
75
public function treeProvider () {
@@ -100,12 +84,14 @@ public function treeProvider() {
100
84
$ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " , $ testCase . ".diag " ];
101
85
}
102
86
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 " ];
109
95
}
110
96
}
111
97
0 commit comments