You are here

final public function CoderSniffUnitTest::testSniff in Coder 8.2

Same name and namespace in other branches
  1. 8.3 tests/Drupal/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::testSniff()
  2. 8.3.x tests/Drupal/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::testSniff()

Tests the extending classes Sniff class.

@preserveGlobalState disabled

Return value

void

Throws

PHPUnit_Framework_Error

File

coder_sniffer/Drupal/Test/CoderSniffUnitTest.php, line 128

Class

CoderSniffUnitTest

Namespace

Drupal\Test

Code

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
    ->allSniffCodes()) {
    list($standardName) = explode('\\', get_class($this));
  }
  $testFileBase = $this->rootDir . $standardName . DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . $categoryName . DIRECTORY_SEPARATOR . $sniffName . 'UnitTest.';
  $this->standardsDir = $this->rootDir . $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 = array();
  $config->standards = array(
    $this->standardsDir,
  );
  $failureMessages = array();
  foreach ($testFiles as $testFile) {

    // Setup to test the selected Sniff.
    if ($this
      ->allSniffCodes()) {
      $config->sniffs = [];
    }
    else {
      $config->sniffs = array(
        $sniffCode,
      );
    }
    $ruleset = new Ruleset($config);
    $GLOBALS['PHP_CODESNIFFER_RULESET'] = $ruleset;
    $filename = basename($testFile);
    $oldConfig = $config
      ->getSettings();
    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());
    }
    $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}";
        }
      }
    }

    // Restore the config.
    $config
      ->setSettings($oldConfig);
  }

  //end foreach
  if (empty($failureMessages) === false) {
    $this
      ->fail(implode(PHP_EOL, $failureMessages));
  }
}