DeployStaticContentCommandTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Console\Command;
  7. use Magento\Deploy\Console\DeployStaticOptions;
  8. use Magento\Framework\App\DeploymentConfig\FileReader;
  9. use Magento\Framework\App\DeploymentConfig\Writer;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\Console\Cli;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. use Magento\Deploy\Console\ConsoleLoggerFactory;
  19. use Magento\Setup\Model\ObjectManagerProvider;
  20. use Magento\Deploy\Console\ConsoleLogger;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class DeployStaticContentCommandTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * @var CommandTester
  32. */
  33. private $commandTester;
  34. /**
  35. * @var Filesystem
  36. */
  37. private $filesystem;
  38. /**
  39. * @var ConfigFilePool
  40. */
  41. private $configFilePool;
  42. /**
  43. * @var FileReader
  44. */
  45. private $reader;
  46. /**
  47. * @var Writer
  48. */
  49. private $writer;
  50. /**
  51. * @var array
  52. */
  53. private $config;
  54. /**
  55. * @var array
  56. */
  57. private $envConfig;
  58. /**
  59. * @var StoreManagerInterface
  60. */
  61. private $storeManager;
  62. /**
  63. * @inheritdoc
  64. */
  65. protected function setUp()
  66. {
  67. $this->objectManager = Bootstrap::getObjectManager();
  68. $this->reader = $this->objectManager->get(FileReader::class);
  69. $this->writer = $this->objectManager->get(Writer::class);
  70. $this->filesystem = $this->objectManager->get(Filesystem::class);
  71. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  72. $this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
  73. $this->config = $this->loadConfig();
  74. $this->envConfig = $this->loadEnvConfig();
  75. $this->clearStaticFiles();
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public function tearDown()
  81. {
  82. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  83. $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
  84. "<?php\n return [];\n"
  85. );
  86. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  87. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  88. "<?php\n return [];\n"
  89. );
  90. $this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
  91. $this->writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
  92. $this->clearStaticFiles();
  93. $this->storeManager->reinitStores();
  94. }
  95. /**
  96. * Clear pub/static and var/view_preprocessed directories
  97. *
  98. * @return void
  99. */
  100. private function clearStaticFiles()
  101. {
  102. $this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
  103. $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->delete(DirectoryList::TMP_MATERIALIZATION_DIR);
  104. }
  105. public function testDeployStaticWithoutDbConnection()
  106. {
  107. // emulate app:config:dump command
  108. $newData = array_replace_recursive(
  109. $this->config,
  110. require __DIR__ . '/_files/config/dump_config.php'
  111. );
  112. $this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $newData], true);
  113. // remove application environment config for emulate work without db
  114. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  115. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  116. "<?php\n return [];\n"
  117. );
  118. $this->storeManager->reinitStores();
  119. $this->commandTester = new CommandTester($this->getStaticContentDeployCommand());
  120. $this->commandTester->execute(
  121. ['--force' => 1, '--' . DeployStaticOptions::THEME => ['Magento/blank']]
  122. );
  123. $commandOutput = $this->commandTester->getDisplay();
  124. $this->assertEquals(Cli::RETURN_SUCCESS, $this->commandTester->getStatusCode());
  125. $this->assertContains('Execution time:', $commandOutput);
  126. $this->assertContains('frontend/Magento/blank/en_US', $commandOutput);
  127. $this->assertNotContains('frontend/Magento/luma/en_US', $commandOutput);
  128. $this->assertNotContains('adminhtml/Magento/backend', $commandOutput);
  129. }
  130. /**
  131. * @return array
  132. */
  133. private function loadConfig()
  134. {
  135. return $this->reader->load(ConfigFilePool::APP_CONFIG);
  136. }
  137. /**
  138. * @return array
  139. */
  140. private function loadEnvConfig()
  141. {
  142. return $this->reader->load(ConfigFilePool::APP_ENV);
  143. }
  144. /**
  145. * Create DeployStaticContentCommand instance with mocked loggers
  146. *
  147. * @return DeployStaticContentCommand
  148. */
  149. private function getStaticContentDeployCommand()
  150. {
  151. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  152. $consoleLoggerFactoryMock = $this->getMockBuilder(ConsoleLoggerFactory::class)
  153. ->setMethods(['getLogger'])
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $consoleLoggerFactoryMock
  157. ->method('getLogger')
  158. ->will($this->returnCallback(
  159. function ($output) use ($objectManager) {
  160. return $objectManager->create(ConsoleLogger::class, ['output' => $output]);
  161. }
  162. ));
  163. $objectManagerProviderMock = $this->getMockBuilder(ObjectManagerProvider::class)
  164. ->setMethods(['get'])
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $objectManagerProviderMock
  168. ->method('get')
  169. ->willReturn(\Magento\TestFramework\Helper\Bootstrap::getObjectManager());
  170. $deployStaticContentCommand = $objectManager->create(
  171. DeployStaticContentCommand::class,
  172. [
  173. 'consoleLoggerFactory' => $consoleLoggerFactoryMock,
  174. 'objectManagerProvider' => $objectManagerProviderMock
  175. ]
  176. );
  177. return $deployStaticContentCommand;
  178. }
  179. }