FilesystemTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Test\Unit\Model;
  7. use Magento\Deploy\Model\Filesystem as DeployFilesystem;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\ShellInterface;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. use Magento\Store\Model\Config\StoreView;
  14. use Magento\User\Model\ResourceModel\User\Collection;
  15. use Magento\User\Model\User;
  16. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Magento\Framework\Validator\Locale;
  19. use Magento\Framework\Setup\Lists;
  20. /**
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class FilesystemTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var StoreView|MockObject
  27. */
  28. private $storeView;
  29. /**
  30. * @var ShellInterface|MockObject
  31. */
  32. private $shell;
  33. /**
  34. * @var OutputInterface|MockObject
  35. */
  36. private $output;
  37. /**
  38. * @var Filesystem|MockObject
  39. */
  40. private $filesystem;
  41. /**
  42. * @var WriteInterface|MockObject
  43. */
  44. private $directoryWrite;
  45. /**
  46. * @var Collection|MockObject
  47. */
  48. private $userCollection;
  49. /**
  50. * @var ObjectManagerInterface|MockObject
  51. */
  52. private $objectManager;
  53. /**
  54. * @var DeployFilesystem
  55. */
  56. private $deployFilesystem;
  57. /**
  58. * @var string
  59. */
  60. private $cmdPrefix;
  61. /**
  62. * @inheritdoc
  63. */
  64. protected function setUp()
  65. {
  66. $objectManager = new ObjectManager($this);
  67. $this->storeView = $this->getMockBuilder(StoreView::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->shell = $this->getMockBuilder(ShellInterface::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->output = $this->getMockBuilder(OutputInterface::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->filesystem = $this->getMockBuilder(Filesystem::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->directoryWrite = $this->getMockBuilder(WriteInterface::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->filesystem->method('getDirectoryWrite')
  86. ->willReturn($this->directoryWrite);
  87. $this->userCollection = $this->getMockBuilder(Collection::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $lists = $this->getMockBuilder(Lists::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $lists->method('getLocaleList')
  94. ->willReturn([
  95. 'fr_FR' => 'France',
  96. 'de_DE' => 'Germany',
  97. 'nl_NL' => 'Netherlands',
  98. 'en_US' => 'USA',
  99. ]);
  100. $locale = $objectManager->getObject(Locale::class, ['lists' => $lists]);
  101. $this->deployFilesystem = $objectManager->getObject(
  102. DeployFilesystem::class,
  103. [
  104. 'storeView' => $this->storeView,
  105. 'shell' => $this->shell,
  106. 'filesystem' => $this->filesystem,
  107. 'userCollection' => $this->userCollection,
  108. 'locale' => $locale,
  109. ]
  110. );
  111. $this->cmdPrefix = PHP_BINARY . ' -f ' . BP . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'magento ';
  112. }
  113. /**
  114. * @throws \Magento\Framework\Exception\LocalizedException
  115. */
  116. public function testRegenerateStatic()
  117. {
  118. $storeLocales = ['fr_FR', 'de_DE', 'nl_NL'];
  119. $this->storeView->method('retrieveLocales')
  120. ->willReturn($storeLocales);
  121. $setupDiCompileCmd = $this->cmdPrefix . 'setup:di:compile';
  122. $this->initAdminLocaleMock('en_US');
  123. $usedLocales = ['fr_FR', 'de_DE', 'nl_NL', 'en_US'];
  124. $cacheFlushCmd = $this->cmdPrefix . 'cache:flush';
  125. $staticContentDeployCmd = $this->cmdPrefix . 'setup:static-content:deploy -f '
  126. . implode(' ', $usedLocales);
  127. $this->shell
  128. ->expects($this->exactly(4))
  129. ->method('execute')
  130. ->withConsecutive([$cacheFlushCmd], [$setupDiCompileCmd], [$cacheFlushCmd], [$staticContentDeployCmd]);
  131. $this->output->expects(self::at(0))
  132. ->method('writeln')
  133. ->with('Starting compilation');
  134. $this->output->expects(self::at(2))
  135. ->method('writeln')
  136. ->with('Compilation complete');
  137. $this->output->expects(self::at(3))
  138. ->method('writeln')
  139. ->with('Starting deployment of static content');
  140. $this->output->expects(self::at(5))
  141. ->method('writeln')
  142. ->with('Deployment of static content complete');
  143. $this->deployFilesystem->regenerateStatic($this->output);
  144. }
  145. /**
  146. * Checks a case when configuration contains incorrect locale code.
  147. *
  148. * @return void
  149. * @expectedException \InvalidArgumentException
  150. * @expectedExceptionMessage ;echo argument has invalid value, run info:language:list for list of available locales
  151. * @throws \Magento\Framework\Exception\LocalizedException
  152. */
  153. public function testGenerateStaticForNotAllowedStoreViewLocale()
  154. {
  155. $storeLocales = ['fr_FR', 'de_DE', ';echo'];
  156. $this->storeView->method('retrieveLocales')
  157. ->willReturn($storeLocales);
  158. $this->initAdminLocaleMock('en_US');
  159. $this->deployFilesystem->regenerateStatic($this->output);
  160. }
  161. /**
  162. * Checks as case when admin locale is incorrect.
  163. *
  164. * @return void
  165. * @expectedException \InvalidArgumentException
  166. * @expectedExceptionMessage ;echo argument has invalid value, run info:language:list for list of available locales
  167. * @throws \Magento\Framework\Exception\LocalizedException
  168. */
  169. public function testGenerateStaticForNotAllowedAdminLocale()
  170. {
  171. $storeLocales = ['fr_FR', 'de_DE', 'en_US'];
  172. $this->storeView->method('retrieveLocales')
  173. ->willReturn($storeLocales);
  174. $this->initAdminLocaleMock(';echo');
  175. $this->deployFilesystem->regenerateStatic($this->output);
  176. }
  177. /**
  178. * Initializes admin user locale.
  179. *
  180. * @param string $locale
  181. * @return void
  182. */
  183. private function initAdminLocaleMock($locale)
  184. {
  185. /** @var User|MockObject $user */
  186. $user = $this->getMockBuilder(User::class)
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $user->method('getInterfaceLocale')
  190. ->willReturn($locale);
  191. $this->userCollection->method('getIterator')
  192. ->willReturn(new \ArrayIterator([$user]));
  193. }
  194. }