/**
  * Tests the extending classes Sniff class.
  *
  * @return void
  * @throws PHPUnit_Framework_Error
  */
 protected final function runTest()
 {
     // Skip this test if we can't run in this environment.
     if ($this->shouldSkipTest() === true) {
         $this->markTestSkipped();
     }
     // The basis for determining file locations.
     $basename = substr(get_class($this), 0, -8);
     // The name of the coding standard we are testing.
     $standardName = substr($basename, 0, strpos($basename, '_'));
     $testFiles = $this->getTestFiles($basename, $standardName);
     // The class name of the sniff we are testing.
     $sniffClass = str_replace('_Tests_', '_Sniffs_', $basename) . 'Sniff';
     $failureMessages = array();
     $multiFileSniff = false;
     foreach ($testFiles as $testFile) {
         try {
             self::$phpcs->process($testFile, $standardName, array($sniffClass));
         } catch (Exception $e) {
             $this->fail('An unexpected exception has been caught: ' . $e->getMessage());
         }
         // After processing a file, check if the sniff was actually
         // a multi-file sniff (i.e., had no indivdual file sniffs).
         // If it is, we can skip checking of the other files and
         // do a single multi-file check.
         $sniffs = self::$phpcs->getTokenSniffs();
         if (empty($sniffs['file']) === true) {
             $multiFileSniff = true;
             break;
         }
         $files = self::$phpcs->getFiles();
         $file = array_pop($files);
         $failures = $this->generateFailureMessages($file, $testFile);
         $failureMessages = array_merge($failureMessages, $failures);
     }
     //end foreach
     if ($multiFileSniff === true) {
         try {
             self::$phpcs->process($testFiles, $standardName, array($sniffClass));
         } catch (Exception $e) {
             $this->fail('An unexpected exception has been caught: ' . $e->getMessage());
         }
         $files = self::$phpcs->getFiles();
         foreach ($files as $file) {
             $failures = $this->generateFailureMessages($file);
             $failureMessages = array_merge($failureMessages, $failures);
         }
     }
     if (empty($failureMessages) === false) {
         $this->fail(implode(PHP_EOL, $failureMessages));
     }
 }
Beispiel #2
0
 /**
  * Runs PHP_CodeSniffer over files are directories.
  * 
  * Duplicated to allow SQLI_CodeSniffer instanciation
  *
  * @param array $values An array of values determined from CLI args.
  *
  * @return int The number of error and warning messages shown.
  * @see getCommandLineValues()
  */
 public function process($values = array())
 {
     if (empty($values) === true) {
         $values = $this->getCommandLineValues();
     }
     if ($values['generator'] !== '') {
         $phpcs = new SQLI_CodeSniffer($values['verbosity']);
         $phpcs->generateDocs($values['standard'], $values['files'], $values['generator']);
         exit(0);
     }
     if (empty($values['files']) === true) {
         echo 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL;
         $this->printUsage();
         exit(2);
     }
     $values['standard'] = $this->validateStandard($values['standard']);
     if (SQLI_CodeSniffer::isInstalledStandard($values['standard']) === false) {
         // They didn't select a valid coding standard, so help them
         // out by letting them know which standards are installed.
         echo 'ERROR: the "' . $values['standard'] . '" coding standard is not installed. ';
         $this->printInstalledStandards();
         exit(2);
     }
     $phpcs = new SQLI_CodeSniffer($values['verbosity'], $values['tabWidth']);
     // Set file extensions if they were specified. Otherwise,
     // let PHP_CodeSniffer decide on the defaults.
     if (empty($values['extensions']) === false) {
         $phpcs->setAllowedFileExtensions($values['extensions']);
     }
     // Set ignore patterns if they were specified.
     if (empty($values['ignored']) === false) {
         $phpcs->setIgnorePatterns($values['ignored']);
     }
     $phpcs->process($values['files'], $values['standard'], $values['sniffs'], $values['local']);
     return $this->printReport($phpcs, $values['standard'], $values['report'], $values['showLevel'], $values['showSources'], $values['reportFile']);
 }