AllSniffs.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * A test class for testing all sniffs for installed standards.
  4. *
  5. * @author Greg Sherwood <gsherwood@squiz.net>
  6. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8. */
  9. namespace PHP_CodeSniffer\Tests\Standards;
  10. use PHP_CodeSniffer\Util\Standards;
  11. use PHP_CodeSniffer\Autoload;
  12. use PHPUnit\TextUI\TestRunner;
  13. use PHPUnit\Framework\TestSuite;
  14. class AllSniffs
  15. {
  16. /**
  17. * Prepare the test runner.
  18. *
  19. * @return void
  20. */
  21. public static function main()
  22. {
  23. TestRunner::run(self::suite());
  24. }//end main()
  25. /**
  26. * Add all sniff unit tests into a test suite.
  27. *
  28. * Sniff unit tests are found by recursing through the 'Tests' directory
  29. * of each installed coding standard.
  30. *
  31. * @return \PHPUnit\Framework\TestSuite
  32. */
  33. public static function suite()
  34. {
  35. $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'] = [];
  36. $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'] = [];
  37. $suite = new TestSuite('PHP CodeSniffer Standards');
  38. $isInstalled = !is_file(__DIR__.'/../../autoload.php');
  39. // Optionally allow for ignoring the tests for one or more standards.
  40. $ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS');
  41. if ($ignoreTestsForStandards === false) {
  42. $ignoreTestsForStandards = [];
  43. } else {
  44. $ignoreTestsForStandards = explode(',', $ignoreTestsForStandards);
  45. }
  46. $installedStandards = self::getInstalledStandardDetails();
  47. foreach ($installedStandards as $standard => $details) {
  48. Autoload::addSearchPath($details['path'], $details['namespace']);
  49. // If the test is running PEAR installed, the built-in standards
  50. // are split into different directories; one for the sniffs and
  51. // a different file system location for tests.
  52. if ($isInstalled === true && is_dir(dirname($details['path']).DIRECTORY_SEPARATOR.'Generic') === true) {
  53. $testPath = realpath(__DIR__.'/../../src/Standards/'.$standard);
  54. } else {
  55. $testPath = $details['path'];
  56. }
  57. if (in_array($standard, $ignoreTestsForStandards) === true) {
  58. continue;
  59. }
  60. $testsDir = $testPath.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
  61. if (is_dir($testsDir) === false) {
  62. // No tests for this standard.
  63. continue;
  64. }
  65. $di = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testsDir));
  66. foreach ($di as $file) {
  67. // Skip hidden files.
  68. if (substr($file->getFilename(), 0, 1) === '.') {
  69. continue;
  70. }
  71. // Tests must have the extension 'php'.
  72. $parts = explode('.', $file);
  73. $ext = array_pop($parts);
  74. if ($ext !== 'php') {
  75. continue;
  76. }
  77. $className = Autoload::loadFile($file->getPathname());
  78. $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $details['path'];
  79. $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$className] = $testsDir;
  80. $suite->addTestSuite($className);
  81. }
  82. }//end foreach
  83. return $suite;
  84. }//end suite()
  85. /**
  86. * Get the details of all coding standards installed.
  87. *
  88. * @return array
  89. * @see Standards::getInstalledStandardDetails()
  90. */
  91. protected static function getInstalledStandardDetails()
  92. {
  93. return Standards::getInstalledStandardDetails(true);
  94. }//end getInstalledStandardDetails()
  95. }//end class