SourceThemeDeployCommandTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. /**
  11. * Class SourceThemeDeployCommandTest
  12. *
  13. * @see \Magento\Developer\Console\Command\SourceThemeDeployCommand
  14. */
  15. class SourceThemeDeployCommandTest extends \PHPUnit\Framework\TestCase
  16. {
  17. const PUB_STATIC_DIRECTORY = 'pub/static';
  18. const AREA_TEST_VALUE = 'frontend';
  19. const LOCALE_TEST_VALUE = 'en_US';
  20. const THEME_TEST_VALUE = 'Magento/luma';
  21. const TYPE_TEST_VALUE = 'less';
  22. /**
  23. * @var SourceThemeDeployCommand
  24. */
  25. private $command;
  26. /**
  27. * @var string
  28. */
  29. private $pubStatic;
  30. /**
  31. * @var array
  32. */
  33. private $compiledFiles = ['css/styles-m', 'css/styles-l'];
  34. /**
  35. * Set up
  36. */
  37. protected function setUp()
  38. {
  39. global $installDir;
  40. $this->pubStatic = $installDir . DIRECTORY_SEPARATOR . self::PUB_STATIC_DIRECTORY;
  41. $this->command = Bootstrap::getObjectManager()->get(SourceThemeDeployCommand::class);
  42. }
  43. /**
  44. * Run test for execute method
  45. */
  46. public function testExecute()
  47. {
  48. $error = [];
  49. /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  50. $outputMock = $this->getMockBuilder(OutputInterface::class)
  51. ->getMockForAbstractClass();
  52. $this->clearStaticDirectory();
  53. $this->command->run($this->getInputMock(), $outputMock);
  54. /** @var \SplFileInfo $file */
  55. foreach ($this->collectFiles($this->pubStatic) as $file) {
  56. $fileInfo = pathinfo($file->getFilename());
  57. if (!in_array('css/' . $fileInfo['filename'], $this->compiledFiles, true)
  58. && !$file->isLink()
  59. ) {
  60. $error[] = 'Bad file -> ' . $file->getFilename() . PHP_EOL;
  61. }
  62. }
  63. $this->clearStaticDirectory();
  64. self::assertEmpty($error, implode($error));
  65. }
  66. /**
  67. * @return void
  68. */
  69. private function clearStaticDirectory()
  70. {
  71. if (is_dir($this->pubStatic)) {
  72. /** @var \SplFileInfo $file */
  73. foreach ($this->collectFiles($this->pubStatic) as $file) {
  74. @unlink($file->getPathname());
  75. }
  76. }
  77. }
  78. /**
  79. * @param string $path
  80. * @return \RegexIterator|array
  81. */
  82. private function collectFiles($path)
  83. {
  84. $flags = \FilesystemIterator::CURRENT_AS_FILEINFO
  85. | \FilesystemIterator::SKIP_DOTS;
  86. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $flags));
  87. return new \RegexIterator(
  88. $iterator,
  89. '#\.less$#',
  90. \RegexIterator::MATCH,
  91. \RegexIterator::USE_KEY
  92. );
  93. }
  94. /**
  95. * @return InputInterface|\PHPUnit_Framework_MockObject_MockObject
  96. */
  97. private function getInputMock()
  98. {
  99. $inputMock = $this->getMockBuilder(InputInterface::class)
  100. ->getMockForAbstractClass();
  101. $inputMock->expects(self::exactly(4))
  102. ->method('getOption')
  103. ->willReturnMap(
  104. [
  105. ['area', self::AREA_TEST_VALUE],
  106. ['locale', self::LOCALE_TEST_VALUE],
  107. ['theme', self::THEME_TEST_VALUE],
  108. ['type', self::TYPE_TEST_VALUE]
  109. ]
  110. );
  111. $inputMock->expects(self::once())
  112. ->method('getArgument')
  113. ->with('file')
  114. ->willReturn($this->compiledFiles);
  115. return $inputMock;
  116. }
  117. }