@@ -16,20 +16,20 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Runs the tests and collects their result in a TestResult. |
|
21 | - * |
|
22 | - * @param \PHPUnit\Framework\TestResult $result A test result. |
|
23 | - * |
|
24 | - * @return \PHPUnit\Framework\TestResult |
|
25 | - */ |
|
26 | - public function run(TestResult $result=null) |
|
27 | - { |
|
28 | - $result = parent::run($result); |
|
29 | - printPHPCodeSnifferTestOutput(); |
|
30 | - return $result; |
|
31 | - |
|
32 | - }//end run() |
|
19 | + /** |
|
20 | + * Runs the tests and collects their result in a TestResult. |
|
21 | + * |
|
22 | + * @param \PHPUnit\Framework\TestResult $result A test result. |
|
23 | + * |
|
24 | + * @return \PHPUnit\Framework\TestResult |
|
25 | + */ |
|
26 | + public function run(TestResult $result=null) |
|
27 | + { |
|
28 | + $result = parent::run($result); |
|
29 | + printPHPCodeSnifferTestOutput(); |
|
30 | + return $result; |
|
31 | + |
|
32 | + }//end run() |
|
33 | 33 | |
34 | 34 | |
35 | 35 | }//end class |
@@ -10,11 +10,11 @@ discard block |
||
10 | 10 | namespace PHP_CodeSniffer\Tests; |
11 | 11 | |
12 | 12 | if (is_file(__DIR__.'/../autoload.php') === true) { |
13 | - include_once 'Core/AllTests.php'; |
|
14 | - include_once 'Standards/AllSniffs.php'; |
|
13 | + include_once 'Core/AllTests.php'; |
|
14 | + include_once 'Standards/AllSniffs.php'; |
|
15 | 15 | } else { |
16 | - include_once 'CodeSniffer/Core/AllTests.php'; |
|
17 | - include_once 'CodeSniffer/Standards/AllSniffs.php'; |
|
16 | + include_once 'CodeSniffer/Core/AllTests.php'; |
|
17 | + include_once 'CodeSniffer/Standards/AllSniffs.php'; |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | // PHPUnit 7 made the TestSuite run() method incompatible with |
@@ -22,42 +22,42 @@ discard block |
||
22 | 22 | // two different suite objects. |
23 | 23 | $phpunit7 = false; |
24 | 24 | if (class_exists('\PHPUnit\Runner\Version') === true) { |
25 | - $version = \PHPUnit\Runner\Version::id(); |
|
26 | - if ($version[0] === '7') { |
|
27 | - $phpunit7 = true; |
|
28 | - } |
|
25 | + $version = \PHPUnit\Runner\Version::id(); |
|
26 | + if ($version[0] === '7') { |
|
27 | + $phpunit7 = true; |
|
28 | + } |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | if ($phpunit7 === true) { |
32 | - include_once 'TestSuite7.php'; |
|
32 | + include_once 'TestSuite7.php'; |
|
33 | 33 | } else { |
34 | - include_once 'TestSuite.php'; |
|
34 | + include_once 'TestSuite.php'; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | class PHP_CodeSniffer_AllTests |
38 | 38 | { |
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * Add all PHP_CodeSniffer test suites into a single test suite. |
|
43 | - * |
|
44 | - * @return \PHPUnit\Framework\TestSuite |
|
45 | - */ |
|
46 | - public static function suite() |
|
47 | - { |
|
48 | - $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = []; |
|
49 | - $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'] = []; |
|
41 | + /** |
|
42 | + * Add all PHP_CodeSniffer test suites into a single test suite. |
|
43 | + * |
|
44 | + * @return \PHPUnit\Framework\TestSuite |
|
45 | + */ |
|
46 | + public static function suite() |
|
47 | + { |
|
48 | + $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = []; |
|
49 | + $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'] = []; |
|
50 | 50 | |
51 | - // Use a special PHP_CodeSniffer test suite so that we can |
|
52 | - // unset our autoload function after the run. |
|
53 | - $suite = new TestSuite('PHP CodeSniffer'); |
|
51 | + // Use a special PHP_CodeSniffer test suite so that we can |
|
52 | + // unset our autoload function after the run. |
|
53 | + $suite = new TestSuite('PHP CodeSniffer'); |
|
54 | 54 | |
55 | - $suite->addTest(Core\AllTests::suite()); |
|
56 | - $suite->addTest(Standards\AllSniffs::suite()); |
|
55 | + $suite->addTest(Core\AllTests::suite()); |
|
56 | + $suite->addTest(Standards\AllSniffs::suite()); |
|
57 | 57 | |
58 | - return $suite; |
|
58 | + return $suite; |
|
59 | 59 | |
60 | - }//end suite() |
|
60 | + }//end suite() |
|
61 | 61 | |
62 | 62 | |
63 | 63 | }//end class |
@@ -23,428 +23,428 @@ |
||
23 | 23 | abstract class AbstractSniffUnitTest extends TestCase |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * Enable or disable the backup and restoration of the $GLOBALS array. |
|
28 | - * Overwrite this attribute in a child class of TestCase. |
|
29 | - * Setting this attribute in setUp() has no effect! |
|
30 | - * |
|
31 | - * @var boolean |
|
32 | - */ |
|
33 | - protected $backupGlobals = false; |
|
34 | - |
|
35 | - /** |
|
36 | - * The path to the standard's main directory. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - public $standardsDir = null; |
|
41 | - |
|
42 | - /** |
|
43 | - * The path to the standard's test directory. |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - public $testsDir = null; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Sets up this unit test. |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - protected function setUp() |
|
56 | - { |
|
57 | - $class = get_class($this); |
|
58 | - $this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class]; |
|
59 | - $this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class]; |
|
60 | - |
|
61 | - }//end setUp() |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Get a list of all test files to check. |
|
66 | - * |
|
67 | - * These will have the same base as the sniff name but different extensions. |
|
68 | - * We ignore the .php file as it is the class. |
|
69 | - * |
|
70 | - * @param string $testFileBase The base path that the unit tests files will have. |
|
71 | - * |
|
72 | - * @return string[] |
|
73 | - */ |
|
74 | - protected function getTestFiles($testFileBase) |
|
75 | - { |
|
76 | - $testFiles = []; |
|
77 | - |
|
78 | - $dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR)); |
|
79 | - $di = new \DirectoryIterator($dir); |
|
80 | - |
|
81 | - foreach ($di as $file) { |
|
82 | - $path = $file->getPathname(); |
|
83 | - if (substr($path, 0, strlen($testFileBase)) === $testFileBase) { |
|
84 | - if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') { |
|
85 | - $testFiles[] = $path; |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - // Put them in order. |
|
91 | - sort($testFiles); |
|
92 | - |
|
93 | - return $testFiles; |
|
94 | - |
|
95 | - }//end getTestFiles() |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Should this test be skipped for some reason. |
|
100 | - * |
|
101 | - * @return boolean |
|
102 | - */ |
|
103 | - protected function shouldSkipTest() |
|
104 | - { |
|
105 | - return false; |
|
106 | - |
|
107 | - }//end shouldSkipTest() |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Tests the extending classes Sniff class. |
|
112 | - * |
|
113 | - * @return void |
|
114 | - * @throws \PHPUnit\Framework\Exception |
|
115 | - */ |
|
116 | - final public function testSniff() |
|
117 | - { |
|
118 | - // Skip this test if we can't run in this environment. |
|
119 | - if ($this->shouldSkipTest() === true) { |
|
120 | - $this->markTestSkipped(); |
|
121 | - } |
|
122 | - |
|
123 | - $sniffCode = Common::getSniffCode(get_class($this)); |
|
124 | - list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode); |
|
125 | - |
|
126 | - $testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.'; |
|
127 | - |
|
128 | - // Get a list of all test files to check. |
|
129 | - $testFiles = $this->getTestFiles($testFileBase); |
|
130 | - $GLOBALS['PHP_CODESNIFFER_SNIFF_CASE_FILES'][] = $testFiles; |
|
131 | - |
|
132 | - if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG']) === true) { |
|
133 | - $config = $GLOBALS['PHP_CODESNIFFER_CONFIG']; |
|
134 | - } else { |
|
135 | - $config = new Config(); |
|
136 | - $config->cache = false; |
|
137 | - $GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config; |
|
138 | - } |
|
139 | - |
|
140 | - $config->standards = [$standardName]; |
|
141 | - $config->sniffs = [$sniffCode]; |
|
142 | - $config->ignored = []; |
|
143 | - |
|
144 | - if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS']) === false) { |
|
145 | - $GLOBALS['PHP_CODESNIFFER_RULESETS'] = []; |
|
146 | - } |
|
147 | - |
|
148 | - if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName]) === false) { |
|
149 | - $ruleset = new Ruleset($config); |
|
150 | - $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName] = $ruleset; |
|
151 | - } |
|
152 | - |
|
153 | - $ruleset = $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName]; |
|
154 | - |
|
155 | - $sniffFile = $this->standardsDir.DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'Sniff.php'; |
|
156 | - |
|
157 | - $sniffClassName = substr(get_class($this), 0, -8).'Sniff'; |
|
158 | - $sniffClassName = str_replace('\Tests\\', '\Sniffs\\', $sniffClassName); |
|
159 | - $sniffClassName = Common::cleanSniffClass($sniffClassName); |
|
160 | - |
|
161 | - $restrictions = [strtolower($sniffClassName) => true]; |
|
162 | - $ruleset->registerSniffs([$sniffFile], $restrictions, []); |
|
163 | - $ruleset->populateTokenListeners(); |
|
164 | - |
|
165 | - $failureMessages = []; |
|
166 | - foreach ($testFiles as $testFile) { |
|
167 | - $filename = basename($testFile); |
|
168 | - $oldConfig = $config->getSettings(); |
|
169 | - |
|
170 | - try { |
|
171 | - $this->setCliValues($filename, $config); |
|
172 | - $phpcsFile = new LocalFile($testFile, $ruleset, $config); |
|
173 | - $phpcsFile->process(); |
|
174 | - } catch (RuntimeException $e) { |
|
175 | - $this->fail('An unexpected exception has been caught: '.$e->getMessage()); |
|
176 | - } |
|
177 | - |
|
178 | - $failures = $this->generateFailureMessages($phpcsFile); |
|
179 | - $failureMessages = array_merge($failureMessages, $failures); |
|
180 | - |
|
181 | - if ($phpcsFile->getFixableCount() > 0) { |
|
182 | - // Attempt to fix the errors. |
|
183 | - $phpcsFile->fixer->fixFile(); |
|
184 | - $fixable = $phpcsFile->getFixableCount(); |
|
185 | - if ($fixable > 0) { |
|
186 | - $failureMessages[] = "Failed to fix $fixable fixable violations in $filename"; |
|
187 | - } |
|
188 | - |
|
189 | - // Check for a .fixed file to check for accuracy of fixes. |
|
190 | - $fixedFile = $testFile.'.fixed'; |
|
191 | - if (file_exists($fixedFile) === true) { |
|
192 | - $diff = $phpcsFile->fixer->generateDiff($fixedFile); |
|
193 | - if (trim($diff) !== '') { |
|
194 | - $filename = basename($testFile); |
|
195 | - $fixedFilename = basename($fixedFile); |
|
196 | - $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff"; |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - // Restore the config. |
|
202 | - $config->setSettings($oldConfig); |
|
203 | - }//end foreach |
|
204 | - |
|
205 | - if (empty($failureMessages) === false) { |
|
206 | - $this->fail(implode(PHP_EOL, $failureMessages)); |
|
207 | - } |
|
208 | - |
|
209 | - }//end testSniff() |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Generate a list of test failures for a given sniffed file. |
|
214 | - * |
|
215 | - * @param \PHP_CodeSniffer\Files\LocalFile $file The file being tested. |
|
216 | - * |
|
217 | - * @return array |
|
218 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
219 | - */ |
|
220 | - public function generateFailureMessages(LocalFile $file) |
|
221 | - { |
|
222 | - $testFile = $file->getFilename(); |
|
223 | - |
|
224 | - $foundErrors = $file->getErrors(); |
|
225 | - $foundWarnings = $file->getWarnings(); |
|
226 | - $expectedErrors = $this->getErrorList(basename($testFile)); |
|
227 | - $expectedWarnings = $this->getWarningList(basename($testFile)); |
|
228 | - |
|
229 | - if (is_array($expectedErrors) === false) { |
|
230 | - throw new RuntimeException('getErrorList() must return an array'); |
|
231 | - } |
|
232 | - |
|
233 | - if (is_array($expectedWarnings) === false) { |
|
234 | - throw new RuntimeException('getWarningList() must return an array'); |
|
235 | - } |
|
236 | - |
|
237 | - /* |
|
26 | + /** |
|
27 | + * Enable or disable the backup and restoration of the $GLOBALS array. |
|
28 | + * Overwrite this attribute in a child class of TestCase. |
|
29 | + * Setting this attribute in setUp() has no effect! |
|
30 | + * |
|
31 | + * @var boolean |
|
32 | + */ |
|
33 | + protected $backupGlobals = false; |
|
34 | + |
|
35 | + /** |
|
36 | + * The path to the standard's main directory. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + public $standardsDir = null; |
|
41 | + |
|
42 | + /** |
|
43 | + * The path to the standard's test directory. |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + public $testsDir = null; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Sets up this unit test. |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + protected function setUp() |
|
56 | + { |
|
57 | + $class = get_class($this); |
|
58 | + $this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class]; |
|
59 | + $this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class]; |
|
60 | + |
|
61 | + }//end setUp() |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Get a list of all test files to check. |
|
66 | + * |
|
67 | + * These will have the same base as the sniff name but different extensions. |
|
68 | + * We ignore the .php file as it is the class. |
|
69 | + * |
|
70 | + * @param string $testFileBase The base path that the unit tests files will have. |
|
71 | + * |
|
72 | + * @return string[] |
|
73 | + */ |
|
74 | + protected function getTestFiles($testFileBase) |
|
75 | + { |
|
76 | + $testFiles = []; |
|
77 | + |
|
78 | + $dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR)); |
|
79 | + $di = new \DirectoryIterator($dir); |
|
80 | + |
|
81 | + foreach ($di as $file) { |
|
82 | + $path = $file->getPathname(); |
|
83 | + if (substr($path, 0, strlen($testFileBase)) === $testFileBase) { |
|
84 | + if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') { |
|
85 | + $testFiles[] = $path; |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + // Put them in order. |
|
91 | + sort($testFiles); |
|
92 | + |
|
93 | + return $testFiles; |
|
94 | + |
|
95 | + }//end getTestFiles() |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Should this test be skipped for some reason. |
|
100 | + * |
|
101 | + * @return boolean |
|
102 | + */ |
|
103 | + protected function shouldSkipTest() |
|
104 | + { |
|
105 | + return false; |
|
106 | + |
|
107 | + }//end shouldSkipTest() |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Tests the extending classes Sniff class. |
|
112 | + * |
|
113 | + * @return void |
|
114 | + * @throws \PHPUnit\Framework\Exception |
|
115 | + */ |
|
116 | + final public function testSniff() |
|
117 | + { |
|
118 | + // Skip this test if we can't run in this environment. |
|
119 | + if ($this->shouldSkipTest() === true) { |
|
120 | + $this->markTestSkipped(); |
|
121 | + } |
|
122 | + |
|
123 | + $sniffCode = Common::getSniffCode(get_class($this)); |
|
124 | + list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode); |
|
125 | + |
|
126 | + $testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.'; |
|
127 | + |
|
128 | + // Get a list of all test files to check. |
|
129 | + $testFiles = $this->getTestFiles($testFileBase); |
|
130 | + $GLOBALS['PHP_CODESNIFFER_SNIFF_CASE_FILES'][] = $testFiles; |
|
131 | + |
|
132 | + if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG']) === true) { |
|
133 | + $config = $GLOBALS['PHP_CODESNIFFER_CONFIG']; |
|
134 | + } else { |
|
135 | + $config = new Config(); |
|
136 | + $config->cache = false; |
|
137 | + $GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config; |
|
138 | + } |
|
139 | + |
|
140 | + $config->standards = [$standardName]; |
|
141 | + $config->sniffs = [$sniffCode]; |
|
142 | + $config->ignored = []; |
|
143 | + |
|
144 | + if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS']) === false) { |
|
145 | + $GLOBALS['PHP_CODESNIFFER_RULESETS'] = []; |
|
146 | + } |
|
147 | + |
|
148 | + if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName]) === false) { |
|
149 | + $ruleset = new Ruleset($config); |
|
150 | + $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName] = $ruleset; |
|
151 | + } |
|
152 | + |
|
153 | + $ruleset = $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName]; |
|
154 | + |
|
155 | + $sniffFile = $this->standardsDir.DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'Sniff.php'; |
|
156 | + |
|
157 | + $sniffClassName = substr(get_class($this), 0, -8).'Sniff'; |
|
158 | + $sniffClassName = str_replace('\Tests\\', '\Sniffs\\', $sniffClassName); |
|
159 | + $sniffClassName = Common::cleanSniffClass($sniffClassName); |
|
160 | + |
|
161 | + $restrictions = [strtolower($sniffClassName) => true]; |
|
162 | + $ruleset->registerSniffs([$sniffFile], $restrictions, []); |
|
163 | + $ruleset->populateTokenListeners(); |
|
164 | + |
|
165 | + $failureMessages = []; |
|
166 | + foreach ($testFiles as $testFile) { |
|
167 | + $filename = basename($testFile); |
|
168 | + $oldConfig = $config->getSettings(); |
|
169 | + |
|
170 | + try { |
|
171 | + $this->setCliValues($filename, $config); |
|
172 | + $phpcsFile = new LocalFile($testFile, $ruleset, $config); |
|
173 | + $phpcsFile->process(); |
|
174 | + } catch (RuntimeException $e) { |
|
175 | + $this->fail('An unexpected exception has been caught: '.$e->getMessage()); |
|
176 | + } |
|
177 | + |
|
178 | + $failures = $this->generateFailureMessages($phpcsFile); |
|
179 | + $failureMessages = array_merge($failureMessages, $failures); |
|
180 | + |
|
181 | + if ($phpcsFile->getFixableCount() > 0) { |
|
182 | + // Attempt to fix the errors. |
|
183 | + $phpcsFile->fixer->fixFile(); |
|
184 | + $fixable = $phpcsFile->getFixableCount(); |
|
185 | + if ($fixable > 0) { |
|
186 | + $failureMessages[] = "Failed to fix $fixable fixable violations in $filename"; |
|
187 | + } |
|
188 | + |
|
189 | + // Check for a .fixed file to check for accuracy of fixes. |
|
190 | + $fixedFile = $testFile.'.fixed'; |
|
191 | + if (file_exists($fixedFile) === true) { |
|
192 | + $diff = $phpcsFile->fixer->generateDiff($fixedFile); |
|
193 | + if (trim($diff) !== '') { |
|
194 | + $filename = basename($testFile); |
|
195 | + $fixedFilename = basename($fixedFile); |
|
196 | + $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff"; |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + // Restore the config. |
|
202 | + $config->setSettings($oldConfig); |
|
203 | + }//end foreach |
|
204 | + |
|
205 | + if (empty($failureMessages) === false) { |
|
206 | + $this->fail(implode(PHP_EOL, $failureMessages)); |
|
207 | + } |
|
208 | + |
|
209 | + }//end testSniff() |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Generate a list of test failures for a given sniffed file. |
|
214 | + * |
|
215 | + * @param \PHP_CodeSniffer\Files\LocalFile $file The file being tested. |
|
216 | + * |
|
217 | + * @return array |
|
218 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
219 | + */ |
|
220 | + public function generateFailureMessages(LocalFile $file) |
|
221 | + { |
|
222 | + $testFile = $file->getFilename(); |
|
223 | + |
|
224 | + $foundErrors = $file->getErrors(); |
|
225 | + $foundWarnings = $file->getWarnings(); |
|
226 | + $expectedErrors = $this->getErrorList(basename($testFile)); |
|
227 | + $expectedWarnings = $this->getWarningList(basename($testFile)); |
|
228 | + |
|
229 | + if (is_array($expectedErrors) === false) { |
|
230 | + throw new RuntimeException('getErrorList() must return an array'); |
|
231 | + } |
|
232 | + |
|
233 | + if (is_array($expectedWarnings) === false) { |
|
234 | + throw new RuntimeException('getWarningList() must return an array'); |
|
235 | + } |
|
236 | + |
|
237 | + /* |
|
238 | 238 | We merge errors and warnings together to make it easier |
239 | 239 | to iterate over them and produce the errors string. In this way, |
240 | 240 | we can report on errors and warnings in the same line even though |
241 | 241 | it's not really structured to allow that. |
242 | 242 | */ |
243 | 243 | |
244 | - $allProblems = []; |
|
245 | - $failureMessages = []; |
|
246 | - |
|
247 | - foreach ($foundErrors as $line => $lineErrors) { |
|
248 | - foreach ($lineErrors as $column => $errors) { |
|
249 | - if (isset($allProblems[$line]) === false) { |
|
250 | - $allProblems[$line] = [ |
|
251 | - 'expected_errors' => 0, |
|
252 | - 'expected_warnings' => 0, |
|
253 | - 'found_errors' => [], |
|
254 | - 'found_warnings' => [], |
|
255 | - ]; |
|
256 | - } |
|
257 | - |
|
258 | - $foundErrorsTemp = []; |
|
259 | - foreach ($allProblems[$line]['found_errors'] as $foundError) { |
|
260 | - $foundErrorsTemp[] = $foundError; |
|
261 | - } |
|
262 | - |
|
263 | - $errorsTemp = []; |
|
264 | - foreach ($errors as $foundError) { |
|
265 | - $errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')'; |
|
266 | - |
|
267 | - $source = $foundError['source']; |
|
268 | - if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'], true) === false) { |
|
269 | - $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source; |
|
270 | - } |
|
271 | - |
|
272 | - if ($foundError['fixable'] === true |
|
273 | - && in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'], true) === false |
|
274 | - ) { |
|
275 | - $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source; |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp); |
|
280 | - }//end foreach |
|
281 | - |
|
282 | - if (isset($expectedErrors[$line]) === true) { |
|
283 | - $allProblems[$line]['expected_errors'] = $expectedErrors[$line]; |
|
284 | - } else { |
|
285 | - $allProblems[$line]['expected_errors'] = 0; |
|
286 | - } |
|
287 | - |
|
288 | - unset($expectedErrors[$line]); |
|
289 | - }//end foreach |
|
290 | - |
|
291 | - foreach ($expectedErrors as $line => $numErrors) { |
|
292 | - if (isset($allProblems[$line]) === false) { |
|
293 | - $allProblems[$line] = [ |
|
294 | - 'expected_errors' => 0, |
|
295 | - 'expected_warnings' => 0, |
|
296 | - 'found_errors' => [], |
|
297 | - 'found_warnings' => [], |
|
298 | - ]; |
|
299 | - } |
|
300 | - |
|
301 | - $allProblems[$line]['expected_errors'] = $numErrors; |
|
302 | - } |
|
303 | - |
|
304 | - foreach ($foundWarnings as $line => $lineWarnings) { |
|
305 | - foreach ($lineWarnings as $column => $warnings) { |
|
306 | - if (isset($allProblems[$line]) === false) { |
|
307 | - $allProblems[$line] = [ |
|
308 | - 'expected_errors' => 0, |
|
309 | - 'expected_warnings' => 0, |
|
310 | - 'found_errors' => [], |
|
311 | - 'found_warnings' => [], |
|
312 | - ]; |
|
313 | - } |
|
314 | - |
|
315 | - $foundWarningsTemp = []; |
|
316 | - foreach ($allProblems[$line]['found_warnings'] as $foundWarning) { |
|
317 | - $foundWarningsTemp[] = $foundWarning; |
|
318 | - } |
|
319 | - |
|
320 | - $warningsTemp = []; |
|
321 | - foreach ($warnings as $warning) { |
|
322 | - $warningsTemp[] = $warning['message'].' ('.$warning['source'].')'; |
|
323 | - } |
|
324 | - |
|
325 | - $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp); |
|
326 | - }//end foreach |
|
327 | - |
|
328 | - if (isset($expectedWarnings[$line]) === true) { |
|
329 | - $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line]; |
|
330 | - } else { |
|
331 | - $allProblems[$line]['expected_warnings'] = 0; |
|
332 | - } |
|
333 | - |
|
334 | - unset($expectedWarnings[$line]); |
|
335 | - }//end foreach |
|
336 | - |
|
337 | - foreach ($expectedWarnings as $line => $numWarnings) { |
|
338 | - if (isset($allProblems[$line]) === false) { |
|
339 | - $allProblems[$line] = [ |
|
340 | - 'expected_errors' => 0, |
|
341 | - 'expected_warnings' => 0, |
|
342 | - 'found_errors' => [], |
|
343 | - 'found_warnings' => [], |
|
344 | - ]; |
|
345 | - } |
|
346 | - |
|
347 | - $allProblems[$line]['expected_warnings'] = $numWarnings; |
|
348 | - } |
|
349 | - |
|
350 | - // Order the messages by line number. |
|
351 | - ksort($allProblems); |
|
352 | - |
|
353 | - foreach ($allProblems as $line => $problems) { |
|
354 | - $numErrors = count($problems['found_errors']); |
|
355 | - $numWarnings = count($problems['found_warnings']); |
|
356 | - $expectedErrors = $problems['expected_errors']; |
|
357 | - $expectedWarnings = $problems['expected_warnings']; |
|
358 | - |
|
359 | - $errors = ''; |
|
360 | - $foundString = ''; |
|
361 | - |
|
362 | - if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) { |
|
363 | - $lineMessage = "[LINE $line]"; |
|
364 | - $expectedMessage = 'Expected '; |
|
365 | - $foundMessage = 'in '.basename($testFile).' but found '; |
|
366 | - |
|
367 | - if ($expectedErrors !== $numErrors) { |
|
368 | - $expectedMessage .= "$expectedErrors error(s)"; |
|
369 | - $foundMessage .= "$numErrors error(s)"; |
|
370 | - if ($numErrors !== 0) { |
|
371 | - $foundString .= 'error(s)'; |
|
372 | - $errors .= implode(PHP_EOL.' -> ', $problems['found_errors']); |
|
373 | - } |
|
374 | - |
|
375 | - if ($expectedWarnings !== $numWarnings) { |
|
376 | - $expectedMessage .= ' and '; |
|
377 | - $foundMessage .= ' and '; |
|
378 | - if ($numWarnings !== 0) { |
|
379 | - if ($foundString !== '') { |
|
380 | - $foundString .= ' and '; |
|
381 | - } |
|
382 | - } |
|
383 | - } |
|
384 | - } |
|
385 | - |
|
386 | - if ($expectedWarnings !== $numWarnings) { |
|
387 | - $expectedMessage .= "$expectedWarnings warning(s)"; |
|
388 | - $foundMessage .= "$numWarnings warning(s)"; |
|
389 | - if ($numWarnings !== 0) { |
|
390 | - $foundString .= 'warning(s)'; |
|
391 | - if (empty($errors) === false) { |
|
392 | - $errors .= PHP_EOL.' -> '; |
|
393 | - } |
|
394 | - |
|
395 | - $errors .= implode(PHP_EOL.' -> ', $problems['found_warnings']); |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - $fullMessage = "$lineMessage $expectedMessage $foundMessage."; |
|
400 | - if ($errors !== '') { |
|
401 | - $fullMessage .= " The $foundString found were:".PHP_EOL." -> $errors"; |
|
402 | - } |
|
403 | - |
|
404 | - $failureMessages[] = $fullMessage; |
|
405 | - }//end if |
|
406 | - }//end foreach |
|
407 | - |
|
408 | - return $failureMessages; |
|
409 | - |
|
410 | - }//end generateFailureMessages() |
|
411 | - |
|
412 | - |
|
413 | - /** |
|
414 | - * Get a list of CLI values to set before the file is tested. |
|
415 | - * |
|
416 | - * @param string $filename The name of the file being tested. |
|
417 | - * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
418 | - * |
|
419 | - * @return void |
|
420 | - */ |
|
421 | - public function setCliValues($filename, $config) |
|
422 | - { |
|
423 | - return; |
|
424 | - |
|
425 | - }//end setCliValues() |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * Returns the lines where errors should occur. |
|
430 | - * |
|
431 | - * The key of the array should represent the line number and the value |
|
432 | - * should represent the number of errors that should occur on that line. |
|
433 | - * |
|
434 | - * @return array<int, int> |
|
435 | - */ |
|
436 | - abstract protected function getErrorList(); |
|
437 | - |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns the lines where warnings should occur. |
|
441 | - * |
|
442 | - * The key of the array should represent the line number and the value |
|
443 | - * should represent the number of warnings that should occur on that line. |
|
444 | - * |
|
445 | - * @return array<int, int> |
|
446 | - */ |
|
447 | - abstract protected function getWarningList(); |
|
244 | + $allProblems = []; |
|
245 | + $failureMessages = []; |
|
246 | + |
|
247 | + foreach ($foundErrors as $line => $lineErrors) { |
|
248 | + foreach ($lineErrors as $column => $errors) { |
|
249 | + if (isset($allProblems[$line]) === false) { |
|
250 | + $allProblems[$line] = [ |
|
251 | + 'expected_errors' => 0, |
|
252 | + 'expected_warnings' => 0, |
|
253 | + 'found_errors' => [], |
|
254 | + 'found_warnings' => [], |
|
255 | + ]; |
|
256 | + } |
|
257 | + |
|
258 | + $foundErrorsTemp = []; |
|
259 | + foreach ($allProblems[$line]['found_errors'] as $foundError) { |
|
260 | + $foundErrorsTemp[] = $foundError; |
|
261 | + } |
|
262 | + |
|
263 | + $errorsTemp = []; |
|
264 | + foreach ($errors as $foundError) { |
|
265 | + $errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')'; |
|
266 | + |
|
267 | + $source = $foundError['source']; |
|
268 | + if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'], true) === false) { |
|
269 | + $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source; |
|
270 | + } |
|
271 | + |
|
272 | + if ($foundError['fixable'] === true |
|
273 | + && in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'], true) === false |
|
274 | + ) { |
|
275 | + $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source; |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp); |
|
280 | + }//end foreach |
|
281 | + |
|
282 | + if (isset($expectedErrors[$line]) === true) { |
|
283 | + $allProblems[$line]['expected_errors'] = $expectedErrors[$line]; |
|
284 | + } else { |
|
285 | + $allProblems[$line]['expected_errors'] = 0; |
|
286 | + } |
|
287 | + |
|
288 | + unset($expectedErrors[$line]); |
|
289 | + }//end foreach |
|
290 | + |
|
291 | + foreach ($expectedErrors as $line => $numErrors) { |
|
292 | + if (isset($allProblems[$line]) === false) { |
|
293 | + $allProblems[$line] = [ |
|
294 | + 'expected_errors' => 0, |
|
295 | + 'expected_warnings' => 0, |
|
296 | + 'found_errors' => [], |
|
297 | + 'found_warnings' => [], |
|
298 | + ]; |
|
299 | + } |
|
300 | + |
|
301 | + $allProblems[$line]['expected_errors'] = $numErrors; |
|
302 | + } |
|
303 | + |
|
304 | + foreach ($foundWarnings as $line => $lineWarnings) { |
|
305 | + foreach ($lineWarnings as $column => $warnings) { |
|
306 | + if (isset($allProblems[$line]) === false) { |
|
307 | + $allProblems[$line] = [ |
|
308 | + 'expected_errors' => 0, |
|
309 | + 'expected_warnings' => 0, |
|
310 | + 'found_errors' => [], |
|
311 | + 'found_warnings' => [], |
|
312 | + ]; |
|
313 | + } |
|
314 | + |
|
315 | + $foundWarningsTemp = []; |
|
316 | + foreach ($allProblems[$line]['found_warnings'] as $foundWarning) { |
|
317 | + $foundWarningsTemp[] = $foundWarning; |
|
318 | + } |
|
319 | + |
|
320 | + $warningsTemp = []; |
|
321 | + foreach ($warnings as $warning) { |
|
322 | + $warningsTemp[] = $warning['message'].' ('.$warning['source'].')'; |
|
323 | + } |
|
324 | + |
|
325 | + $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp); |
|
326 | + }//end foreach |
|
327 | + |
|
328 | + if (isset($expectedWarnings[$line]) === true) { |
|
329 | + $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line]; |
|
330 | + } else { |
|
331 | + $allProblems[$line]['expected_warnings'] = 0; |
|
332 | + } |
|
333 | + |
|
334 | + unset($expectedWarnings[$line]); |
|
335 | + }//end foreach |
|
336 | + |
|
337 | + foreach ($expectedWarnings as $line => $numWarnings) { |
|
338 | + if (isset($allProblems[$line]) === false) { |
|
339 | + $allProblems[$line] = [ |
|
340 | + 'expected_errors' => 0, |
|
341 | + 'expected_warnings' => 0, |
|
342 | + 'found_errors' => [], |
|
343 | + 'found_warnings' => [], |
|
344 | + ]; |
|
345 | + } |
|
346 | + |
|
347 | + $allProblems[$line]['expected_warnings'] = $numWarnings; |
|
348 | + } |
|
349 | + |
|
350 | + // Order the messages by line number. |
|
351 | + ksort($allProblems); |
|
352 | + |
|
353 | + foreach ($allProblems as $line => $problems) { |
|
354 | + $numErrors = count($problems['found_errors']); |
|
355 | + $numWarnings = count($problems['found_warnings']); |
|
356 | + $expectedErrors = $problems['expected_errors']; |
|
357 | + $expectedWarnings = $problems['expected_warnings']; |
|
358 | + |
|
359 | + $errors = ''; |
|
360 | + $foundString = ''; |
|
361 | + |
|
362 | + if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) { |
|
363 | + $lineMessage = "[LINE $line]"; |
|
364 | + $expectedMessage = 'Expected '; |
|
365 | + $foundMessage = 'in '.basename($testFile).' but found '; |
|
366 | + |
|
367 | + if ($expectedErrors !== $numErrors) { |
|
368 | + $expectedMessage .= "$expectedErrors error(s)"; |
|
369 | + $foundMessage .= "$numErrors error(s)"; |
|
370 | + if ($numErrors !== 0) { |
|
371 | + $foundString .= 'error(s)'; |
|
372 | + $errors .= implode(PHP_EOL.' -> ', $problems['found_errors']); |
|
373 | + } |
|
374 | + |
|
375 | + if ($expectedWarnings !== $numWarnings) { |
|
376 | + $expectedMessage .= ' and '; |
|
377 | + $foundMessage .= ' and '; |
|
378 | + if ($numWarnings !== 0) { |
|
379 | + if ($foundString !== '') { |
|
380 | + $foundString .= ' and '; |
|
381 | + } |
|
382 | + } |
|
383 | + } |
|
384 | + } |
|
385 | + |
|
386 | + if ($expectedWarnings !== $numWarnings) { |
|
387 | + $expectedMessage .= "$expectedWarnings warning(s)"; |
|
388 | + $foundMessage .= "$numWarnings warning(s)"; |
|
389 | + if ($numWarnings !== 0) { |
|
390 | + $foundString .= 'warning(s)'; |
|
391 | + if (empty($errors) === false) { |
|
392 | + $errors .= PHP_EOL.' -> '; |
|
393 | + } |
|
394 | + |
|
395 | + $errors .= implode(PHP_EOL.' -> ', $problems['found_warnings']); |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + $fullMessage = "$lineMessage $expectedMessage $foundMessage."; |
|
400 | + if ($errors !== '') { |
|
401 | + $fullMessage .= " The $foundString found were:".PHP_EOL." -> $errors"; |
|
402 | + } |
|
403 | + |
|
404 | + $failureMessages[] = $fullMessage; |
|
405 | + }//end if |
|
406 | + }//end foreach |
|
407 | + |
|
408 | + return $failureMessages; |
|
409 | + |
|
410 | + }//end generateFailureMessages() |
|
411 | + |
|
412 | + |
|
413 | + /** |
|
414 | + * Get a list of CLI values to set before the file is tested. |
|
415 | + * |
|
416 | + * @param string $filename The name of the file being tested. |
|
417 | + * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
418 | + * |
|
419 | + * @return void |
|
420 | + */ |
|
421 | + public function setCliValues($filename, $config) |
|
422 | + { |
|
423 | + return; |
|
424 | + |
|
425 | + }//end setCliValues() |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * Returns the lines where errors should occur. |
|
430 | + * |
|
431 | + * The key of the array should represent the line number and the value |
|
432 | + * should represent the number of errors that should occur on that line. |
|
433 | + * |
|
434 | + * @return array<int, int> |
|
435 | + */ |
|
436 | + abstract protected function getErrorList(); |
|
437 | + |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns the lines where warnings should occur. |
|
441 | + * |
|
442 | + * The key of the array should represent the line number and the value |
|
443 | + * should represent the number of warnings that should occur on that line. |
|
444 | + * |
|
445 | + * @return array<int, int> |
|
446 | + */ |
|
447 | + abstract protected function getWarningList(); |
|
448 | 448 | |
449 | 449 | |
450 | 450 | }//end class |
@@ -18,106 +18,106 @@ |
||
18 | 18 | { |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * Prepare the test runner. |
|
23 | - * |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public static function main() |
|
27 | - { |
|
28 | - TestRunner::run(self::suite()); |
|
29 | - |
|
30 | - }//end main() |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * Add all sniff unit tests into a test suite. |
|
35 | - * |
|
36 | - * Sniff unit tests are found by recursing through the 'Tests' directory |
|
37 | - * of each installed coding standard. |
|
38 | - * |
|
39 | - * @return \PHPUnit\Framework\TestSuite |
|
40 | - */ |
|
41 | - public static function suite() |
|
42 | - { |
|
43 | - $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'] = []; |
|
44 | - $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'] = []; |
|
45 | - $GLOBALS['PHP_CODESNIFFER_SNIFF_CASE_FILES'] = []; |
|
46 | - |
|
47 | - $suite = new TestSuite('PHP CodeSniffer Standards'); |
|
48 | - |
|
49 | - $isInstalled = !is_file(__DIR__.'/../../autoload.php'); |
|
50 | - |
|
51 | - // Optionally allow for ignoring the tests for one or more standards. |
|
52 | - $ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS'); |
|
53 | - if ($ignoreTestsForStandards === false) { |
|
54 | - $ignoreTestsForStandards = []; |
|
55 | - } else { |
|
56 | - $ignoreTestsForStandards = explode(',', $ignoreTestsForStandards); |
|
57 | - } |
|
58 | - |
|
59 | - $installedStandards = self::getInstalledStandardDetails(); |
|
60 | - |
|
61 | - foreach ($installedStandards as $standard => $details) { |
|
62 | - Autoload::addSearchPath($details['path'], $details['namespace']); |
|
63 | - |
|
64 | - // If the test is running PEAR installed, the built-in standards |
|
65 | - // are split into different directories; one for the sniffs and |
|
66 | - // a different file system location for tests. |
|
67 | - if ($isInstalled === true && is_dir(dirname($details['path']).DIRECTORY_SEPARATOR.'Generic') === true) { |
|
68 | - $testPath = realpath(__DIR__.'/../../src/Standards/'.$standard); |
|
69 | - } else { |
|
70 | - $testPath = $details['path']; |
|
71 | - } |
|
72 | - |
|
73 | - if (in_array($standard, $ignoreTestsForStandards, true) === true) { |
|
74 | - continue; |
|
75 | - } |
|
76 | - |
|
77 | - $testsDir = $testPath.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; |
|
78 | - if (is_dir($testsDir) === false) { |
|
79 | - // No tests for this standard. |
|
80 | - continue; |
|
81 | - } |
|
82 | - |
|
83 | - $di = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testsDir)); |
|
84 | - |
|
85 | - foreach ($di as $file) { |
|
86 | - // Skip hidden files. |
|
87 | - if (substr($file->getFilename(), 0, 1) === '.') { |
|
88 | - continue; |
|
89 | - } |
|
90 | - |
|
91 | - // Tests must have the extension 'php'. |
|
92 | - $parts = explode('.', $file); |
|
93 | - $ext = array_pop($parts); |
|
94 | - if ($ext !== 'php') { |
|
95 | - continue; |
|
96 | - } |
|
97 | - |
|
98 | - $className = Autoload::loadFile($file->getPathname()); |
|
99 | - $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $details['path']; |
|
100 | - $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$className] = $testsDir; |
|
101 | - $suite->addTestSuite($className); |
|
102 | - } |
|
103 | - }//end foreach |
|
104 | - |
|
105 | - return $suite; |
|
106 | - |
|
107 | - }//end suite() |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Get the details of all coding standards installed. |
|
112 | - * |
|
113 | - * @return array |
|
114 | - * @see Standards::getInstalledStandardDetails() |
|
115 | - */ |
|
116 | - protected static function getInstalledStandardDetails() |
|
117 | - { |
|
118 | - return Standards::getInstalledStandardDetails(true); |
|
119 | - |
|
120 | - }//end getInstalledStandardDetails() |
|
21 | + /** |
|
22 | + * Prepare the test runner. |
|
23 | + * |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public static function main() |
|
27 | + { |
|
28 | + TestRunner::run(self::suite()); |
|
29 | + |
|
30 | + }//end main() |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * Add all sniff unit tests into a test suite. |
|
35 | + * |
|
36 | + * Sniff unit tests are found by recursing through the 'Tests' directory |
|
37 | + * of each installed coding standard. |
|
38 | + * |
|
39 | + * @return \PHPUnit\Framework\TestSuite |
|
40 | + */ |
|
41 | + public static function suite() |
|
42 | + { |
|
43 | + $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'] = []; |
|
44 | + $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'] = []; |
|
45 | + $GLOBALS['PHP_CODESNIFFER_SNIFF_CASE_FILES'] = []; |
|
46 | + |
|
47 | + $suite = new TestSuite('PHP CodeSniffer Standards'); |
|
48 | + |
|
49 | + $isInstalled = !is_file(__DIR__.'/../../autoload.php'); |
|
50 | + |
|
51 | + // Optionally allow for ignoring the tests for one or more standards. |
|
52 | + $ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS'); |
|
53 | + if ($ignoreTestsForStandards === false) { |
|
54 | + $ignoreTestsForStandards = []; |
|
55 | + } else { |
|
56 | + $ignoreTestsForStandards = explode(',', $ignoreTestsForStandards); |
|
57 | + } |
|
58 | + |
|
59 | + $installedStandards = self::getInstalledStandardDetails(); |
|
60 | + |
|
61 | + foreach ($installedStandards as $standard => $details) { |
|
62 | + Autoload::addSearchPath($details['path'], $details['namespace']); |
|
63 | + |
|
64 | + // If the test is running PEAR installed, the built-in standards |
|
65 | + // are split into different directories; one for the sniffs and |
|
66 | + // a different file system location for tests. |
|
67 | + if ($isInstalled === true && is_dir(dirname($details['path']).DIRECTORY_SEPARATOR.'Generic') === true) { |
|
68 | + $testPath = realpath(__DIR__.'/../../src/Standards/'.$standard); |
|
69 | + } else { |
|
70 | + $testPath = $details['path']; |
|
71 | + } |
|
72 | + |
|
73 | + if (in_array($standard, $ignoreTestsForStandards, true) === true) { |
|
74 | + continue; |
|
75 | + } |
|
76 | + |
|
77 | + $testsDir = $testPath.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; |
|
78 | + if (is_dir($testsDir) === false) { |
|
79 | + // No tests for this standard. |
|
80 | + continue; |
|
81 | + } |
|
82 | + |
|
83 | + $di = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testsDir)); |
|
84 | + |
|
85 | + foreach ($di as $file) { |
|
86 | + // Skip hidden files. |
|
87 | + if (substr($file->getFilename(), 0, 1) === '.') { |
|
88 | + continue; |
|
89 | + } |
|
90 | + |
|
91 | + // Tests must have the extension 'php'. |
|
92 | + $parts = explode('.', $file); |
|
93 | + $ext = array_pop($parts); |
|
94 | + if ($ext !== 'php') { |
|
95 | + continue; |
|
96 | + } |
|
97 | + |
|
98 | + $className = Autoload::loadFile($file->getPathname()); |
|
99 | + $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $details['path']; |
|
100 | + $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$className] = $testsDir; |
|
101 | + $suite->addTestSuite($className); |
|
102 | + } |
|
103 | + }//end foreach |
|
104 | + |
|
105 | + return $suite; |
|
106 | + |
|
107 | + }//end suite() |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Get the details of all coding standards installed. |
|
112 | + * |
|
113 | + * @return array |
|
114 | + * @see Standards::getInstalledStandardDetails() |
|
115 | + */ |
|
116 | + protected static function getInstalledStandardDetails() |
|
117 | + { |
|
118 | + return Standards::getInstalledStandardDetails(true); |
|
119 | + |
|
120 | + }//end getInstalledStandardDetails() |
|
121 | 121 | |
122 | 122 | |
123 | 123 | }//end class |
@@ -16,120 +16,120 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Test valid public function/method names. |
|
21 | - * |
|
22 | - * @return void |
|
23 | - */ |
|
24 | - public function testValidNotClassFormatPublic() |
|
25 | - { |
|
26 | - $this->assertTrue(Common::isCamelCaps('thisIsCamelCaps', false, true, true)); |
|
27 | - $this->assertTrue(Common::isCamelCaps('thisISCamelCaps', false, true, false)); |
|
28 | - |
|
29 | - }//end testValidNotClassFormatPublic() |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Test invalid public function/method names. |
|
34 | - * |
|
35 | - * @return void |
|
36 | - */ |
|
37 | - public function testInvalidNotClassFormatPublic() |
|
38 | - { |
|
39 | - $this->assertFalse(Common::isCamelCaps('_thisIsCamelCaps', false, true, true)); |
|
40 | - $this->assertFalse(Common::isCamelCaps('thisISCamelCaps', false, true, true)); |
|
41 | - $this->assertFalse(Common::isCamelCaps('ThisIsCamelCaps', false, true, true)); |
|
42 | - |
|
43 | - $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, true, true)); |
|
44 | - $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, true, true)); |
|
45 | - $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, true, true)); |
|
46 | - |
|
47 | - $this->assertFalse(Common::isCamelCaps('this*IsCamelCaps', false, true, true)); |
|
48 | - $this->assertFalse(Common::isCamelCaps('this-IsCamelCaps', false, true, true)); |
|
49 | - $this->assertFalse(Common::isCamelCaps('this_IsCamelCaps', false, true, true)); |
|
50 | - $this->assertFalse(Common::isCamelCaps('this_is_camel_caps', false, true, true)); |
|
51 | - |
|
52 | - }//end testInvalidNotClassFormatPublic() |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Test valid private method names. |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function testValidNotClassFormatPrivate() |
|
61 | - { |
|
62 | - $this->assertTrue(Common::isCamelCaps('_thisIsCamelCaps', false, false, true)); |
|
63 | - $this->assertTrue(Common::isCamelCaps('_thisISCamelCaps', false, false, false)); |
|
64 | - $this->assertTrue(Common::isCamelCaps('_i18N', false, false, true)); |
|
65 | - $this->assertTrue(Common::isCamelCaps('_i18n', false, false, true)); |
|
66 | - |
|
67 | - }//end testValidNotClassFormatPrivate() |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Test invalid private method names. |
|
72 | - * |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function testInvalidNotClassFormatPrivate() |
|
76 | - { |
|
77 | - $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', false, false, true)); |
|
78 | - $this->assertFalse(Common::isCamelCaps('_thisISCamelCaps', false, false, true)); |
|
79 | - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', false, false, true)); |
|
80 | - $this->assertFalse(Common::isCamelCaps('__thisIsCamelCaps', false, false, true)); |
|
81 | - $this->assertFalse(Common::isCamelCaps('__thisISCamelCaps', false, false, false)); |
|
82 | - |
|
83 | - $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, false, true)); |
|
84 | - $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, false, true)); |
|
85 | - $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, false, true)); |
|
86 | - $this->assertFalse(Common::isCamelCaps('_this_is_camel_caps', false, false, true)); |
|
87 | - |
|
88 | - }//end testInvalidNotClassFormatPrivate() |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Test valid class names. |
|
93 | - * |
|
94 | - * @return void |
|
95 | - */ |
|
96 | - public function testValidClassFormatPublic() |
|
97 | - { |
|
98 | - $this->assertTrue(Common::isCamelCaps('ThisIsCamelCaps', true, true, true)); |
|
99 | - $this->assertTrue(Common::isCamelCaps('ThisISCamelCaps', true, true, false)); |
|
100 | - $this->assertTrue(Common::isCamelCaps('This3IsCamelCaps', true, true, false)); |
|
101 | - |
|
102 | - }//end testValidClassFormatPublic() |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Test invalid class names. |
|
107 | - * |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - public function testInvalidClassFormat() |
|
111 | - { |
|
112 | - $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', true)); |
|
113 | - $this->assertFalse(Common::isCamelCaps('This-IsCamelCaps', true)); |
|
114 | - $this->assertFalse(Common::isCamelCaps('This_Is_Camel_Caps', true)); |
|
115 | - |
|
116 | - }//end testInvalidClassFormat() |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Test invalid class names with the private flag set. |
|
121 | - * |
|
122 | - * Note that the private flag is ignored if the class format |
|
123 | - * flag is set, so these names are all invalid. |
|
124 | - * |
|
125 | - * @return void |
|
126 | - */ |
|
127 | - public function testInvalidClassFormatPrivate() |
|
128 | - { |
|
129 | - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, true)); |
|
130 | - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, false)); |
|
131 | - |
|
132 | - }//end testInvalidClassFormatPrivate() |
|
19 | + /** |
|
20 | + * Test valid public function/method names. |
|
21 | + * |
|
22 | + * @return void |
|
23 | + */ |
|
24 | + public function testValidNotClassFormatPublic() |
|
25 | + { |
|
26 | + $this->assertTrue(Common::isCamelCaps('thisIsCamelCaps', false, true, true)); |
|
27 | + $this->assertTrue(Common::isCamelCaps('thisISCamelCaps', false, true, false)); |
|
28 | + |
|
29 | + }//end testValidNotClassFormatPublic() |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Test invalid public function/method names. |
|
34 | + * |
|
35 | + * @return void |
|
36 | + */ |
|
37 | + public function testInvalidNotClassFormatPublic() |
|
38 | + { |
|
39 | + $this->assertFalse(Common::isCamelCaps('_thisIsCamelCaps', false, true, true)); |
|
40 | + $this->assertFalse(Common::isCamelCaps('thisISCamelCaps', false, true, true)); |
|
41 | + $this->assertFalse(Common::isCamelCaps('ThisIsCamelCaps', false, true, true)); |
|
42 | + |
|
43 | + $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, true, true)); |
|
44 | + $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, true, true)); |
|
45 | + $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, true, true)); |
|
46 | + |
|
47 | + $this->assertFalse(Common::isCamelCaps('this*IsCamelCaps', false, true, true)); |
|
48 | + $this->assertFalse(Common::isCamelCaps('this-IsCamelCaps', false, true, true)); |
|
49 | + $this->assertFalse(Common::isCamelCaps('this_IsCamelCaps', false, true, true)); |
|
50 | + $this->assertFalse(Common::isCamelCaps('this_is_camel_caps', false, true, true)); |
|
51 | + |
|
52 | + }//end testInvalidNotClassFormatPublic() |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Test valid private method names. |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function testValidNotClassFormatPrivate() |
|
61 | + { |
|
62 | + $this->assertTrue(Common::isCamelCaps('_thisIsCamelCaps', false, false, true)); |
|
63 | + $this->assertTrue(Common::isCamelCaps('_thisISCamelCaps', false, false, false)); |
|
64 | + $this->assertTrue(Common::isCamelCaps('_i18N', false, false, true)); |
|
65 | + $this->assertTrue(Common::isCamelCaps('_i18n', false, false, true)); |
|
66 | + |
|
67 | + }//end testValidNotClassFormatPrivate() |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Test invalid private method names. |
|
72 | + * |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function testInvalidNotClassFormatPrivate() |
|
76 | + { |
|
77 | + $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', false, false, true)); |
|
78 | + $this->assertFalse(Common::isCamelCaps('_thisISCamelCaps', false, false, true)); |
|
79 | + $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', false, false, true)); |
|
80 | + $this->assertFalse(Common::isCamelCaps('__thisIsCamelCaps', false, false, true)); |
|
81 | + $this->assertFalse(Common::isCamelCaps('__thisISCamelCaps', false, false, false)); |
|
82 | + |
|
83 | + $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, false, true)); |
|
84 | + $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, false, true)); |
|
85 | + $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, false, true)); |
|
86 | + $this->assertFalse(Common::isCamelCaps('_this_is_camel_caps', false, false, true)); |
|
87 | + |
|
88 | + }//end testInvalidNotClassFormatPrivate() |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Test valid class names. |
|
93 | + * |
|
94 | + * @return void |
|
95 | + */ |
|
96 | + public function testValidClassFormatPublic() |
|
97 | + { |
|
98 | + $this->assertTrue(Common::isCamelCaps('ThisIsCamelCaps', true, true, true)); |
|
99 | + $this->assertTrue(Common::isCamelCaps('ThisISCamelCaps', true, true, false)); |
|
100 | + $this->assertTrue(Common::isCamelCaps('This3IsCamelCaps', true, true, false)); |
|
101 | + |
|
102 | + }//end testValidClassFormatPublic() |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Test invalid class names. |
|
107 | + * |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + public function testInvalidClassFormat() |
|
111 | + { |
|
112 | + $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', true)); |
|
113 | + $this->assertFalse(Common::isCamelCaps('This-IsCamelCaps', true)); |
|
114 | + $this->assertFalse(Common::isCamelCaps('This_Is_Camel_Caps', true)); |
|
115 | + |
|
116 | + }//end testInvalidClassFormat() |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Test invalid class names with the private flag set. |
|
121 | + * |
|
122 | + * Note that the private flag is ignored if the class format |
|
123 | + * flag is set, so these names are all invalid. |
|
124 | + * |
|
125 | + * @return void |
|
126 | + */ |
|
127 | + public function testInvalidClassFormatPrivate() |
|
128 | + { |
|
129 | + $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, true)); |
|
130 | + $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, false)); |
|
131 | + |
|
132 | + }//end testInvalidClassFormatPrivate() |
|
133 | 133 | |
134 | 134 | |
135 | 135 | }//end class |
@@ -18,8 +18,8 @@ |
||
18 | 18 | |
19 | 19 | /* testSwitch */ |
20 | 20 | switch ($a) { |
21 | - case 1: {break;} |
|
22 | - default: {break;} |
|
21 | + case 1: {break;} |
|
22 | + default: {break;} |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /* testStatementAsArrayValue */ |
@@ -17,187 +17,187 @@ |
||
17 | 17 | class FindEndOfStatementTest extends TestCase |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. |
|
22 | - * |
|
23 | - * @var \PHP_CodeSniffer\Files\File |
|
24 | - */ |
|
25 | - private $phpcsFile; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. |
|
30 | - * |
|
31 | - * Methods used for these tests can be found in a test case file in the same |
|
32 | - * directory and with the same name, using the .inc extension. |
|
33 | - * |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function setUp() |
|
37 | - { |
|
38 | - $config = new Config(); |
|
39 | - $config->standards = ['Generic']; |
|
20 | + /** |
|
21 | + * The PHP_CodeSniffer_File object containing parsed contents of the test case file. |
|
22 | + * |
|
23 | + * @var \PHP_CodeSniffer\Files\File |
|
24 | + */ |
|
25 | + private $phpcsFile; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. |
|
30 | + * |
|
31 | + * Methods used for these tests can be found in a test case file in the same |
|
32 | + * directory and with the same name, using the .inc extension. |
|
33 | + * |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function setUp() |
|
37 | + { |
|
38 | + $config = new Config(); |
|
39 | + $config->standards = ['Generic']; |
|
40 | 40 | |
41 | - $ruleset = new Ruleset($config); |
|
41 | + $ruleset = new Ruleset($config); |
|
42 | 42 | |
43 | - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; |
|
44 | - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); |
|
45 | - $this->phpcsFile->process(); |
|
46 | - |
|
47 | - }//end setUp() |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Clean up after finished test. |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function tearDown() |
|
56 | - { |
|
57 | - unset($this->phpcsFile); |
|
58 | - |
|
59 | - }//end tearDown() |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * Test a simple assignment. |
|
64 | - * |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function testSimpleAssignment() |
|
68 | - { |
|
69 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2); |
|
70 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
43 | + $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; |
|
44 | + $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); |
|
45 | + $this->phpcsFile->process(); |
|
46 | + |
|
47 | + }//end setUp() |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Clean up after finished test. |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function tearDown() |
|
56 | + { |
|
57 | + unset($this->phpcsFile); |
|
58 | + |
|
59 | + }//end tearDown() |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * Test a simple assignment. |
|
64 | + * |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function testSimpleAssignment() |
|
68 | + { |
|
69 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2); |
|
70 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
71 | 71 | |
72 | - $tokens = $this->phpcsFile->getTokens(); |
|
73 | - $this->assertSame($tokens[($start + 5)], $tokens[$found]); |
|
72 | + $tokens = $this->phpcsFile->getTokens(); |
|
73 | + $this->assertSame($tokens[($start + 5)], $tokens[$found]); |
|
74 | 74 | |
75 | - }//end testSimpleAssignment() |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Test a direct call to a control structure. |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function testControlStructure() |
|
84 | - { |
|
85 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2); |
|
86 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
87 | - |
|
88 | - $tokens = $this->phpcsFile->getTokens(); |
|
89 | - $this->assertSame($tokens[($start + 6)], $tokens[$found]); |
|
90 | - |
|
91 | - }//end testControlStructure() |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Test the assignment of a closure. |
|
96 | - * |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function testClosureAssignment() |
|
100 | - { |
|
101 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2); |
|
102 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
103 | - |
|
104 | - $tokens = $this->phpcsFile->getTokens(); |
|
105 | - $this->assertSame($tokens[($start + 13)], $tokens[$found]); |
|
106 | - |
|
107 | - }//end testClosureAssignment() |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Test using a heredoc in a function argument. |
|
112 | - * |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - public function testHeredocFunctionArg() |
|
116 | - { |
|
117 | - // Find the end of the function. |
|
118 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2); |
|
119 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
120 | - |
|
121 | - $tokens = $this->phpcsFile->getTokens(); |
|
122 | - $this->assertSame($tokens[($start + 10)], $tokens[$found]); |
|
75 | + }//end testSimpleAssignment() |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Test a direct call to a control structure. |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function testControlStructure() |
|
84 | + { |
|
85 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2); |
|
86 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
87 | + |
|
88 | + $tokens = $this->phpcsFile->getTokens(); |
|
89 | + $this->assertSame($tokens[($start + 6)], $tokens[$found]); |
|
90 | + |
|
91 | + }//end testControlStructure() |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Test the assignment of a closure. |
|
96 | + * |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function testClosureAssignment() |
|
100 | + { |
|
101 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2); |
|
102 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
103 | + |
|
104 | + $tokens = $this->phpcsFile->getTokens(); |
|
105 | + $this->assertSame($tokens[($start + 13)], $tokens[$found]); |
|
106 | + |
|
107 | + }//end testClosureAssignment() |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Test using a heredoc in a function argument. |
|
112 | + * |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + public function testHeredocFunctionArg() |
|
116 | + { |
|
117 | + // Find the end of the function. |
|
118 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2); |
|
119 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
120 | + |
|
121 | + $tokens = $this->phpcsFile->getTokens(); |
|
122 | + $this->assertSame($tokens[($start + 10)], $tokens[$found]); |
|
123 | 123 | |
124 | - // Find the end of the heredoc. |
|
125 | - $start += 2; |
|
126 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
124 | + // Find the end of the heredoc. |
|
125 | + $start += 2; |
|
126 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
127 | 127 | |
128 | - $tokens = $this->phpcsFile->getTokens(); |
|
129 | - $this->assertSame($tokens[($start + 4)], $tokens[$found]); |
|
128 | + $tokens = $this->phpcsFile->getTokens(); |
|
129 | + $this->assertSame($tokens[($start + 4)], $tokens[$found]); |
|
130 | 130 | |
131 | - // Find the end of the last arg. |
|
132 | - $start = ($found + 2); |
|
133 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
131 | + // Find the end of the last arg. |
|
132 | + $start = ($found + 2); |
|
133 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
134 | 134 | |
135 | - $tokens = $this->phpcsFile->getTokens(); |
|
136 | - $this->assertSame($tokens[$start], $tokens[$found]); |
|
135 | + $tokens = $this->phpcsFile->getTokens(); |
|
136 | + $this->assertSame($tokens[$start], $tokens[$found]); |
|
137 | 137 | |
138 | - }//end testHeredocFunctionArg() |
|
138 | + }//end testHeredocFunctionArg() |
|
139 | 139 | |
140 | 140 | |
141 | - /** |
|
142 | - * Test parts of a switch statement. |
|
143 | - * |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - public function testSwitch() |
|
147 | - { |
|
148 | - // Find the end of the switch. |
|
149 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2); |
|
150 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
141 | + /** |
|
142 | + * Test parts of a switch statement. |
|
143 | + * |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + public function testSwitch() |
|
147 | + { |
|
148 | + // Find the end of the switch. |
|
149 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2); |
|
150 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
151 | 151 | |
152 | - $tokens = $this->phpcsFile->getTokens(); |
|
153 | - $this->assertSame($tokens[($start + 28)], $tokens[$found]); |
|
152 | + $tokens = $this->phpcsFile->getTokens(); |
|
153 | + $this->assertSame($tokens[($start + 28)], $tokens[$found]); |
|
154 | 154 | |
155 | - // Find the end of the case. |
|
156 | - $start += 9; |
|
157 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
155 | + // Find the end of the case. |
|
156 | + $start += 9; |
|
157 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
158 | 158 | |
159 | - $tokens = $this->phpcsFile->getTokens(); |
|
160 | - $this->assertSame($tokens[($start + 8)], $tokens[$found]); |
|
159 | + $tokens = $this->phpcsFile->getTokens(); |
|
160 | + $this->assertSame($tokens[($start + 8)], $tokens[$found]); |
|
161 | 161 | |
162 | - // Find the end of default case. |
|
163 | - $start += 11; |
|
164 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
162 | + // Find the end of default case. |
|
163 | + $start += 11; |
|
164 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
165 | 165 | |
166 | - $tokens = $this->phpcsFile->getTokens(); |
|
167 | - $this->assertSame($tokens[($start + 6)], $tokens[$found]); |
|
166 | + $tokens = $this->phpcsFile->getTokens(); |
|
167 | + $this->assertSame($tokens[($start + 6)], $tokens[$found]); |
|
168 | 168 | |
169 | - }//end testSwitch() |
|
169 | + }//end testSwitch() |
|
170 | 170 | |
171 | 171 | |
172 | - /** |
|
173 | - * Test statements that are array values. |
|
174 | - * |
|
175 | - * @return void |
|
176 | - */ |
|
177 | - public function testStatementAsArrayValue() |
|
178 | - { |
|
179 | - // Test short array syntax. |
|
180 | - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7); |
|
181 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
172 | + /** |
|
173 | + * Test statements that are array values. |
|
174 | + * |
|
175 | + * @return void |
|
176 | + */ |
|
177 | + public function testStatementAsArrayValue() |
|
178 | + { |
|
179 | + // Test short array syntax. |
|
180 | + $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7); |
|
181 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
182 | 182 | |
183 | - $tokens = $this->phpcsFile->getTokens(); |
|
184 | - $this->assertSame($tokens[($start + 2)], $tokens[$found]); |
|
183 | + $tokens = $this->phpcsFile->getTokens(); |
|
184 | + $this->assertSame($tokens[($start + 2)], $tokens[$found]); |
|
185 | 185 | |
186 | - // Test long array syntax. |
|
187 | - $start += 12; |
|
188 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
186 | + // Test long array syntax. |
|
187 | + $start += 12; |
|
188 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
189 | 189 | |
190 | - $tokens = $this->phpcsFile->getTokens(); |
|
191 | - $this->assertSame($tokens[($start + 2)], $tokens[$found]); |
|
190 | + $tokens = $this->phpcsFile->getTokens(); |
|
191 | + $this->assertSame($tokens[($start + 2)], $tokens[$found]); |
|
192 | 192 | |
193 | - // Test same statement outside of array. |
|
194 | - $start += 10; |
|
195 | - $found = $this->phpcsFile->findEndOfStatement($start); |
|
193 | + // Test same statement outside of array. |
|
194 | + $start += 10; |
|
195 | + $found = $this->phpcsFile->findEndOfStatement($start); |
|
196 | 196 | |
197 | - $tokens = $this->phpcsFile->getTokens(); |
|
198 | - $this->assertSame($tokens[($start + 3)], $tokens[$found]); |
|
197 | + $tokens = $this->phpcsFile->getTokens(); |
|
198 | + $this->assertSame($tokens[($start + 3)], $tokens[$found]); |
|
199 | 199 | |
200 | - }//end testStatementAsArrayValue() |
|
200 | + }//end testStatementAsArrayValue() |
|
201 | 201 | |
202 | 202 | |
203 | 203 | }//end class |
@@ -2,113 +2,113 @@ |
||
2 | 2 | |
3 | 3 | class TestMemberProperties |
4 | 4 | { |
5 | - /* testVar */ |
|
6 | - var $varA = true; |
|
7 | - |
|
8 | - /* testPublic */ |
|
9 | - public $varB = true; |
|
10 | - |
|
11 | - /* testProtected */ |
|
12 | - protected $varC = true; |
|
13 | - |
|
14 | - /* testPrivate */ |
|
15 | - private $varD = true; |
|
16 | - |
|
17 | - /* testStatic */ |
|
18 | - static $varE = true; |
|
19 | - |
|
20 | - /* testStaticVar */ |
|
21 | - static var $varF = true; |
|
22 | - |
|
23 | - /* testVarStatic */ |
|
24 | - var static $varG = true; |
|
25 | - |
|
26 | - /* testPublicStatic */ |
|
27 | - public static $varH = true; |
|
28 | - |
|
29 | - /* testProtectedStatic */ |
|
30 | - static protected $varI = true; |
|
31 | - |
|
32 | - /* testPrivateStatic */ |
|
33 | - private static $varJ = true; |
|
34 | - |
|
35 | - /* testNoPrefix */ |
|
36 | - $varK = true; |
|
37 | - |
|
38 | - /* testPublicStaticWithDocblock */ |
|
39 | - /** |
|
40 | - * Comment here. |
|
41 | - * |
|
42 | - * @phpcs:ignore Standard.Category.Sniff -- because |
|
43 | - * @var boolean |
|
44 | - */ |
|
45 | - public static $varH = true; |
|
46 | - |
|
47 | - /* testProtectedStaticWithDocblock */ |
|
48 | - /** |
|
49 | - * Comment here. |
|
50 | - * |
|
51 | - * @phpcs:ignore Standard.Category.Sniff -- because |
|
52 | - * @var boolean |
|
53 | - */ |
|
54 | - static protected $varI = true; |
|
55 | - |
|
56 | - /* testPrivateStaticWithDocblock */ |
|
57 | - /** |
|
58 | - * Comment here. |
|
59 | - * |
|
60 | - * @phpcs:ignore Standard.Category.Sniff -- because |
|
61 | - * @var boolean |
|
62 | - */ |
|
63 | - private static $varJ = true; |
|
64 | - |
|
65 | - |
|
66 | - protected static |
|
67 | - /* testGroupProtectedStatic 1 */ |
|
68 | - $varL, |
|
69 | - /* testGroupProtectedStatic 2 */ |
|
70 | - $varM, |
|
71 | - /* testGroupProtectedStatic 3 */ |
|
72 | - $varN; |
|
73 | - |
|
74 | - private |
|
75 | - /* testGroupPrivate 1 */ |
|
76 | - $varO = true, |
|
77 | - /* testGroupPrivate 2 */ |
|
78 | - $varP = array( 'a' => 'a', 'b' => 'b' ), |
|
79 | - /* testGroupPrivate 3 */ |
|
80 | - $varQ = 'string', |
|
81 | - /* testGroupPrivate 4 */ |
|
82 | - $varR = 123, |
|
83 | - /* testGroupPrivate 5 */ |
|
84 | - $varS = ONE / self::THREE, |
|
85 | - /* testGroupPrivate 6 */ |
|
86 | - $varT = [ |
|
5 | + /* testVar */ |
|
6 | + var $varA = true; |
|
7 | + |
|
8 | + /* testPublic */ |
|
9 | + public $varB = true; |
|
10 | + |
|
11 | + /* testProtected */ |
|
12 | + protected $varC = true; |
|
13 | + |
|
14 | + /* testPrivate */ |
|
15 | + private $varD = true; |
|
16 | + |
|
17 | + /* testStatic */ |
|
18 | + static $varE = true; |
|
19 | + |
|
20 | + /* testStaticVar */ |
|
21 | + static var $varF = true; |
|
22 | + |
|
23 | + /* testVarStatic */ |
|
24 | + var static $varG = true; |
|
25 | + |
|
26 | + /* testPublicStatic */ |
|
27 | + public static $varH = true; |
|
28 | + |
|
29 | + /* testProtectedStatic */ |
|
30 | + static protected $varI = true; |
|
31 | + |
|
32 | + /* testPrivateStatic */ |
|
33 | + private static $varJ = true; |
|
34 | + |
|
35 | + /* testNoPrefix */ |
|
36 | + $varK = true; |
|
37 | + |
|
38 | + /* testPublicStaticWithDocblock */ |
|
39 | + /** |
|
40 | + * Comment here. |
|
41 | + * |
|
42 | + * @phpcs:ignore Standard.Category.Sniff -- because |
|
43 | + * @var boolean |
|
44 | + */ |
|
45 | + public static $varH = true; |
|
46 | + |
|
47 | + /* testProtectedStaticWithDocblock */ |
|
48 | + /** |
|
49 | + * Comment here. |
|
50 | + * |
|
51 | + * @phpcs:ignore Standard.Category.Sniff -- because |
|
52 | + * @var boolean |
|
53 | + */ |
|
54 | + static protected $varI = true; |
|
55 | + |
|
56 | + /* testPrivateStaticWithDocblock */ |
|
57 | + /** |
|
58 | + * Comment here. |
|
59 | + * |
|
60 | + * @phpcs:ignore Standard.Category.Sniff -- because |
|
61 | + * @var boolean |
|
62 | + */ |
|
63 | + private static $varJ = true; |
|
64 | + |
|
65 | + |
|
66 | + protected static |
|
67 | + /* testGroupProtectedStatic 1 */ |
|
68 | + $varL, |
|
69 | + /* testGroupProtectedStatic 2 */ |
|
70 | + $varM, |
|
71 | + /* testGroupProtectedStatic 3 */ |
|
72 | + $varN; |
|
73 | + |
|
74 | + private |
|
75 | + /* testGroupPrivate 1 */ |
|
76 | + $varO = true, |
|
77 | + /* testGroupPrivate 2 */ |
|
78 | + $varP = array( 'a' => 'a', 'b' => 'b' ), |
|
79 | + /* testGroupPrivate 3 */ |
|
80 | + $varQ = 'string', |
|
81 | + /* testGroupPrivate 4 */ |
|
82 | + $varR = 123, |
|
83 | + /* testGroupPrivate 5 */ |
|
84 | + $varS = ONE / self::THREE, |
|
85 | + /* testGroupPrivate 6 */ |
|
86 | + $varT = [ |
|
87 | 87 | 'a' => 'a', |
88 | 88 | 'b' => 'b' |
89 | 89 | ], |
90 | - /* testGroupPrivate 7 */ |
|
91 | - $varU = __DIR__ . "/base"; |
|
90 | + /* testGroupPrivate 7 */ |
|
91 | + $varU = __DIR__ . "/base"; |
|
92 | 92 | |
93 | 93 | |
94 | - /* testMethodParam */ |
|
95 | - public function methodName($param) { |
|
96 | - /* testImportedGlobal */ |
|
97 | - global $importedGlobal = true; |
|
94 | + /* testMethodParam */ |
|
95 | + public function methodName($param) { |
|
96 | + /* testImportedGlobal */ |
|
97 | + global $importedGlobal = true; |
|
98 | 98 | |
99 | - /* testLocalVariable */ |
|
100 | - $localVariable = true; |
|
101 | - } |
|
99 | + /* testLocalVariable */ |
|
100 | + $localVariable = true; |
|
101 | + } |
|
102 | 102 | |
103 | - /* testPropertyAfterMethod */ |
|
104 | - private static $varV = true; |
|
103 | + /* testPropertyAfterMethod */ |
|
104 | + private static $varV = true; |
|
105 | 105 | |
106 | 106 | } |
107 | 107 | |
108 | 108 | interface Base |
109 | 109 | { |
110 | - /* testInterfaceProperty */ |
|
111 | - protected $anonymous; |
|
110 | + /* testInterfaceProperty */ |
|
111 | + protected $anonymous; |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /* testGlobalVariable */ |
@@ -17,384 +17,384 @@ |
||
17 | 17 | class GetMemberPropertiesTest extends TestCase |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. |
|
22 | - * |
|
23 | - * @var \PHP_CodeSniffer\Files\File |
|
24 | - */ |
|
25 | - private $phpcsFile; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * Initialize & tokenize PHP_CodeSniffer_File with code from the test case file. |
|
30 | - * |
|
31 | - * Methods used for these tests can be found in a test case file in the same |
|
32 | - * directory and with the same name, using the .inc extension. |
|
33 | - * |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function setUp() |
|
37 | - { |
|
38 | - $config = new Config(); |
|
39 | - $config->standards = ['Generic']; |
|
40 | - |
|
41 | - $ruleset = new Ruleset($config); |
|
42 | - |
|
43 | - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; |
|
44 | - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); |
|
45 | - $this->phpcsFile->process(); |
|
46 | - |
|
47 | - }//end setUp() |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Clean up after finished test. |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function tearDown() |
|
56 | - { |
|
57 | - unset($this->phpcsFile); |
|
58 | - |
|
59 | - }//end tearDown() |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * Test the getMemberProperties() method. |
|
64 | - * |
|
65 | - * @param string $identifier Comment which precedes the test case. |
|
66 | - * @param bool $expected Expected function output. |
|
67 | - * |
|
68 | - * @dataProvider dataGetMemberProperties |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function testGetMemberProperties($identifier, $expected) |
|
73 | - { |
|
74 | - $start = ($this->phpcsFile->numTokens - 1); |
|
75 | - $delim = $this->phpcsFile->findPrevious( |
|
76 | - T_COMMENT, |
|
77 | - $start, |
|
78 | - null, |
|
79 | - false, |
|
80 | - $identifier |
|
81 | - ); |
|
82 | - $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); |
|
83 | - |
|
84 | - $result = $this->phpcsFile->getMemberProperties($variable); |
|
85 | - $this->assertSame($expected, $result); |
|
86 | - |
|
87 | - }//end testGetMemberProperties() |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * Data provider for the GetMemberProperties test. |
|
92 | - * |
|
93 | - * @see testGetMemberProperties() |
|
94 | - * |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function dataGetMemberProperties() |
|
98 | - { |
|
99 | - return [ |
|
100 | - [ |
|
101 | - '/* testVar */', |
|
102 | - [ |
|
103 | - 'scope' => 'public', |
|
104 | - 'scope_specified' => false, |
|
105 | - 'is_static' => false, |
|
106 | - ], |
|
107 | - ], |
|
108 | - [ |
|
109 | - '/* testPublic */', |
|
110 | - [ |
|
111 | - 'scope' => 'public', |
|
112 | - 'scope_specified' => true, |
|
113 | - 'is_static' => false, |
|
114 | - ], |
|
115 | - ], |
|
116 | - [ |
|
117 | - '/* testProtected */', |
|
118 | - [ |
|
119 | - 'scope' => 'protected', |
|
120 | - 'scope_specified' => true, |
|
121 | - 'is_static' => false, |
|
122 | - ], |
|
123 | - ], |
|
124 | - [ |
|
125 | - '/* testPrivate */', |
|
126 | - [ |
|
127 | - 'scope' => 'private', |
|
128 | - 'scope_specified' => true, |
|
129 | - 'is_static' => false, |
|
130 | - ], |
|
131 | - ], |
|
132 | - [ |
|
133 | - '/* testStatic */', |
|
134 | - [ |
|
135 | - 'scope' => 'public', |
|
136 | - 'scope_specified' => false, |
|
137 | - 'is_static' => true, |
|
138 | - ], |
|
139 | - ], |
|
140 | - [ |
|
141 | - '/* testStaticVar */', |
|
142 | - [ |
|
143 | - 'scope' => 'public', |
|
144 | - 'scope_specified' => false, |
|
145 | - 'is_static' => true, |
|
146 | - ], |
|
147 | - ], |
|
148 | - [ |
|
149 | - '/* testVarStatic */', |
|
150 | - [ |
|
151 | - 'scope' => 'public', |
|
152 | - 'scope_specified' => false, |
|
153 | - 'is_static' => true, |
|
154 | - ], |
|
155 | - ], |
|
156 | - [ |
|
157 | - '/* testPublicStatic */', |
|
158 | - [ |
|
159 | - 'scope' => 'public', |
|
160 | - 'scope_specified' => true, |
|
161 | - 'is_static' => true, |
|
162 | - ], |
|
163 | - ], |
|
164 | - [ |
|
165 | - '/* testProtectedStatic */', |
|
166 | - [ |
|
167 | - 'scope' => 'protected', |
|
168 | - 'scope_specified' => true, |
|
169 | - 'is_static' => true, |
|
170 | - ], |
|
171 | - ], |
|
172 | - [ |
|
173 | - '/* testPrivateStatic */', |
|
174 | - [ |
|
175 | - 'scope' => 'private', |
|
176 | - 'scope_specified' => true, |
|
177 | - 'is_static' => true, |
|
178 | - ], |
|
179 | - ], |
|
180 | - [ |
|
181 | - '/* testPublicStaticWithDocblock */', |
|
182 | - [ |
|
183 | - 'scope' => 'public', |
|
184 | - 'scope_specified' => true, |
|
185 | - 'is_static' => true, |
|
186 | - ], |
|
187 | - ], |
|
188 | - [ |
|
189 | - '/* testProtectedStaticWithDocblock */', |
|
190 | - [ |
|
191 | - 'scope' => 'protected', |
|
192 | - 'scope_specified' => true, |
|
193 | - 'is_static' => true, |
|
194 | - ], |
|
195 | - ], |
|
196 | - [ |
|
197 | - '/* testPrivateStaticWithDocblock */', |
|
198 | - [ |
|
199 | - 'scope' => 'private', |
|
200 | - 'scope_specified' => true, |
|
201 | - 'is_static' => true, |
|
202 | - ], |
|
203 | - ], |
|
204 | - [ |
|
205 | - '/* testNoPrefix */', |
|
206 | - [ |
|
207 | - 'scope' => 'public', |
|
208 | - 'scope_specified' => false, |
|
209 | - 'is_static' => false, |
|
210 | - ], |
|
211 | - ], |
|
212 | - [ |
|
213 | - '/* testGroupProtectedStatic 1 */', |
|
214 | - [ |
|
215 | - 'scope' => 'protected', |
|
216 | - 'scope_specified' => true, |
|
217 | - 'is_static' => true, |
|
218 | - ], |
|
219 | - ], |
|
220 | - [ |
|
221 | - '/* testGroupProtectedStatic 2 */', |
|
222 | - [ |
|
223 | - 'scope' => 'protected', |
|
224 | - 'scope_specified' => true, |
|
225 | - 'is_static' => true, |
|
226 | - ], |
|
227 | - ], |
|
228 | - [ |
|
229 | - '/* testGroupProtectedStatic 3 */', |
|
230 | - [ |
|
231 | - 'scope' => 'protected', |
|
232 | - 'scope_specified' => true, |
|
233 | - 'is_static' => true, |
|
234 | - ], |
|
235 | - ], |
|
236 | - [ |
|
237 | - '/* testGroupPrivate 1 */', |
|
238 | - [ |
|
239 | - 'scope' => 'private', |
|
240 | - 'scope_specified' => true, |
|
241 | - 'is_static' => false, |
|
242 | - ], |
|
243 | - ], |
|
244 | - [ |
|
245 | - '/* testGroupPrivate 2 */', |
|
246 | - [ |
|
247 | - 'scope' => 'private', |
|
248 | - 'scope_specified' => true, |
|
249 | - 'is_static' => false, |
|
250 | - ], |
|
251 | - ], |
|
252 | - [ |
|
253 | - '/* testGroupPrivate 3 */', |
|
254 | - [ |
|
255 | - 'scope' => 'private', |
|
256 | - 'scope_specified' => true, |
|
257 | - 'is_static' => false, |
|
258 | - ], |
|
259 | - ], |
|
260 | - [ |
|
261 | - '/* testGroupPrivate 4 */', |
|
262 | - [ |
|
263 | - 'scope' => 'private', |
|
264 | - 'scope_specified' => true, |
|
265 | - 'is_static' => false, |
|
266 | - ], |
|
267 | - ], |
|
268 | - [ |
|
269 | - '/* testGroupPrivate 5 */', |
|
270 | - [ |
|
271 | - 'scope' => 'private', |
|
272 | - 'scope_specified' => true, |
|
273 | - 'is_static' => false, |
|
274 | - ], |
|
275 | - ], |
|
276 | - [ |
|
277 | - '/* testGroupPrivate 6 */', |
|
278 | - [ |
|
279 | - 'scope' => 'private', |
|
280 | - 'scope_specified' => true, |
|
281 | - 'is_static' => false, |
|
282 | - ], |
|
283 | - ], |
|
284 | - [ |
|
285 | - '/* testGroupPrivate 7 */', |
|
286 | - [ |
|
287 | - 'scope' => 'private', |
|
288 | - 'scope_specified' => true, |
|
289 | - 'is_static' => false, |
|
290 | - ], |
|
291 | - ], |
|
292 | - [ |
|
293 | - '/* testPropertyAfterMethod */', |
|
294 | - [ |
|
295 | - 'scope' => 'private', |
|
296 | - 'scope_specified' => true, |
|
297 | - 'is_static' => true, |
|
298 | - ], |
|
299 | - ], |
|
300 | - [ |
|
301 | - '/* testInterfaceProperty */', |
|
302 | - [], |
|
303 | - ], |
|
304 | - [ |
|
305 | - '/* testNestedProperty 1 */', |
|
306 | - [ |
|
307 | - 'scope' => 'public', |
|
308 | - 'scope_specified' => true, |
|
309 | - 'is_static' => false, |
|
310 | - ], |
|
311 | - ], |
|
312 | - [ |
|
313 | - '/* testNestedProperty 2 */', |
|
314 | - [ |
|
315 | - 'scope' => 'public', |
|
316 | - 'scope_specified' => true, |
|
317 | - 'is_static' => false, |
|
318 | - ], |
|
319 | - ], |
|
320 | - ]; |
|
321 | - |
|
322 | - }//end dataGetMemberProperties() |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * Test receiving an expected exception when a non property is passed. |
|
327 | - * |
|
328 | - * @param string $identifier Comment which precedes the test case. |
|
329 | - * |
|
330 | - * @expectedException PHP_CodeSniffer\Exceptions\TokenizerException |
|
331 | - * @expectedExceptionMessage $stackPtr is not a class member var |
|
332 | - * |
|
333 | - * @dataProvider dataNotClassProperty |
|
334 | - * |
|
335 | - * @return void |
|
336 | - */ |
|
337 | - public function testNotClassPropertyException($identifier) |
|
338 | - { |
|
339 | - $start = ($this->phpcsFile->numTokens - 1); |
|
340 | - $delim = $this->phpcsFile->findPrevious( |
|
341 | - T_COMMENT, |
|
342 | - $start, |
|
343 | - null, |
|
344 | - false, |
|
345 | - $identifier |
|
346 | - ); |
|
347 | - $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); |
|
348 | - |
|
349 | - $result = $this->phpcsFile->getMemberProperties($variable); |
|
350 | - |
|
351 | - }//end testNotClassPropertyException() |
|
352 | - |
|
353 | - |
|
354 | - /** |
|
355 | - * Data provider for the NotClassPropertyException test. |
|
356 | - * |
|
357 | - * @see testNotClassPropertyException() |
|
358 | - * |
|
359 | - * @return array |
|
360 | - */ |
|
361 | - public function dataNotClassProperty() |
|
362 | - { |
|
363 | - return [ |
|
364 | - ['/* testMethodParam */'], |
|
365 | - ['/* testImportedGlobal */'], |
|
366 | - ['/* testLocalVariable */'], |
|
367 | - ['/* testGlobalVariable */'], |
|
368 | - ['/* testNestedMethodParam 1 */'], |
|
369 | - ['/* testNestedMethodParam 2 */'], |
|
370 | - ]; |
|
371 | - |
|
372 | - }//end dataNotClassProperty() |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Test receiving an expected exception when a non variable is passed. |
|
377 | - * |
|
378 | - * @expectedException PHP_CodeSniffer\Exceptions\TokenizerException |
|
379 | - * @expectedExceptionMessage $stackPtr must be of type T_VARIABLE |
|
380 | - * |
|
381 | - * @return void |
|
382 | - */ |
|
383 | - public function testNotAVariableException() |
|
384 | - { |
|
385 | - $start = ($this->phpcsFile->numTokens - 1); |
|
386 | - $delim = $this->phpcsFile->findPrevious( |
|
387 | - T_COMMENT, |
|
388 | - $start, |
|
389 | - null, |
|
390 | - false, |
|
391 | - '/* testNotAVariable */' |
|
392 | - ); |
|
393 | - $next = $this->phpcsFile->findNext(T_WHITESPACE, ($delim + 1), null, true); |
|
394 | - |
|
395 | - $result = $this->phpcsFile->getMemberProperties($next); |
|
396 | - |
|
397 | - }//end testNotAVariableException() |
|
20 | + /** |
|
21 | + * The PHP_CodeSniffer_File object containing parsed contents of the test case file. |
|
22 | + * |
|
23 | + * @var \PHP_CodeSniffer\Files\File |
|
24 | + */ |
|
25 | + private $phpcsFile; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * Initialize & tokenize PHP_CodeSniffer_File with code from the test case file. |
|
30 | + * |
|
31 | + * Methods used for these tests can be found in a test case file in the same |
|
32 | + * directory and with the same name, using the .inc extension. |
|
33 | + * |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function setUp() |
|
37 | + { |
|
38 | + $config = new Config(); |
|
39 | + $config->standards = ['Generic']; |
|
40 | + |
|
41 | + $ruleset = new Ruleset($config); |
|
42 | + |
|
43 | + $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; |
|
44 | + $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); |
|
45 | + $this->phpcsFile->process(); |
|
46 | + |
|
47 | + }//end setUp() |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Clean up after finished test. |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function tearDown() |
|
56 | + { |
|
57 | + unset($this->phpcsFile); |
|
58 | + |
|
59 | + }//end tearDown() |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * Test the getMemberProperties() method. |
|
64 | + * |
|
65 | + * @param string $identifier Comment which precedes the test case. |
|
66 | + * @param bool $expected Expected function output. |
|
67 | + * |
|
68 | + * @dataProvider dataGetMemberProperties |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function testGetMemberProperties($identifier, $expected) |
|
73 | + { |
|
74 | + $start = ($this->phpcsFile->numTokens - 1); |
|
75 | + $delim = $this->phpcsFile->findPrevious( |
|
76 | + T_COMMENT, |
|
77 | + $start, |
|
78 | + null, |
|
79 | + false, |
|
80 | + $identifier |
|
81 | + ); |
|
82 | + $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); |
|
83 | + |
|
84 | + $result = $this->phpcsFile->getMemberProperties($variable); |
|
85 | + $this->assertSame($expected, $result); |
|
86 | + |
|
87 | + }//end testGetMemberProperties() |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * Data provider for the GetMemberProperties test. |
|
92 | + * |
|
93 | + * @see testGetMemberProperties() |
|
94 | + * |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function dataGetMemberProperties() |
|
98 | + { |
|
99 | + return [ |
|
100 | + [ |
|
101 | + '/* testVar */', |
|
102 | + [ |
|
103 | + 'scope' => 'public', |
|
104 | + 'scope_specified' => false, |
|
105 | + 'is_static' => false, |
|
106 | + ], |
|
107 | + ], |
|
108 | + [ |
|
109 | + '/* testPublic */', |
|
110 | + [ |
|
111 | + 'scope' => 'public', |
|
112 | + 'scope_specified' => true, |
|
113 | + 'is_static' => false, |
|
114 | + ], |
|
115 | + ], |
|
116 | + [ |
|
117 | + '/* testProtected */', |
|
118 | + [ |
|
119 | + 'scope' => 'protected', |
|
120 | + 'scope_specified' => true, |
|
121 | + 'is_static' => false, |
|
122 | + ], |
|
123 | + ], |
|
124 | + [ |
|
125 | + '/* testPrivate */', |
|
126 | + [ |
|
127 | + 'scope' => 'private', |
|
128 | + 'scope_specified' => true, |
|
129 | + 'is_static' => false, |
|
130 | + ], |
|
131 | + ], |
|
132 | + [ |
|
133 | + '/* testStatic */', |
|
134 | + [ |
|
135 | + 'scope' => 'public', |
|
136 | + 'scope_specified' => false, |
|
137 | + 'is_static' => true, |
|
138 | + ], |
|
139 | + ], |
|
140 | + [ |
|
141 | + '/* testStaticVar */', |
|
142 | + [ |
|
143 | + 'scope' => 'public', |
|
144 | + 'scope_specified' => false, |
|
145 | + 'is_static' => true, |
|
146 | + ], |
|
147 | + ], |
|
148 | + [ |
|
149 | + '/* testVarStatic */', |
|
150 | + [ |
|
151 | + 'scope' => 'public', |
|
152 | + 'scope_specified' => false, |
|
153 | + 'is_static' => true, |
|
154 | + ], |
|
155 | + ], |
|
156 | + [ |
|
157 | + '/* testPublicStatic */', |
|
158 | + [ |
|
159 | + 'scope' => 'public', |
|
160 | + 'scope_specified' => true, |
|
161 | + 'is_static' => true, |
|
162 | + ], |
|
163 | + ], |
|
164 | + [ |
|
165 | + '/* testProtectedStatic */', |
|
166 | + [ |
|
167 | + 'scope' => 'protected', |
|
168 | + 'scope_specified' => true, |
|
169 | + 'is_static' => true, |
|
170 | + ], |
|
171 | + ], |
|
172 | + [ |
|
173 | + '/* testPrivateStatic */', |
|
174 | + [ |
|
175 | + 'scope' => 'private', |
|
176 | + 'scope_specified' => true, |
|
177 | + 'is_static' => true, |
|
178 | + ], |
|
179 | + ], |
|
180 | + [ |
|
181 | + '/* testPublicStaticWithDocblock */', |
|
182 | + [ |
|
183 | + 'scope' => 'public', |
|
184 | + 'scope_specified' => true, |
|
185 | + 'is_static' => true, |
|
186 | + ], |
|
187 | + ], |
|
188 | + [ |
|
189 | + '/* testProtectedStaticWithDocblock */', |
|
190 | + [ |
|
191 | + 'scope' => 'protected', |
|
192 | + 'scope_specified' => true, |
|
193 | + 'is_static' => true, |
|
194 | + ], |
|
195 | + ], |
|
196 | + [ |
|
197 | + '/* testPrivateStaticWithDocblock */', |
|
198 | + [ |
|
199 | + 'scope' => 'private', |
|
200 | + 'scope_specified' => true, |
|
201 | + 'is_static' => true, |
|
202 | + ], |
|
203 | + ], |
|
204 | + [ |
|
205 | + '/* testNoPrefix */', |
|
206 | + [ |
|
207 | + 'scope' => 'public', |
|
208 | + 'scope_specified' => false, |
|
209 | + 'is_static' => false, |
|
210 | + ], |
|
211 | + ], |
|
212 | + [ |
|
213 | + '/* testGroupProtectedStatic 1 */', |
|
214 | + [ |
|
215 | + 'scope' => 'protected', |
|
216 | + 'scope_specified' => true, |
|
217 | + 'is_static' => true, |
|
218 | + ], |
|
219 | + ], |
|
220 | + [ |
|
221 | + '/* testGroupProtectedStatic 2 */', |
|
222 | + [ |
|
223 | + 'scope' => 'protected', |
|
224 | + 'scope_specified' => true, |
|
225 | + 'is_static' => true, |
|
226 | + ], |
|
227 | + ], |
|
228 | + [ |
|
229 | + '/* testGroupProtectedStatic 3 */', |
|
230 | + [ |
|
231 | + 'scope' => 'protected', |
|
232 | + 'scope_specified' => true, |
|
233 | + 'is_static' => true, |
|
234 | + ], |
|
235 | + ], |
|
236 | + [ |
|
237 | + '/* testGroupPrivate 1 */', |
|
238 | + [ |
|
239 | + 'scope' => 'private', |
|
240 | + 'scope_specified' => true, |
|
241 | + 'is_static' => false, |
|
242 | + ], |
|
243 | + ], |
|
244 | + [ |
|
245 | + '/* testGroupPrivate 2 */', |
|
246 | + [ |
|
247 | + 'scope' => 'private', |
|
248 | + 'scope_specified' => true, |
|
249 | + 'is_static' => false, |
|
250 | + ], |
|
251 | + ], |
|
252 | + [ |
|
253 | + '/* testGroupPrivate 3 */', |
|
254 | + [ |
|
255 | + 'scope' => 'private', |
|
256 | + 'scope_specified' => true, |
|
257 | + 'is_static' => false, |
|
258 | + ], |
|
259 | + ], |
|
260 | + [ |
|
261 | + '/* testGroupPrivate 4 */', |
|
262 | + [ |
|
263 | + 'scope' => 'private', |
|
264 | + 'scope_specified' => true, |
|
265 | + 'is_static' => false, |
|
266 | + ], |
|
267 | + ], |
|
268 | + [ |
|
269 | + '/* testGroupPrivate 5 */', |
|
270 | + [ |
|
271 | + 'scope' => 'private', |
|
272 | + 'scope_specified' => true, |
|
273 | + 'is_static' => false, |
|
274 | + ], |
|
275 | + ], |
|
276 | + [ |
|
277 | + '/* testGroupPrivate 6 */', |
|
278 | + [ |
|
279 | + 'scope' => 'private', |
|
280 | + 'scope_specified' => true, |
|
281 | + 'is_static' => false, |
|
282 | + ], |
|
283 | + ], |
|
284 | + [ |
|
285 | + '/* testGroupPrivate 7 */', |
|
286 | + [ |
|
287 | + 'scope' => 'private', |
|
288 | + 'scope_specified' => true, |
|
289 | + 'is_static' => false, |
|
290 | + ], |
|
291 | + ], |
|
292 | + [ |
|
293 | + '/* testPropertyAfterMethod */', |
|
294 | + [ |
|
295 | + 'scope' => 'private', |
|
296 | + 'scope_specified' => true, |
|
297 | + 'is_static' => true, |
|
298 | + ], |
|
299 | + ], |
|
300 | + [ |
|
301 | + '/* testInterfaceProperty */', |
|
302 | + [], |
|
303 | + ], |
|
304 | + [ |
|
305 | + '/* testNestedProperty 1 */', |
|
306 | + [ |
|
307 | + 'scope' => 'public', |
|
308 | + 'scope_specified' => true, |
|
309 | + 'is_static' => false, |
|
310 | + ], |
|
311 | + ], |
|
312 | + [ |
|
313 | + '/* testNestedProperty 2 */', |
|
314 | + [ |
|
315 | + 'scope' => 'public', |
|
316 | + 'scope_specified' => true, |
|
317 | + 'is_static' => false, |
|
318 | + ], |
|
319 | + ], |
|
320 | + ]; |
|
321 | + |
|
322 | + }//end dataGetMemberProperties() |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * Test receiving an expected exception when a non property is passed. |
|
327 | + * |
|
328 | + * @param string $identifier Comment which precedes the test case. |
|
329 | + * |
|
330 | + * @expectedException PHP_CodeSniffer\Exceptions\TokenizerException |
|
331 | + * @expectedExceptionMessage $stackPtr is not a class member var |
|
332 | + * |
|
333 | + * @dataProvider dataNotClassProperty |
|
334 | + * |
|
335 | + * @return void |
|
336 | + */ |
|
337 | + public function testNotClassPropertyException($identifier) |
|
338 | + { |
|
339 | + $start = ($this->phpcsFile->numTokens - 1); |
|
340 | + $delim = $this->phpcsFile->findPrevious( |
|
341 | + T_COMMENT, |
|
342 | + $start, |
|
343 | + null, |
|
344 | + false, |
|
345 | + $identifier |
|
346 | + ); |
|
347 | + $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); |
|
348 | + |
|
349 | + $result = $this->phpcsFile->getMemberProperties($variable); |
|
350 | + |
|
351 | + }//end testNotClassPropertyException() |
|
352 | + |
|
353 | + |
|
354 | + /** |
|
355 | + * Data provider for the NotClassPropertyException test. |
|
356 | + * |
|
357 | + * @see testNotClassPropertyException() |
|
358 | + * |
|
359 | + * @return array |
|
360 | + */ |
|
361 | + public function dataNotClassProperty() |
|
362 | + { |
|
363 | + return [ |
|
364 | + ['/* testMethodParam */'], |
|
365 | + ['/* testImportedGlobal */'], |
|
366 | + ['/* testLocalVariable */'], |
|
367 | + ['/* testGlobalVariable */'], |
|
368 | + ['/* testNestedMethodParam 1 */'], |
|
369 | + ['/* testNestedMethodParam 2 */'], |
|
370 | + ]; |
|
371 | + |
|
372 | + }//end dataNotClassProperty() |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Test receiving an expected exception when a non variable is passed. |
|
377 | + * |
|
378 | + * @expectedException PHP_CodeSniffer\Exceptions\TokenizerException |
|
379 | + * @expectedExceptionMessage $stackPtr must be of type T_VARIABLE |
|
380 | + * |
|
381 | + * @return void |
|
382 | + */ |
|
383 | + public function testNotAVariableException() |
|
384 | + { |
|
385 | + $start = ($this->phpcsFile->numTokens - 1); |
|
386 | + $delim = $this->phpcsFile->findPrevious( |
|
387 | + T_COMMENT, |
|
388 | + $start, |
|
389 | + null, |
|
390 | + false, |
|
391 | + '/* testNotAVariable */' |
|
392 | + ); |
|
393 | + $next = $this->phpcsFile->findNext(T_WHITESPACE, ($delim + 1), null, true); |
|
394 | + |
|
395 | + $result = $this->phpcsFile->getMemberProperties($next); |
|
396 | + |
|
397 | + }//end testNotAVariableException() |
|
398 | 398 | |
399 | 399 | |
400 | 400 | }//end class |