|
10 | 10 |
|
11 | 11 | namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP;
|
12 | 12 |
|
| 13 | +use PHP_CodeSniffer\Files\DummyFile; |
| 14 | +use PHP_CodeSniffer\Ruleset; |
| 15 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
13 | 16 | use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
|
14 | 17 |
|
15 | 18 | /**
|
@@ -60,4 +63,108 @@ public function getWarningList()
|
60 | 63 | }//end getWarningList()
|
61 | 64 |
|
62 | 65 |
|
| 66 | + /** |
| 67 | + * Test the sniff checks syntax when file contents are passed via STDIN. |
| 68 | + * |
| 69 | + * Note: this test doesn't run on Windows as PHPCS currently doesn't support STDIN on this OS. |
| 70 | + * |
| 71 | + * @param string $content The content to test. |
| 72 | + * @param int $errorCount The expected number of errors. |
| 73 | + * @param array $expectedErrors The expected errors. |
| 74 | + * |
| 75 | + * @dataProvider dataStdIn |
| 76 | + * @requires OS ^(?!WIN).* |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testStdIn($content, $errorCount, $expectedErrors) |
| 81 | + { |
| 82 | + $config = new ConfigDouble(); |
| 83 | + $config->standards = ['Generic']; |
| 84 | + $config->sniffs = ['Generic.PHP.Syntax']; |
| 85 | + |
| 86 | + $ruleset = new Ruleset($config); |
| 87 | + |
| 88 | + $file = new DummyFile($content, $ruleset, $config); |
| 89 | + $file->process(); |
| 90 | + |
| 91 | + $this->assertSame( |
| 92 | + $errorCount, |
| 93 | + $file->getErrorCount(), |
| 94 | + 'Error count does not match expected value' |
| 95 | + ); |
| 96 | + $this->assertSame( |
| 97 | + 0, |
| 98 | + $file->getWarningCount(), |
| 99 | + 'Warning count does not match expected value' |
| 100 | + ); |
| 101 | + $this->assertSame( |
| 102 | + $expectedErrors, |
| 103 | + $file->getErrors(), |
| 104 | + 'Error list does not match expected errors' |
| 105 | + ); |
| 106 | + |
| 107 | + }//end testStdIn() |
| 108 | + |
| 109 | + |
| 110 | + /** |
| 111 | + * Data provider for testStdIn(). |
| 112 | + * |
| 113 | + * @return array[] |
| 114 | + */ |
| 115 | + public static function dataStdIn() |
| 116 | + { |
| 117 | + // The error message changed in PHP 8+. |
| 118 | + if (PHP_VERSION_ID >= 80000) { |
| 119 | + $errorMessage = 'PHP syntax error: syntax error, unexpected token ";", expecting "]"'; |
| 120 | + } else { |
| 121 | + $errorMessage = 'PHP syntax error: syntax error, unexpected \';\', expecting \']\''; |
| 122 | + } |
| 123 | + |
| 124 | + return [ |
| 125 | + 'No syntax errors' => [ |
| 126 | + '<?php $array = [1, 2, 3];', |
| 127 | + 0, |
| 128 | + [], |
| 129 | + ], |
| 130 | + 'One syntax error' => [ |
| 131 | + '<?php $array = [1, 2, 3; // Missing closing bracket.', |
| 132 | + 1, |
| 133 | + [ |
| 134 | + 1 => [ |
| 135 | + 1 => [ |
| 136 | + 0 => [ |
| 137 | + 'message' => $errorMessage, |
| 138 | + 'source' => 'Generic.PHP.Syntax.PHPSyntax', |
| 139 | + 'listener' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\SyntaxSniff', |
| 140 | + 'severity' => 5, |
| 141 | + 'fixable' => false, |
| 142 | + ], |
| 143 | + ], |
| 144 | + ], |
| 145 | + ], |
| 146 | + ], |
| 147 | + 'Single error reported even when there is more than one syntax error in the file' => [ |
| 148 | + '<?php $array = [1, 2, 3; // Missing closing bracket. |
| 149 | + $anotherArray = [4, 5, 6; // Another missing closing bracket.', |
| 150 | + 1, |
| 151 | + [ |
| 152 | + 1 => [ |
| 153 | + 1 => [ |
| 154 | + 0 => [ |
| 155 | + 'message' => $errorMessage, |
| 156 | + 'source' => 'Generic.PHP.Syntax.PHPSyntax', |
| 157 | + 'listener' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\SyntaxSniff', |
| 158 | + 'severity' => 5, |
| 159 | + 'fixable' => false, |
| 160 | + ], |
| 161 | + ], |
| 162 | + ], |
| 163 | + ], |
| 164 | + ], |
| 165 | + ]; |
| 166 | + |
| 167 | + }//end dataStdIn() |
| 168 | + |
| 169 | + |
63 | 170 | }//end class
|
0 commit comments