final public function CoderSniffUnitTest::testSniff in Coder 8.3.x
Same name and namespace in other branches
- 8.3 tests/Drupal/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::testSniff()
- 8.2 coder_sniffer/Drupal/Test/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::testSniff()
Tests the extending classes Sniff class.
@preserveGlobalState disabled
Return value
void
Throws
File
- tests/
Drupal/ CoderSniffUnitTest.php, line 135
Class
Namespace
Drupal\TestCode
public final function testSniff() {
// Skip this test if we can't run in this environment.
if ($this
->shouldSkipTest() === true) {
$this
->markTestSkipped();
}
$sniffCode = Common::getSniffCode(get_class($this));
list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode);
// In the case where we are running all the sniffs, the standard will
// be the root class name.
if ($this
->checkAllSniffCodes() !== false) {
list($standardName) = explode('\\', get_class($this));
}
$testFileBase = $this->rootDir . 'tests' . DIRECTORY_SEPARATOR . $standardName . DIRECTORY_SEPARATOR . $categoryName . DIRECTORY_SEPARATOR . $sniffName . 'UnitTest.';
$this->standardsDir = $this->rootDir . 'coder_sniffer' . DIRECTORY_SEPARATOR . $standardName . DIRECTORY_SEPARATOR;
// $testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.';
// Get a list of all test files to check.
$testFiles = $this
->getTestFiles($testFileBase);
$config = new Config();
$config->cache = false;
$GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config;
// $config->standards = array($standardName);
$config->ignored = [];
$config->standards = [
$this->standardsDir,
];
$failureMessages = [];
foreach ($testFiles as $testFile) {
// Setup to test the selected Sniff.
if ($this
->checkAllSniffCodes() !== false) {
$config->sniffs = [];
}
else {
$config->sniffs = [
$sniffCode,
];
}
$ruleset = new Ruleset($config);
$GLOBALS['PHP_CODESNIFFER_RULESET'] = $ruleset;
$filename = basename($testFile);
$oldConfig = $config
->getSettings();
$this
->setCliValues($filename, $config);
$phpcsFile = new LocalFile($testFile, $ruleset, $config);
$phpcsFile
->process();
$failures = $this
->generateFailureMessages($phpcsFile);
$failureMessages = array_merge($failureMessages, $failures);
if ($phpcsFile
->getFixableCount() > 0) {
// Reinit to use all sniffs.
$config->sniffs = [];
$ruleset = new Ruleset($config);
try {
$this
->setCliValues($filename, $config);
$phpcsFile = new LocalFile($testFile, $ruleset, $config);
$phpcsFile
->process();
} catch (RuntimeException $e) {
$this
->fail('An unexpected exception has been caught: ' . $e
->getMessage());
}
// Attempt to fix the errors.
$phpcsFile->fixer
->fixFile();
$fixable = $phpcsFile
->getFixableCount();
if ($fixable > 0) {
$failureMessages[] = "Failed to fix {$fixable} fixable violations in {$filename}";
}
// Check for a .fixed file to check for accuracy of fixes.
$fixedFile = $testFile . '.fixed';
if (file_exists($fixedFile) === true) {
$diff = $phpcsFile->fixer
->generateDiff($fixedFile);
if (trim($diff) !== '') {
$filename = basename($testFile);
$fixedFilename = basename($fixedFile);
$failureMessages[] = "Fixed version of {$filename} does not match expected version in {$fixedFilename}; the diff is\n{$diff}";
}
}
}
//end if
// Restore the config.
$config
->setSettings($oldConfig);
}
//end foreach
if (empty($failureMessages) === false) {
$this
->fail(implode(PHP_EOL, $failureMessages));
}
}