annotate vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php @ 19:fa3358dc1485 tip

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