SampleDataDeployCommandTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SampleData\Test\Unit\Console\Command;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\SampleData\Console\Command\SampleDataDeployCommand;
  9. use Magento\Setup\Model\PackagesAuth;
  10. use Symfony\Component\Console\Tester\CommandTester;
  11. class SampleDataDeployCommandTest extends AbstractSampleDataCommandTest
  12. {
  13. /**
  14. * @param bool $authExist True to test with existing auth.json, false without
  15. */
  16. protected function setupMocksForAuthFile($authExist)
  17. {
  18. $this->directoryWriteMock->expects($this->once())
  19. ->method('isExist')
  20. ->with(PackagesAuth::PATH_TO_AUTH_FILE)
  21. ->willReturn($authExist);
  22. $this->directoryWriteMock->expects($authExist ? $this->never() : $this->once())->method('writeFile')->with(
  23. PackagesAuth::PATH_TO_AUTH_FILE,
  24. '{}'
  25. );
  26. $this->filesystemMock->expects($this->once())
  27. ->method('getDirectoryWrite')
  28. ->with(DirectoryList::COMPOSER_HOME)
  29. ->willReturn($this->directoryWriteMock);
  30. }
  31. /**
  32. * @param array $sampleDataPackages
  33. * @param int $appRunResult - int 0 if everything went fine, or an error code
  34. * @param string $expectedMsg
  35. * @param bool $authExist
  36. * @return void
  37. *
  38. * @dataProvider processDataProvider
  39. */
  40. public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
  41. {
  42. $this->setupMocks($sampleDataPackages, '/path/to/composer.json', $appRunResult);
  43. $this->setupMocksForAuthFile($authExist);
  44. $commandTester = $this->createCommandTester();
  45. $commandTester->execute([]);
  46. $this->assertEquals($expectedMsg, $commandTester->getDisplay());
  47. }
  48. /**
  49. * @param array $sampleDataPackages
  50. * @param int $appRunResult - int 0 if everything went fine, or an error code
  51. * @param string $expectedMsg
  52. * @param bool $authExist
  53. * @return void
  54. *
  55. * @dataProvider processDataProvider
  56. */
  57. public function testExecuteWithNoUpdate(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
  58. {
  59. $this->setupMocks(
  60. $sampleDataPackages,
  61. '/path/to/composer.json',
  62. $appRunResult,
  63. ['--no-update' => 1]
  64. );
  65. $this->setupMocksForAuthFile($authExist);
  66. $commandInput = ['--no-update' => 1];
  67. $commandTester = $this->createCommandTester();
  68. $commandTester->execute($commandInput);
  69. $this->assertEquals($expectedMsg, $commandTester->getDisplay());
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function processDataProvider()
  75. {
  76. return [
  77. 'No sample data found' => [
  78. 'sampleDataPackages' => [],
  79. 'appRunResult' => 1,
  80. 'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
  81. 'authExist' => true,
  82. ],
  83. 'No auth.json found' => [
  84. 'sampleDataPackages' => [
  85. 'magento/module-cms-sample-data' => '1.0.0-beta',
  86. ],
  87. 'appRunResult' => 1,
  88. 'expectedMsg' => 'There is an error during sample data deployment. Composer file will be reverted.'
  89. . PHP_EOL,
  90. 'authExist' => false,
  91. ],
  92. 'Successful sample data installation' => [
  93. 'sampleDataPackages' => [
  94. 'magento/module-cms-sample-data' => '1.0.0-beta',
  95. ],
  96. 'appRunResult' => 0,
  97. 'expectedMsg' => '',
  98. 'authExist' => true,
  99. ],
  100. ];
  101. }
  102. /**
  103. * @expectedException \Exception
  104. * @expectedExceptionMessage Error in writing Auth file path/to/auth.json. Please check permissions for writing.
  105. * @return void
  106. */
  107. public function testExecuteWithException()
  108. {
  109. $this->directoryReadMock->expects($this->once())
  110. ->method('readFile')
  111. ->with('composer.json')
  112. ->willReturn('{"version": "0.0.1"}');
  113. $this->filesystemMock->expects($this->once())
  114. ->method('getDirectoryRead')
  115. ->with(DirectoryList::ROOT)
  116. ->willReturn($this->directoryReadMock);
  117. $this->directoryWriteMock->expects($this->once())
  118. ->method('isExist')
  119. ->with(PackagesAuth::PATH_TO_AUTH_FILE)
  120. ->willReturn(false);
  121. $this->directoryWriteMock->expects($this->once())
  122. ->method('writeFile')
  123. ->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}')
  124. ->willThrowException(new \Exception('Something went wrong...'));
  125. $this->directoryWriteMock->expects($this->once())
  126. ->method('getAbsolutePath')
  127. ->with(PackagesAuth::PATH_TO_AUTH_FILE)
  128. ->willReturn('path/to/auth.json');
  129. $this->filesystemMock->expects($this->once())
  130. ->method('getDirectoryWrite')
  131. ->with(DirectoryList::COMPOSER_HOME)
  132. ->willReturn($this->directoryWriteMock);
  133. $this->createCommandTester()->execute([]);
  134. }
  135. /**
  136. * @return CommandTester
  137. */
  138. private function createCommandTester(): CommandTester
  139. {
  140. $commandTester = new CommandTester(
  141. new SampleDataDeployCommand(
  142. $this->filesystemMock,
  143. $this->sampleDataDependencyMock,
  144. $this->arrayInputFactoryMock,
  145. $this->applicationFactoryMock
  146. )
  147. );
  148. return $commandTester;
  149. }
  150. /**
  151. * @param $sampleDataPackages
  152. * @param $pathToComposerJson
  153. * @return array
  154. */
  155. protected function expectedComposerArguments(
  156. array $sampleDataPackages,
  157. string $pathToComposerJson
  158. ) : array {
  159. return [
  160. 'command' => 'require',
  161. '--working-dir' => $pathToComposerJson,
  162. '--no-progress' => 1,
  163. 'packages' => $this->packageVersionStrings($sampleDataPackages),
  164. ];
  165. }
  166. /**
  167. * @param array $sampleDataPackages
  168. * @return array
  169. */
  170. private function packageVersionStrings(array $sampleDataPackages): array
  171. {
  172. array_walk($sampleDataPackages, function (&$v, $k) {
  173. $v = "$k:$v";
  174. });
  175. return array_values($sampleDataPackages);
  176. }
  177. }