PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/setup/src/Magento/Setup/Test/Unit/Console/Command/InstallCommandTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 270 lines | 195 code | 22 blank | 53 comment | 0 complexity | 79ffd6035a7c7db0cfca73ed23988e1b MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Test\Unit\Console\Command;
  7. use Magento\Setup\Console\Command\InstallCommand;
  8. use Symfony\Component\Console\Tester\CommandTester;
  9. use Magento\Setup\Model\AdminAccount;
  10. use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList;
  11. use Magento\Framework\Config\ConfigOptionsListConstants as SetupConfigOptionsList;
  12. use Magento\Setup\Model\StoreConfigurationDataMapper;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class InstallCommandTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $input;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|InstallCommand
  24. */
  25. private $command;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Model\InstallerFactory
  28. */
  29. private $installerFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Model\Installer
  32. */
  33. private $installer;
  34. public function setUp()
  35. {
  36. $this->input = [
  37. '--' . SetupConfigOptionsList::INPUT_KEY_DB_HOST => 'localhost',
  38. '--' . SetupConfigOptionsList::INPUT_KEY_DB_NAME => 'magento',
  39. '--' . SetupConfigOptionsList::INPUT_KEY_DB_USER => 'root',
  40. '--' . BackendConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME => 'admin',
  41. '--' . StoreConfigurationDataMapper::KEY_BASE_URL => 'http://127.0.0.1/magento2ce/',
  42. '--' . StoreConfigurationDataMapper::KEY_LANGUAGE => 'en_US',
  43. '--' . StoreConfigurationDataMapper::KEY_TIMEZONE => 'America/Chicago',
  44. '--' . StoreConfigurationDataMapper::KEY_CURRENCY => 'USD',
  45. '--' . AdminAccount::KEY_USER => 'user',
  46. '--' . AdminAccount::KEY_PASSWORD => '123123q',
  47. '--' . AdminAccount::KEY_EMAIL => 'test@test.com',
  48. '--' . AdminAccount::KEY_FIRST_NAME => 'John',
  49. '--' . AdminAccount::KEY_LAST_NAME => 'Doe',
  50. ];
  51. $configModel = $this->getMock(\Magento\Setup\Model\ConfigModel::class, [], [], '', false);
  52. $configModel
  53. ->expects($this->exactly(2))
  54. ->method('getAvailableOptions')
  55. ->will($this->returnValue($this->getOptionsListDeployConfig()));
  56. $configModel
  57. ->expects($this->once())
  58. ->method('validate')
  59. ->will($this->returnValue([]));
  60. $userConfig = $this->getMock(
  61. \Magento\Setup\Console\Command\InstallStoreConfigurationCommand::class,
  62. [],
  63. [],
  64. '',
  65. false
  66. );
  67. $userConfig
  68. ->expects($this->once())
  69. ->method('getOptionsList')
  70. ->will($this->returnValue($this->getOptionsListUserConfig()));
  71. $userConfig
  72. ->expects($this->once())
  73. ->method('validate')
  74. ->will($this->returnValue([]));
  75. $adminUser = $this->getMock(\Magento\Setup\Console\Command\AdminUserCreateCommand::class, [], [], '', false);
  76. $adminUser
  77. ->expects($this->once())
  78. ->method('getOptionsList')
  79. ->will($this->returnValue($this->getOptionsListAdminUser()));
  80. $adminUser
  81. ->expects($this->once())
  82. ->method('validate')
  83. ->will($this->returnValue([]));
  84. $this->installerFactory = $this->getMock(\Magento\Setup\Model\InstallerFactory::class, [], [], '', false);
  85. $this->installer = $this->getMock(\Magento\Setup\Model\Installer::class, [], [], '', false);
  86. $this->command = new InstallCommand(
  87. $this->installerFactory,
  88. $configModel,
  89. $userConfig,
  90. $adminUser
  91. );
  92. }
  93. public function testExecute()
  94. {
  95. $this->installerFactory->expects($this->once())
  96. ->method('create')
  97. ->will($this->returnValue($this->installer));
  98. $this->installer->expects($this->once())->method('install');
  99. $commandTester = new CommandTester($this->command);
  100. $commandTester->execute($this->input);
  101. }
  102. /**
  103. * Get list of options for deployment configuration
  104. *
  105. * @return array
  106. */
  107. private function getOptionsListDeployConfig()
  108. {
  109. $option1 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  110. $option1
  111. ->expects($this->any())
  112. ->method('getName')
  113. ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_HOST));
  114. $option2 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  115. $option2
  116. ->expects($this->any())
  117. ->method('getName')
  118. ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_NAME));
  119. $option3 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  120. $option3
  121. ->expects($this->any())
  122. ->method('getName')
  123. ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_USER));
  124. $option4 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  125. $option4
  126. ->expects($this->any())
  127. ->method('getName')
  128. ->will($this->returnValue(BackendConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME));
  129. return [$option1, $option2, $option3, $option4];
  130. }
  131. /**
  132. * Get list of options for user configuration
  133. *
  134. * @return array
  135. */
  136. private function getOptionsListUserConfig()
  137. {
  138. $option1 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  139. $option1
  140. ->expects($this->any())
  141. ->method('getName')
  142. ->will($this->returnValue(StoreConfigurationDataMapper::KEY_BASE_URL));
  143. $option2 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  144. $option2
  145. ->expects($this->any())
  146. ->method('getName')
  147. ->will($this->returnValue(StoreConfigurationDataMapper::KEY_LANGUAGE));
  148. $option3 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  149. $option3
  150. ->expects($this->any())
  151. ->method('getName')
  152. ->will($this->returnValue(StoreConfigurationDataMapper::KEY_TIMEZONE));
  153. $option4 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  154. $option4
  155. ->expects($this->any())
  156. ->method('getName')
  157. ->will($this->returnValue(StoreConfigurationDataMapper::KEY_CURRENCY));
  158. return [$option1, $option2, $option3, $option4];
  159. }
  160. /**
  161. * Get list of options for admin user
  162. *
  163. * @return array
  164. */
  165. private function getOptionsListAdminUser()
  166. {
  167. $option1 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  168. $option1
  169. ->expects($this->any())
  170. ->method('getName')
  171. ->will($this->returnValue(AdminAccount::KEY_USER));
  172. $option2 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  173. $option2
  174. ->expects($this->any())
  175. ->method('getName')
  176. ->will($this->returnValue(AdminAccount::KEY_PASSWORD));
  177. $option3 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  178. $option3
  179. ->expects($this->any())
  180. ->method('getName')
  181. ->will($this->returnValue(AdminAccount::KEY_EMAIL));
  182. $option4 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  183. $option4
  184. ->expects($this->any())
  185. ->method('getName')
  186. ->will($this->returnValue(AdminAccount::KEY_FIRST_NAME));
  187. $option5 = $this->getMock(\Magento\Framework\Setup\Option\TextConfigOption::class, [], [], '', false);
  188. $option5
  189. ->expects($this->any())
  190. ->method('getName')
  191. ->will($this->returnValue(AdminAccount::KEY_LAST_NAME));
  192. return [$option1, $option2, $option3, $option4, $option5];
  193. }
  194. /**
  195. * Test install command with valid sales_order_increment_prefix value
  196. *
  197. * @dataProvider validateDataProvider
  198. * @param $prefixValue
  199. */
  200. public function testValidate($prefixValue)
  201. {
  202. $this->installerFactory->expects($this->once())
  203. ->method('create')
  204. ->will($this->returnValue($this->installer));
  205. $this->installer->expects($this->once())->method('install');
  206. $this->input['--' . InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX] = $prefixValue;
  207. $commandTester = new CommandTester($this->command);
  208. $commandTester->execute($this->input);
  209. }
  210. /**
  211. * Test install command with invalid sales_order_increment_prefix value
  212. *
  213. * @expectedException \InvalidArgumentException
  214. * @dataProvider validateWithExceptionDataProvider
  215. * @param $prefixValue
  216. */
  217. public function testValidateWithException($prefixValue)
  218. {
  219. $this->installerFactory->expects($this->never())
  220. ->method('create')
  221. ->will($this->returnValue($this->installer));
  222. $this->installer->expects($this->never())->method('install');
  223. $this->input['--' . InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX] = $prefixValue;
  224. $commandTester = new CommandTester($this->command);
  225. $commandTester->execute($this->input);
  226. }
  227. /**
  228. * @return array
  229. */
  230. public function validateDataProvider()
  231. {
  232. return [
  233. 'without option' => ['', ''],
  234. 'normal case' => ['abcde', ''],
  235. '20 chars' => ['12345678901234567890', '']
  236. ];
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function validateWithExceptionDataProvider()
  242. {
  243. return [
  244. ['123456789012345678901', 'InvalidArgumentException'],
  245. ['abcdefghijk12345678fdgsdfgsdfgsdfsgsdfg90abcdefgdfddgsdfg', 'InvalidArgumentException']
  246. ];
  247. }
  248. }