/** * Add all sniff unit tests into a test suite. * * Sniff unit tests are found by recursing through the 'Tests' directory * of each installed coding standard. * * @return PHPUnit_Framework_TestSuite */ public static function suite() { $suite = new PHPUnit_Framework_TestSuite('SQLI CodeSniffer Standards'); $isInstalled = !is_file(dirname(__FILE__) . '/../../CodeSniffer.php'); if ($isInstalled) { $standardsDir = ''; } else { // We have not been installed. // TODO : this folders should be directly in Standards - fix with package.xml ! $standardsDir = realpath(dirname(__FILE__) . '/../../CodeSniffer/Standards'); } $standards = SQLI_CodeSniffer::getInstalledStandards(true, $standardsDir); foreach ($standards as $standard) { if ($isInstalled) { // TODO : this folders should be directly in Standards - fix with package.xml ! $standardDir = realpath(dirname(__FILE__) . '/../../CodeSniffer/Standards/' . $standard . '/Tests/'); } else { $standardDir = realpath($standardsDir . '/' . $standard . '/Tests/'); } if (!is_dir($standardDir)) { // No tests for this standard. continue; } $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($standardDir)); foreach ($di as $file) { // Skip hidden files. if (substr($file->getFilename(), 0, 1) === '.') { continue; } // Tests must have the extention 'php'. $parts = explode('.', $file); $ext = array_pop($parts); if ($ext !== 'php') { continue; } $filePath = realpath($file->getPathname()); if ($isInstalled === false) { $className = str_replace($standardDir . DIRECTORY_SEPARATOR, '', $filePath); } else { // TODO : fix this with package.xml ! $cutposition = strpos($filePath, '/CodeSniffer/Standards/'); $className = substr($filePath, $cutposition + strlen('/CodeSniffer/Standards/')); } $className = substr($className, 0, -4); $className = str_replace(DIRECTORY_SEPARATOR, '_', $className); if ($isInstalled === false) { $className = $standard . '_Tests_' . $className; } $niceName = substr($className, strrpos($className, '_') + 1, -8); include_once $filePath; $class = new $className($niceName); $suite->addTest($class); } } return $suite; }
/** * 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)); } }
/** * 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']); }