SourceThemeDeployCommandTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Test\Unit\Console\Command;
  7. use Magento\Framework\Validator\Locale;
  8. use Magento\Framework\View\Asset\Repository;
  9. use Magento\Framework\App\View\Asset\Publisher;
  10. use Magento\Framework\View\Asset\LocalInterface;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Magento\Developer\Console\Command\SourceThemeDeployCommand;
  14. /**
  15. * Class SourceThemeDeployCommandTest
  16. *
  17. * @see \Magento\Developer\Console\Command\SourceThemeDeployCommand
  18. */
  19. class SourceThemeDeployCommandTest extends \PHPUnit\Framework\TestCase
  20. {
  21. const AREA_TEST_VALUE = 'area-test-value';
  22. const LOCALE_TEST_VALUE = 'locale-test-value';
  23. const THEME_TEST_VALUE = 'Vendor/theme';
  24. const THEME_INCORRECT_FORMAT_VALUE = 'theme-value';
  25. const THEME_NONEXISTING_VALUE = 'NonExistentVendor/theme';
  26. const TYPE_TEST_VALUE = 'type-test-value';
  27. const FILE_TEST_VALUE = 'file-test-value/test/file';
  28. /**
  29. * @var SourceThemeDeployCommand
  30. */
  31. private $sourceThemeDeployCommand;
  32. /**
  33. * @var Locale|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $validatorMock;
  36. /**
  37. * @var Publisher|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $assetPublisherMock;
  40. /**
  41. * @var Repository|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $assetRepositoryMock;
  44. /**
  45. * Set up
  46. */
  47. protected function setUp()
  48. {
  49. $this->validatorMock = $this->getMockBuilder(Locale::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->assetPublisherMock = $this->getMockBuilder(Publisher::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->assetRepositoryMock = $this->getMockBuilder(Repository::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->sourceThemeDeployCommand = new SourceThemeDeployCommand(
  59. $this->validatorMock,
  60. $this->assetPublisherMock,
  61. $this->assetRepositoryMock
  62. );
  63. }
  64. /**
  65. * Run test for execute method
  66. */
  67. public function testExecute()
  68. {
  69. /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  70. $outputMock = $this->getMockBuilder(OutputInterface::class)
  71. ->getMockForAbstractClass();
  72. $assetMock = $this->getMockBuilder(LocalInterface::class)
  73. ->getMockForAbstractClass();
  74. $this->validatorMock->expects(self::once())
  75. ->method('isValid')
  76. ->with(self::LOCALE_TEST_VALUE)
  77. ->willReturn(true);
  78. $message = sprintf(
  79. '<info>Processed Area: %s, Locale: %s, Theme: %s, File type: %s.</info>',
  80. self::AREA_TEST_VALUE,
  81. self::LOCALE_TEST_VALUE,
  82. self::THEME_TEST_VALUE,
  83. self::TYPE_TEST_VALUE
  84. );
  85. $outputMock->expects(self::at(0))
  86. ->method('writeln')
  87. ->with($message);
  88. $outputMock->expects(self::at(1))
  89. ->method('writeln')
  90. ->with('<comment>-> file-test-value/test/file</comment>');
  91. $outputMock->expects(self::at(2))
  92. ->method('writeln')
  93. ->with('<info>Successfully processed.</info>');
  94. $this->assetRepositoryMock->expects(self::once())
  95. ->method('createAsset')
  96. ->with(
  97. 'file-test-value/test' . DIRECTORY_SEPARATOR . 'file' . '.' . self::TYPE_TEST_VALUE,
  98. [
  99. 'area' => self::AREA_TEST_VALUE,
  100. 'theme' => self::THEME_TEST_VALUE,
  101. 'locale' => self::LOCALE_TEST_VALUE,
  102. ]
  103. )->willReturn($assetMock);
  104. $this->assetPublisherMock->expects(self::once())
  105. ->method('publish')
  106. ->with($assetMock);
  107. $assetMock->expects(self::once())
  108. ->method('getFilePath')
  109. ->willReturn(self::FILE_TEST_VALUE);
  110. $this->sourceThemeDeployCommand->run($this->getInputMock(), $outputMock);
  111. }
  112. /**
  113. * Run test for execute method with incorrect theme value
  114. *
  115. * @expectedException \InvalidArgumentException
  116. * @expectedExceptionMessage Value "theme-value" of the option "theme" has invalid format. The format should be
  117. */
  118. public function testExecuteIncorrectThemeFormat()
  119. {
  120. /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  121. $outputMock = $this->getMockBuilder(OutputInterface::class)
  122. ->getMockForAbstractClass();
  123. $this->validatorMock->expects(self::once())
  124. ->method('isValid')
  125. ->with(self::LOCALE_TEST_VALUE)
  126. ->willReturn(true);
  127. $valueMap = [
  128. ['area', self::AREA_TEST_VALUE],
  129. ['locale', self::LOCALE_TEST_VALUE],
  130. ['theme', self::THEME_INCORRECT_FORMAT_VALUE],
  131. ['type', self::TYPE_TEST_VALUE]
  132. ];
  133. $this->sourceThemeDeployCommand->run(
  134. $this->getInputMock($valueMap),
  135. $outputMock
  136. );
  137. }
  138. /**
  139. * Run test for execute method with non existing theme
  140. *
  141. * @expectedException \InvalidArgumentException
  142. * @expectedExceptionMessage Verify entered values of the argument and options.
  143. */
  144. public function testExecuteNonExistingValue()
  145. {
  146. /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  147. $outputMock = $this->getMockBuilder(OutputInterface::class)
  148. ->getMockForAbstractClass();
  149. $assetMock = $this->getMockBuilder(LocalInterface::class)
  150. ->getMockForAbstractClass();
  151. $this->validatorMock->expects(self::once())
  152. ->method('isValid')
  153. ->with(self::LOCALE_TEST_VALUE)
  154. ->willReturn(true);
  155. $this->assetRepositoryMock->expects(self::once())
  156. ->method('createAsset')
  157. ->with(
  158. 'file-test-value/test' . DIRECTORY_SEPARATOR . 'file' . '.' . self::TYPE_TEST_VALUE,
  159. [
  160. 'area' => self::AREA_TEST_VALUE,
  161. 'theme' => self::THEME_NONEXISTING_VALUE,
  162. 'locale' => self::LOCALE_TEST_VALUE,
  163. ]
  164. )->willReturn($assetMock);
  165. $this->assetPublisherMock->expects(self::once())
  166. ->method('publish')
  167. ->with($assetMock)
  168. ->willThrowException(new \Magento\Framework\View\Asset\File\NotFoundException);
  169. $valueMap = [
  170. ['area', self::AREA_TEST_VALUE],
  171. ['locale', self::LOCALE_TEST_VALUE],
  172. ['theme', self::THEME_NONEXISTING_VALUE],
  173. ['type', self::TYPE_TEST_VALUE]
  174. ];
  175. $this->sourceThemeDeployCommand->run(
  176. $this->getInputMock($valueMap),
  177. $outputMock
  178. );
  179. }
  180. /**
  181. * @return InputInterface|\PHPUnit_Framework_MockObject_MockObject
  182. */
  183. private function getInputMock(array $valueMap = [])
  184. {
  185. $inputMock = $this->getMockBuilder(InputInterface::class)
  186. ->getMockForAbstractClass();
  187. $defaultValueMap = [
  188. ['area', self::AREA_TEST_VALUE],
  189. ['locale', self::LOCALE_TEST_VALUE],
  190. ['theme', self::THEME_TEST_VALUE],
  191. ['type', self::TYPE_TEST_VALUE]
  192. ];
  193. $valueMap = empty($valueMap) ? $defaultValueMap : $valueMap;
  194. $inputMock->expects(self::exactly(4))
  195. ->method('getOption')
  196. ->willReturnMap(
  197. $valueMap
  198. );
  199. $inputMock->expects(self::once())
  200. ->method('getArgument')
  201. ->with('file')
  202. ->willReturn([self::FILE_TEST_VALUE]);
  203. return $inputMock;
  204. }
  205. }