DeployStaticContentTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Test\Unit\Service;
  7. use Magento\Deploy\Package\Package;
  8. use Magento\Deploy\Process\Queue;
  9. use Magento\Deploy\Service\Bundle;
  10. use Magento\Deploy\Service\DeployPackage;
  11. use Magento\Deploy\Service\DeployRequireJsConfig;
  12. use Magento\Deploy\Service\DeployStaticContent;
  13. use Magento\Deploy\Process\QueueFactory;
  14. use Magento\Deploy\Service\DeployTranslationsDictionary;
  15. use Magento\Deploy\Service\MinifyTemplates;
  16. use Magento\Deploy\Strategy\CompactDeploy;
  17. use Magento\Deploy\Strategy\DeployStrategyFactory;
  18. use Magento\Framework\App\View\Deployment\Version\StorageInterface;
  19. use Magento\Framework\ObjectManagerInterface;
  20. use Psr\Log\LoggerInterface;
  21. use PHPUnit_Framework_MockObject_MockObject as Mock;
  22. /**
  23. * Static Content deploy service class unit tests
  24. *
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. */
  27. class DeployStaticContentTest extends \PHPUnit\Framework\TestCase
  28. {
  29. /**
  30. * @var DeployStaticContent|Mock
  31. */
  32. private $service;
  33. /**
  34. * @var DeployStrategyFactory|Mock
  35. */
  36. private $deployStrategyFactory;
  37. /**
  38. * @var QueueFactory|Mock
  39. */
  40. private $queueFactory;
  41. /**
  42. * @var LoggerInterface|Mock
  43. */
  44. private $logger;
  45. /**
  46. * @var ObjectManagerInterface|Mock
  47. */
  48. private $objectManager;
  49. /**
  50. * @var StorageInterface|Mock
  51. */
  52. private $versionStorage;
  53. protected function setUp()
  54. {
  55. $this->deployStrategyFactory = $this->createPartialMock(DeployStrategyFactory::class, ['create']);
  56. $this->queueFactory = $this->createPartialMock(QueueFactory::class, ['create']);
  57. $this->logger = $this->getMockForAbstractClass(
  58. LoggerInterface::class,
  59. [],
  60. '',
  61. false
  62. );
  63. $this->objectManager = $this->createPartialMock(ObjectManagerInterface::class, ['create', 'get', 'configure']);
  64. $this->versionStorage = $this->getMockForAbstractClass(
  65. StorageInterface::class,
  66. ['save'],
  67. '',
  68. false
  69. );
  70. $this->service = new DeployStaticContent(
  71. $this->objectManager,
  72. $this->logger,
  73. $this->versionStorage,
  74. $this->deployStrategyFactory,
  75. $this->queueFactory
  76. );
  77. }
  78. /**
  79. * @param array $options
  80. * @param string $expectedContentVersion
  81. * @dataProvider deployDataProvider
  82. */
  83. public function testDeploy($options, $expectedContentVersion)
  84. {
  85. $package = $this->createMock(Package::class);
  86. if ($options['refresh-content-version-only']) {
  87. $package->expects($this->never())->method('isVirtual');
  88. $package->expects($this->never())->method('getArea');
  89. $package->expects($this->never())->method('getTheme');
  90. $package->expects($this->never())->method('getLocale');
  91. } else {
  92. $package->expects($this->exactly(1))->method('isVirtual')->willReturn(false);
  93. $package->expects($this->exactly(3))->method('getArea')->willReturn('area');
  94. $package->expects($this->exactly(3))->method('getTheme')->willReturn('theme');
  95. $package->expects($this->exactly(3))->method('getLocale')->willReturn('locale');
  96. }
  97. $packages = ['package' => $package];
  98. if ($expectedContentVersion) {
  99. $this->versionStorage->expects($this->once())->method('save')->with($expectedContentVersion);
  100. } else {
  101. $this->versionStorage->expects($this->once())->method('save');
  102. }
  103. $queue = $this->getMockBuilder(Queue::class)
  104. ->disableOriginalConstructor()
  105. ->getMockForAbstractClass();
  106. if ($options['refresh-content-version-only']) {
  107. $this->queueFactory->expects($this->never())->method('create');
  108. } else {
  109. $this->queueFactory->expects($this->once())->method('create')->willReturn($queue);
  110. }
  111. $strategy = $this->getMockBuilder(CompactDeploy::class)
  112. ->setMethods(['deploy'])
  113. ->disableOriginalConstructor()
  114. ->getMockForAbstractClass();
  115. if ($options['refresh-content-version-only']) {
  116. $strategy->expects($this->never())->method('deploy');
  117. } else {
  118. $strategy->expects($this->once())->method('deploy')
  119. ->with($options)
  120. ->willReturn($packages);
  121. $this->deployStrategyFactory->expects($this->once())
  122. ->method('create')
  123. ->with('compact', ['queue' => $queue])
  124. ->willReturn($strategy);
  125. }
  126. $deployPackageService = $this->getMockBuilder(DeployPackage::class)
  127. ->disableOriginalConstructor()
  128. ->getMockForAbstractClass();
  129. $deployRjsConfig = $this->getMockBuilder(DeployRequireJsConfig::class)
  130. ->setMethods(['deploy'])
  131. ->disableOriginalConstructor()
  132. ->getMockForAbstractClass();
  133. $deployI18n = $this->getMockBuilder(DeployTranslationsDictionary::class)
  134. ->setMethods(['deploy'])
  135. ->disableOriginalConstructor()
  136. ->getMockForAbstractClass();
  137. $deployBundle = $this->getMockBuilder(Bundle::class)
  138. ->setMethods(['deploy'])
  139. ->disableOriginalConstructor()
  140. ->getMockForAbstractClass();
  141. $minifyTemplates = $this->getMockBuilder(MinifyTemplates::class)
  142. ->setMethods(['minifyTemplates'])
  143. ->disableOriginalConstructor()
  144. ->getMockForAbstractClass();
  145. if ($options['refresh-content-version-only']) {
  146. $this->objectManager->expects($this->never())->method('create');
  147. $this->objectManager->expects($this->never())->method('get');
  148. } else {
  149. $this->objectManager->expects($this->exactly(4))
  150. ->method('create')
  151. ->withConsecutive(
  152. [DeployPackage::class, ['logger' => $this->logger]],
  153. [DeployRequireJsConfig::class, ['logger' => $this->logger]],
  154. [DeployTranslationsDictionary::class, ['logger' => $this->logger]],
  155. [Bundle::class, ['logger' => $this->logger]]
  156. )
  157. ->willReturnOnConsecutiveCalls(
  158. $deployPackageService,
  159. $deployRjsConfig,
  160. $deployI18n,
  161. $deployBundle
  162. );
  163. $this->objectManager->expects($this->exactly(1))
  164. ->method('get')
  165. ->withConsecutive([MinifyTemplates::class])
  166. ->willReturnOnConsecutiveCalls($minifyTemplates);
  167. }
  168. $this->assertEquals(null, $this->service->deploy($options));
  169. }
  170. /**
  171. * @return array
  172. */
  173. public function deployDataProvider()
  174. {
  175. return [
  176. [
  177. [
  178. 'strategy' => 'compact',
  179. 'no-javascript' => false,
  180. 'no-html-minify' => false,
  181. 'refresh-content-version-only' => false,
  182. ],
  183. null // content version value should not be asserted in this case
  184. ],
  185. [
  186. [
  187. 'strategy' => 'compact',
  188. 'no-javascript' => false,
  189. 'no-html-minify' => false,
  190. 'refresh-content-version-only' => false,
  191. 'content-version' => '123456',
  192. ],
  193. '123456'
  194. ],
  195. [
  196. [
  197. 'refresh-content-version-only' => true,
  198. 'content-version' => '654321',
  199. ],
  200. '654321'
  201. ]
  202. ];
  203. }
  204. }