annotate vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2 /**
Chris@4 3 * An abstract class that all sniff unit tests must extend.
Chris@4 4 *
Chris@4 5 * A sniff unit test checks a .inc file for expected violations of a single
Chris@4 6 * coding standard. Expected errors and warnings that are not found, or
Chris@4 7 * warnings and errors that are not expected, are considered test failures.
Chris@4 8 *
Chris@4 9 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@4 10 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@4 11 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@4 12 */
Chris@4 13
Chris@4 14 namespace PHP_CodeSniffer\Tests\Standards;
Chris@4 15
Chris@4 16 use PHP_CodeSniffer\Config;
Chris@4 17 use PHP_CodeSniffer\Exceptions\RuntimeException;
Chris@4 18 use PHP_CodeSniffer\Ruleset;
Chris@4 19 use PHP_CodeSniffer\Files\LocalFile;
Chris@4 20 use PHP_CodeSniffer\Util\Common;
Chris@4 21 use PHPUnit\Framework\TestCase;
Chris@4 22
Chris@4 23 abstract class AbstractSniffUnitTest extends TestCase
Chris@4 24 {
Chris@4 25
Chris@4 26 /**
Chris@4 27 * Enable or disable the backup and restoration of the $GLOBALS array.
Chris@4 28 * Overwrite this attribute in a child class of TestCase.
Chris@4 29 * Setting this attribute in setUp() has no effect!
Chris@4 30 *
Chris@4 31 * @var boolean
Chris@4 32 */
Chris@4 33 protected $backupGlobals = false;
Chris@4 34
Chris@4 35 /**
Chris@4 36 * The path to the standard's main directory.
Chris@4 37 *
Chris@4 38 * @var string
Chris@4 39 */
Chris@4 40 public $standardsDir = null;
Chris@4 41
Chris@4 42 /**
Chris@4 43 * The path to the standard's test directory.
Chris@4 44 *
Chris@4 45 * @var string
Chris@4 46 */
Chris@4 47 public $testsDir = null;
Chris@4 48
Chris@4 49
Chris@4 50 /**
Chris@4 51 * Sets up this unit test.
Chris@4 52 *
Chris@4 53 * @return void
Chris@4 54 */
Chris@4 55 protected function setUp()
Chris@4 56 {
Chris@4 57 $class = get_class($this);
Chris@4 58 $this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class];
Chris@4 59 $this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class];
Chris@4 60
Chris@4 61 }//end setUp()
Chris@4 62
Chris@4 63
Chris@4 64 /**
Chris@4 65 * Get a list of all test files to check.
Chris@4 66 *
Chris@4 67 * These will have the same base as the sniff name but different extensions.
Chris@4 68 * We ignore the .php file as it is the class.
Chris@4 69 *
Chris@4 70 * @param string $testFileBase The base path that the unit tests files will have.
Chris@4 71 *
Chris@4 72 * @return string[]
Chris@4 73 */
Chris@4 74 protected function getTestFiles($testFileBase)
Chris@4 75 {
Chris@4 76 $testFiles = [];
Chris@4 77
Chris@4 78 $dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR));
Chris@4 79 $di = new \DirectoryIterator($dir);
Chris@4 80
Chris@4 81 foreach ($di as $file) {
Chris@4 82 $path = $file->getPathname();
Chris@4 83 if (substr($path, 0, strlen($testFileBase)) === $testFileBase) {
Chris@4 84 if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') {
Chris@4 85 $testFiles[] = $path;
Chris@4 86 }
Chris@4 87 }
Chris@4 88 }
Chris@4 89
Chris@4 90 // Put them in order.
Chris@4 91 sort($testFiles);
Chris@4 92
Chris@4 93 return $testFiles;
Chris@4 94
Chris@4 95 }//end getTestFiles()
Chris@4 96
Chris@4 97
Chris@4 98 /**
Chris@4 99 * Should this test be skipped for some reason.
Chris@4 100 *
Chris@4 101 * @return boolean
Chris@4 102 */
Chris@4 103 protected function shouldSkipTest()
Chris@4 104 {
Chris@4 105 return false;
Chris@4 106
Chris@4 107 }//end shouldSkipTest()
Chris@4 108
Chris@4 109
Chris@4 110 /**
Chris@4 111 * Tests the extending classes Sniff class.
Chris@4 112 *
Chris@4 113 * @return void
Chris@4 114 * @throws \PHPUnit\Framework\Exception
Chris@4 115 */
Chris@4 116 final public function testSniff()
Chris@4 117 {
Chris@4 118 // Skip this test if we can't run in this environment.
Chris@4 119 if ($this->shouldSkipTest() === true) {
Chris@4 120 $this->markTestSkipped();
Chris@4 121 }
Chris@4 122
Chris@4 123 $sniffCode = Common::getSniffCode(get_class($this));
Chris@4 124 list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode);
Chris@4 125
Chris@4 126 $testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.';
Chris@4 127
Chris@4 128 // Get a list of all test files to check.
Chris@4 129 $testFiles = $this->getTestFiles($testFileBase);
Chris@4 130 $GLOBALS['PHP_CODESNIFFER_SNIFF_CASE_FILES'][] = $testFiles;
Chris@4 131
Chris@4 132 if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG']) === true) {
Chris@4 133 $config = $GLOBALS['PHP_CODESNIFFER_CONFIG'];
Chris@4 134 } else {
Chris@4 135 $config = new Config();
Chris@4 136 $config->cache = false;
Chris@4 137 $GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config;
Chris@4 138 }
Chris@4 139
Chris@4 140 $config->standards = [$standardName];
Chris@4 141 $config->sniffs = [$sniffCode];
Chris@4 142 $config->ignored = [];
Chris@4 143
Chris@4 144 if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS']) === false) {
Chris@4 145 $GLOBALS['PHP_CODESNIFFER_RULESETS'] = [];
Chris@4 146 }
Chris@4 147
Chris@4 148 if (isset($GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName]) === false) {
Chris@4 149 $ruleset = new Ruleset($config);
Chris@4 150 $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName] = $ruleset;
Chris@4 151 }
Chris@4 152
Chris@4 153 $ruleset = $GLOBALS['PHP_CODESNIFFER_RULESETS'][$standardName];
Chris@4 154
Chris@4 155 $sniffFile = $this->standardsDir.DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'Sniff.php';
Chris@4 156
Chris@4 157 $sniffClassName = substr(get_class($this), 0, -8).'Sniff';
Chris@4 158 $sniffClassName = str_replace('\Tests\\', '\Sniffs\\', $sniffClassName);
Chris@4 159 $sniffClassName = Common::cleanSniffClass($sniffClassName);
Chris@4 160
Chris@4 161 $restrictions = [strtolower($sniffClassName) => true];
Chris@4 162 $ruleset->registerSniffs([$sniffFile], $restrictions, []);
Chris@4 163 $ruleset->populateTokenListeners();
Chris@4 164
Chris@4 165 $failureMessages = [];
Chris@4 166 foreach ($testFiles as $testFile) {
Chris@4 167 $filename = basename($testFile);
Chris@4 168 $oldConfig = $config->getSettings();
Chris@4 169
Chris@4 170 try {
Chris@4 171 $this->setCliValues($filename, $config);
Chris@4 172 $phpcsFile = new LocalFile($testFile, $ruleset, $config);
Chris@4 173 $phpcsFile->process();
Chris@4 174 } catch (RuntimeException $e) {
Chris@4 175 $this->fail('An unexpected exception has been caught: '.$e->getMessage());
Chris@4 176 }
Chris@4 177
Chris@4 178 $failures = $this->generateFailureMessages($phpcsFile);
Chris@4 179 $failureMessages = array_merge($failureMessages, $failures);
Chris@4 180
Chris@4 181 if ($phpcsFile->getFixableCount() > 0) {
Chris@4 182 // Attempt to fix the errors.
Chris@4 183 $phpcsFile->fixer->fixFile();
Chris@4 184 $fixable = $phpcsFile->getFixableCount();
Chris@4 185 if ($fixable > 0) {
Chris@4 186 $failureMessages[] = "Failed to fix $fixable fixable violations in $filename";
Chris@4 187 }
Chris@4 188
Chris@4 189 // Check for a .fixed file to check for accuracy of fixes.
Chris@4 190 $fixedFile = $testFile.'.fixed';
Chris@4 191 if (file_exists($fixedFile) === true) {
Chris@4 192 $diff = $phpcsFile->fixer->generateDiff($fixedFile);
Chris@4 193 if (trim($diff) !== '') {
Chris@4 194 $filename = basename($testFile);
Chris@4 195 $fixedFilename = basename($fixedFile);
Chris@4 196 $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff";
Chris@4 197 }
Chris@4 198 }
Chris@4 199 }
Chris@4 200
Chris@4 201 // Restore the config.
Chris@4 202 $config->setSettings($oldConfig);
Chris@4 203 }//end foreach
Chris@4 204
Chris@4 205 if (empty($failureMessages) === false) {
Chris@4 206 $this->fail(implode(PHP_EOL, $failureMessages));
Chris@4 207 }
Chris@4 208
Chris@4 209 }//end testSniff()
Chris@4 210
Chris@4 211
Chris@4 212 /**
Chris@4 213 * Generate a list of test failures for a given sniffed file.
Chris@4 214 *
Chris@4 215 * @param \PHP_CodeSniffer\Files\LocalFile $file The file being tested.
Chris@4 216 *
Chris@4 217 * @return array
Chris@4 218 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException
Chris@4 219 */
Chris@4 220 public function generateFailureMessages(LocalFile $file)
Chris@4 221 {
Chris@4 222 $testFile = $file->getFilename();
Chris@4 223
Chris@4 224 $foundErrors = $file->getErrors();
Chris@4 225 $foundWarnings = $file->getWarnings();
Chris@4 226 $expectedErrors = $this->getErrorList(basename($testFile));
Chris@4 227 $expectedWarnings = $this->getWarningList(basename($testFile));
Chris@4 228
Chris@4 229 if (is_array($expectedErrors) === false) {
Chris@4 230 throw new RuntimeException('getErrorList() must return an array');
Chris@4 231 }
Chris@4 232
Chris@4 233 if (is_array($expectedWarnings) === false) {
Chris@4 234 throw new RuntimeException('getWarningList() must return an array');
Chris@4 235 }
Chris@4 236
Chris@4 237 /*
Chris@4 238 We merge errors and warnings together to make it easier
Chris@4 239 to iterate over them and produce the errors string. In this way,
Chris@4 240 we can report on errors and warnings in the same line even though
Chris@4 241 it's not really structured to allow that.
Chris@4 242 */
Chris@4 243
Chris@4 244 $allProblems = [];
Chris@4 245 $failureMessages = [];
Chris@4 246
Chris@4 247 foreach ($foundErrors as $line => $lineErrors) {
Chris@4 248 foreach ($lineErrors as $column => $errors) {
Chris@4 249 if (isset($allProblems[$line]) === false) {
Chris@4 250 $allProblems[$line] = [
Chris@4 251 'expected_errors' => 0,
Chris@4 252 'expected_warnings' => 0,
Chris@4 253 'found_errors' => [],
Chris@4 254 'found_warnings' => [],
Chris@4 255 ];
Chris@4 256 }
Chris@4 257
Chris@4 258 $foundErrorsTemp = [];
Chris@4 259 foreach ($allProblems[$line]['found_errors'] as $foundError) {
Chris@4 260 $foundErrorsTemp[] = $foundError;
Chris@4 261 }
Chris@4 262
Chris@4 263 $errorsTemp = [];
Chris@4 264 foreach ($errors as $foundError) {
Chris@4 265 $errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')';
Chris@4 266
Chris@4 267 $source = $foundError['source'];
Chris@5 268 if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'], true) === false) {
Chris@4 269 $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source;
Chris@4 270 }
Chris@4 271
Chris@4 272 if ($foundError['fixable'] === true
Chris@5 273 && in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'], true) === false
Chris@4 274 ) {
Chris@4 275 $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source;
Chris@4 276 }
Chris@4 277 }
Chris@4 278
Chris@4 279 $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp);
Chris@4 280 }//end foreach
Chris@4 281
Chris@4 282 if (isset($expectedErrors[$line]) === true) {
Chris@4 283 $allProblems[$line]['expected_errors'] = $expectedErrors[$line];
Chris@4 284 } else {
Chris@4 285 $allProblems[$line]['expected_errors'] = 0;
Chris@4 286 }
Chris@4 287
Chris@4 288 unset($expectedErrors[$line]);
Chris@4 289 }//end foreach
Chris@4 290
Chris@4 291 foreach ($expectedErrors as $line => $numErrors) {
Chris@4 292 if (isset($allProblems[$line]) === false) {
Chris@4 293 $allProblems[$line] = [
Chris@4 294 'expected_errors' => 0,
Chris@4 295 'expected_warnings' => 0,
Chris@4 296 'found_errors' => [],
Chris@4 297 'found_warnings' => [],
Chris@4 298 ];
Chris@4 299 }
Chris@4 300
Chris@4 301 $allProblems[$line]['expected_errors'] = $numErrors;
Chris@4 302 }
Chris@4 303
Chris@4 304 foreach ($foundWarnings as $line => $lineWarnings) {
Chris@4 305 foreach ($lineWarnings as $column => $warnings) {
Chris@4 306 if (isset($allProblems[$line]) === false) {
Chris@4 307 $allProblems[$line] = [
Chris@4 308 'expected_errors' => 0,
Chris@4 309 'expected_warnings' => 0,
Chris@4 310 'found_errors' => [],
Chris@4 311 'found_warnings' => [],
Chris@4 312 ];
Chris@4 313 }
Chris@4 314
Chris@4 315 $foundWarningsTemp = [];
Chris@4 316 foreach ($allProblems[$line]['found_warnings'] as $foundWarning) {
Chris@4 317 $foundWarningsTemp[] = $foundWarning;
Chris@4 318 }
Chris@4 319
Chris@4 320 $warningsTemp = [];
Chris@4 321 foreach ($warnings as $warning) {
Chris@4 322 $warningsTemp[] = $warning['message'].' ('.$warning['source'].')';
Chris@4 323 }
Chris@4 324
Chris@4 325 $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp);
Chris@4 326 }//end foreach
Chris@4 327
Chris@4 328 if (isset($expectedWarnings[$line]) === true) {
Chris@4 329 $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line];
Chris@4 330 } else {
Chris@4 331 $allProblems[$line]['expected_warnings'] = 0;
Chris@4 332 }
Chris@4 333
Chris@4 334 unset($expectedWarnings[$line]);
Chris@4 335 }//end foreach
Chris@4 336
Chris@4 337 foreach ($expectedWarnings as $line => $numWarnings) {
Chris@4 338 if (isset($allProblems[$line]) === false) {
Chris@4 339 $allProblems[$line] = [
Chris@4 340 'expected_errors' => 0,
Chris@4 341 'expected_warnings' => 0,
Chris@4 342 'found_errors' => [],
Chris@4 343 'found_warnings' => [],
Chris@4 344 ];
Chris@4 345 }
Chris@4 346
Chris@4 347 $allProblems[$line]['expected_warnings'] = $numWarnings;
Chris@4 348 }
Chris@4 349
Chris@4 350 // Order the messages by line number.
Chris@4 351 ksort($allProblems);
Chris@4 352
Chris@4 353 foreach ($allProblems as $line => $problems) {
Chris@4 354 $numErrors = count($problems['found_errors']);
Chris@4 355 $numWarnings = count($problems['found_warnings']);
Chris@4 356 $expectedErrors = $problems['expected_errors'];
Chris@4 357 $expectedWarnings = $problems['expected_warnings'];
Chris@4 358
Chris@4 359 $errors = '';
Chris@4 360 $foundString = '';
Chris@4 361
Chris@4 362 if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) {
Chris@4 363 $lineMessage = "[LINE $line]";
Chris@4 364 $expectedMessage = 'Expected ';
Chris@4 365 $foundMessage = 'in '.basename($testFile).' but found ';
Chris@4 366
Chris@4 367 if ($expectedErrors !== $numErrors) {
Chris@4 368 $expectedMessage .= "$expectedErrors error(s)";
Chris@4 369 $foundMessage .= "$numErrors error(s)";
Chris@4 370 if ($numErrors !== 0) {
Chris@4 371 $foundString .= 'error(s)';
Chris@4 372 $errors .= implode(PHP_EOL.' -> ', $problems['found_errors']);
Chris@4 373 }
Chris@4 374
Chris@4 375 if ($expectedWarnings !== $numWarnings) {
Chris@4 376 $expectedMessage .= ' and ';
Chris@4 377 $foundMessage .= ' and ';
Chris@4 378 if ($numWarnings !== 0) {
Chris@4 379 if ($foundString !== '') {
Chris@4 380 $foundString .= ' and ';
Chris@4 381 }
Chris@4 382 }
Chris@4 383 }
Chris@4 384 }
Chris@4 385
Chris@4 386 if ($expectedWarnings !== $numWarnings) {
Chris@4 387 $expectedMessage .= "$expectedWarnings warning(s)";
Chris@4 388 $foundMessage .= "$numWarnings warning(s)";
Chris@4 389 if ($numWarnings !== 0) {
Chris@4 390 $foundString .= 'warning(s)';
Chris@4 391 if (empty($errors) === false) {
Chris@4 392 $errors .= PHP_EOL.' -> ';
Chris@4 393 }
Chris@4 394
Chris@4 395 $errors .= implode(PHP_EOL.' -> ', $problems['found_warnings']);
Chris@4 396 }
Chris@4 397 }
Chris@4 398
Chris@4 399 $fullMessage = "$lineMessage $expectedMessage $foundMessage.";
Chris@4 400 if ($errors !== '') {
Chris@4 401 $fullMessage .= " The $foundString found were:".PHP_EOL." -> $errors";
Chris@4 402 }
Chris@4 403
Chris@4 404 $failureMessages[] = $fullMessage;
Chris@4 405 }//end if
Chris@4 406 }//end foreach
Chris@4 407
Chris@4 408 return $failureMessages;
Chris@4 409
Chris@4 410 }//end generateFailureMessages()
Chris@4 411
Chris@4 412
Chris@4 413 /**
Chris@4 414 * Get a list of CLI values to set before the file is tested.
Chris@4 415 *
Chris@4 416 * @param string $filename The name of the file being tested.
Chris@4 417 * @param \PHP_CodeSniffer\Config $config The config data for the run.
Chris@4 418 *
Chris@4 419 * @return void
Chris@4 420 */
Chris@4 421 public function setCliValues($filename, $config)
Chris@4 422 {
Chris@4 423 return;
Chris@4 424
Chris@4 425 }//end setCliValues()
Chris@4 426
Chris@4 427
Chris@4 428 /**
Chris@4 429 * Returns the lines where errors should occur.
Chris@4 430 *
Chris@4 431 * The key of the array should represent the line number and the value
Chris@4 432 * should represent the number of errors that should occur on that line.
Chris@4 433 *
Chris@4 434 * @return array<int, int>
Chris@4 435 */
Chris@4 436 abstract protected function getErrorList();
Chris@4 437
Chris@4 438
Chris@4 439 /**
Chris@4 440 * Returns the lines where warnings should occur.
Chris@4 441 *
Chris@4 442 * The key of the array should represent the line number and the value
Chris@4 443 * should represent the number of warnings that should occur on that line.
Chris@4 444 *
Chris@4 445 * @return array<int, int>
Chris@4 446 */
Chris@4 447 abstract protected function getWarningList();
Chris@4 448
Chris@4 449
Chris@4 450 }//end class