|
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 | | - private $expectedTokensFile; |
19 | | - private $expectedDiagnosticsFile; |
20 | | - private $tokens; |
21 | | - 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 | | - |
41 | 16 | /** |
42 | 17 | * @dataProvider treeProvider |
43 | 18 | */ |
44 | 19 | public function testOutputTreeClassificationAndLength($testCaseFile, $expectedTokensFile, $expectedDiagnosticsFile) { |
45 | | - $this->expectedTokensFile = $expectedTokensFile; |
46 | | - $this->expectedDiagnosticsFile = $expectedDiagnosticsFile; |
47 | | - |
48 | 20 | $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 | 21 |
|
59 | 22 | $parser = new \Microsoft\PhpParser\Parser(); |
60 | 23 | $sourceFileNode = $parser->parseSourceFile($fileContents); |
61 | 24 |
|
62 | | - $expectedTokens = str_replace("\r\n", "\n", file_get_contents($expectedTokensFile)); |
63 | | - $expectedDiagnostics = str_replace("\r\n", "\n", file_get_contents($expectedDiagnosticsFile)); |
64 | | - |
65 | 25 | $GLOBALS["SHORT_TOKEN_SERIALIZE"] = true; |
66 | 26 | $tokens = str_replace("\r\n", "\n", json_encode($sourceFileNode, JSON_PRETTY_PRINT)); |
67 | 27 | $diagnostics = str_replace("\r\n", "\n", json_encode(\Microsoft\PhpParser\DiagnosticsProvider::getDiagnostics($sourceFileNode), JSON_PRETTY_PRINT)); |
68 | 28 | $GLOBALS["SHORT_TOKEN_SERIALIZE"] = false; |
69 | 29 |
|
70 | | - $this->tokens = $tokens; |
71 | | - $this->diagnostics = $diagnostics; |
| 30 | + $skip = false; |
| 31 | + if (!file_exists($expectedTokensFile)) { |
| 32 | + file_put_contents($expectedTokensFile, $tokens); |
| 33 | + $skip = true; |
| 34 | + } else { |
| 35 | + $expectedTokens = trim(str_replace("\r\n", "\n", file_get_contents($expectedTokensFile))); |
| 36 | + } |
72 | 37 |
|
73 | | - $tokensOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedTokensFile"; |
74 | | - $diagnosticsOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedDiagnosticsFile"; |
| 38 | + |
| 39 | + if (!file_exists($expectedDiagnosticsFile)) { |
| 40 | + file_put_contents($expectedDiagnosticsFile, $diagnostics); |
| 41 | + $skip = true; |
| 42 | + } else { |
| 43 | + $expectedDiagnostics = trim(str_replace("\r\n", "\n", file_get_contents($expectedDiagnosticsFile))); |
| 44 | + } |
| 45 | + |
| 46 | + if ($skip) { |
| 47 | + self::markTestSkipped('Snapshot generated'); |
| 48 | + } |
| 49 | + |
| 50 | + $tokensOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedTokensFile (deleted expected file to regenerate)"; |
| 51 | + $diagnosticsOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedDiagnosticsFile (delete expected file to regenerate)"; |
75 | 52 |
|
76 | 53 | $this->assertEquals($expectedTokens, $tokens, $tokensOutputStr); |
77 | 54 | $this->assertEquals($expectedDiagnostics, $diagnostics, $diagnosticsOutputStr); |
@@ -104,12 +81,14 @@ public function treeProvider() { |
104 | 81 | $testProviderArray[basename($testCase)] = [$testCase, $testCase . ".tree", $testCase . ".diag"]; |
105 | 82 | } |
106 | 83 |
|
107 | | - foreach (self::PATTERNS_FOR_MINIMUM_PHP_VERSION as list($minVersionId, $filePattern)) { |
108 | | - if (PHP_VERSION_ID >= $minVersionId) { |
109 | | - $testCases = glob($filePattern . ".php"); |
110 | | - foreach ($testCases as $testCase) { |
111 | | - $testProviderArray[basename($testCase)] = [$testCase, $testCase . ".tree", $testCase . ".diag"]; |
112 | | - } |
| 84 | + foreach (self::PATTERNS_FOR_MINIMUM_PHP_VERSION as [$minVersionId, $filePattern]) { |
| 85 | + if (PHP_VERSION_ID < $minVersionId) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $testCases = glob($filePattern . ".php"); |
| 90 | + foreach ($testCases as $testCase) { |
| 91 | + $testProviderArray[basename($testCase)] = [$testCase, $testCase . ".tree", $testCase . ".diag"]; |
113 | 92 | } |
114 | 93 | } |
115 | 94 |
|
|
0 commit comments