LiveCodeTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Less;
  7. use Magento\Framework\App\Utility;
  8. use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
  9. use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\LessWrapper;
  10. use Magento\Framework\App\Utility\Files;
  11. use Magento\Test\Php\LiveCodeTest as PHPCodeTest;
  12. /**
  13. * Set of tests for static code style
  14. */
  15. class LiveCodeTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. private static $reportDir = '';
  21. /**
  22. * Setup basics for all tests
  23. *
  24. * @return void
  25. */
  26. public static function setUpBeforeClass()
  27. {
  28. self::$reportDir = BP . '/dev/tests/static/report';
  29. if (!is_dir(self::$reportDir)) {
  30. mkdir(self::$reportDir, 0770);
  31. }
  32. }
  33. /**
  34. * Run the magento specific coding standards on the code
  35. *
  36. * @return void
  37. */
  38. public function testCodeStyle()
  39. {
  40. $reportFile = self::$reportDir . '/csless_report.txt';
  41. $wrapper = new LessWrapper();
  42. $codeSniffer = new CodeSniffer(realpath(__DIR__ . '/_files/lesscs'), $reportFile, $wrapper);
  43. if (!$codeSniffer->canRun()) {
  44. $this->markTestSkipped('PHP Code Sniffer is not installed.');
  45. }
  46. $codeSniffer->setExtensions([LessWrapper::LESS_FILE_EXTENSION]);
  47. $fileList = PHPCodeTest::getWhitelist([LessWrapper::LESS_FILE_EXTENSION], __DIR__, __DIR__);
  48. $result = $codeSniffer->run($this->filterFiles($fileList));
  49. $report = file_exists($reportFile) ? file_get_contents($reportFile) : "";
  50. $this->assertEquals(
  51. 0,
  52. $result,
  53. "PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $report
  54. );
  55. }
  56. /**
  57. * Skip blacklisted files
  58. *
  59. * @param array $fileList
  60. * @return array
  61. * @throws \Exception
  62. */
  63. private function filterFiles(array $fileList)
  64. {
  65. $blackListFiles = Files::init()->readLists(__DIR__ . '/_files/blacklist/*.txt');
  66. $filter = function ($value) use ($blackListFiles) {
  67. return !in_array($value, $blackListFiles);
  68. };
  69. return array_filter($fileList, $filter);
  70. }
  71. }