Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstallCommandTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Symfony\Component\Console\Application;
12 use Symfony\Component\Console\Helper\HelperSet;
13 use Symfony\Component\Console\Input\InputDefinition;
14 use Symfony\Component\Console\Tester\CommandTester;
16 use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList;
17 use Magento\Framework\Config\ConfigOptionsListConstants as SetupConfigOptionsList;
20 
24 class InstallCommandTest extends \PHPUnit\Framework\TestCase
25 {
29  private $input;
30 
34  private $command;
35 
39  private $installerFactory;
40 
44  private $installer;
45 
49  private $applicationMock;
50 
54  private $helperSetMock;
55 
59  private $definitionMock;
60 
64  private $configImportMock;
65 
69  private $adminUserMock;
70 
71  public function setUp()
72  {
73  $this->input = [
74  '--' . SetupConfigOptionsList::INPUT_KEY_DB_HOST => 'localhost',
75  '--' . SetupConfigOptionsList::INPUT_KEY_DB_NAME => 'magento',
76  '--' . SetupConfigOptionsList::INPUT_KEY_DB_USER => 'root',
77  '--' . BackendConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME => 'admin',
78  '--' . StoreConfigurationDataMapper::KEY_BASE_URL => 'http://127.0.0.1/magento2ce/',
80  '--' . StoreConfigurationDataMapper::KEY_TIMEZONE => 'America/Chicago',
82  ];
83 
84  $configModel = $this->createMock(\Magento\Setup\Model\ConfigModel::class);
85  $configModel
86  ->expects($this->exactly(2))
87  ->method('getAvailableOptions')
88  ->will($this->returnValue($this->getOptionsListDeployConfig()));
89  $configModel
90  ->expects($this->once())
91  ->method('validate')
92  ->will($this->returnValue([]));
93 
94  $userConfig = $this->createMock(\Magento\Setup\Console\Command\InstallStoreConfigurationCommand::class);
95  $userConfig
96  ->expects($this->once())
97  ->method('getOptionsList')
98  ->will($this->returnValue($this->getOptionsListUserConfig()));
99  $userConfig
100  ->expects($this->once())
101  ->method('validate')
102  ->will($this->returnValue([]));
103 
104  $this->adminUserMock = $this->createMock(AdminUserCreateCommand::class);
105  $this->adminUserMock
106  ->expects($this->once())
107  ->method('getOptionsList')
108  ->will($this->returnValue($this->getOptionsListAdminUser()));
109 
110  $this->installerFactory = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
111  $this->installer = $this->createMock(\Magento\Setup\Model\Installer::class);
112  $this->applicationMock = $this->getMockBuilder(Application::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->helperSetMock = $this->getMockBuilder(HelperSet::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118  $this->definitionMock = $this->getMockBuilder(InputDefinition::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->configImportMock = $this->getMockBuilder(ConfigImportCommand::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124 
125  $this->applicationMock->expects($this->any())
126  ->method('getHelperSet')
127  ->willReturn($this->helperSetMock);
128  $this->applicationMock->expects($this->any())
129  ->method('getDefinition')
130  ->willReturn($this->definitionMock);
131  $this->definitionMock->expects($this->any())
132  ->method('getOptions')
133  ->willReturn([]);
134  $this->applicationMock->expects($this->any())
135  ->method('find')
137  ->willReturn($this->configImportMock);
138 
139  $this->command = new InstallCommand(
140  $this->installerFactory,
141  $configModel,
142  $userConfig,
143  $this->adminUserMock
144  );
145  $this->command->setApplication(
146  $this->applicationMock
147  );
148  }
149 
150  public function testExecute()
151  {
152  $this->input['--' . AdminAccount::KEY_USER] = 'user';
153  $this->input['--' . AdminAccount::KEY_PASSWORD] = '123123q';
154  $this->input['--' . AdminAccount::KEY_EMAIL] = '[email protected]';
155  $this->input['--' . AdminAccount::KEY_FIRST_NAME] = 'John';
156  $this->input['--' . AdminAccount::KEY_LAST_NAME] = 'Doe';
157 
158  $this->adminUserMock
159  ->expects($this->once())
160  ->method('validate')
161  ->willReturn([]);
162  $this->installerFactory->expects($this->once())
163  ->method('create')
164  ->will($this->returnValue($this->installer));
165  $this->installer->expects($this->once())->method('install');
166  $this->configImportMock->expects($this->once())
167  ->method('run');
168 
169  $commandTester = new CommandTester($this->command);
170  $commandTester->execute($this->input);
171  }
172 
178  private function getOptionsListDeployConfig()
179  {
180  $option1 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
181  $option1
182  ->expects($this->any())
183  ->method('getName')
184  ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_HOST));
185  $option2 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
186  $option2
187  ->expects($this->any())
188  ->method('getName')
189  ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_NAME));
190  $option3 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
191  $option3
192  ->expects($this->any())
193  ->method('getName')
194  ->will($this->returnValue(SetupConfigOptionsList::INPUT_KEY_DB_USER));
195  $option4 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
196  $option4
197  ->expects($this->any())
198  ->method('getName')
199  ->will($this->returnValue(BackendConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME));
200 
201  return [$option1, $option2, $option3, $option4];
202  }
203 
209  private function getOptionsListUserConfig()
210  {
211  $option1 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
212  $option1
213  ->expects($this->any())
214  ->method('getName')
215  ->will($this->returnValue(StoreConfigurationDataMapper::KEY_BASE_URL));
216  $option2 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
217  $option2
218  ->expects($this->any())
219  ->method('getName')
220  ->will($this->returnValue(StoreConfigurationDataMapper::KEY_LANGUAGE));
221  $option3 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
222  $option3
223  ->expects($this->any())
224  ->method('getName')
225  ->will($this->returnValue(StoreConfigurationDataMapper::KEY_TIMEZONE));
226  $option4 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
227  $option4
228  ->expects($this->any())
229  ->method('getName')
230  ->will($this->returnValue(StoreConfigurationDataMapper::KEY_CURRENCY));
231 
232  return [$option1, $option2, $option3, $option4];
233  }
234 
240  private function getOptionsListAdminUser()
241  {
242  $option1 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
243  $option1
244  ->expects($this->any())
245  ->method('getName')
246  ->will($this->returnValue(AdminAccount::KEY_USER));
247  $option2 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
248  $option2
249  ->expects($this->any())
250  ->method('getName')
251  ->will($this->returnValue(AdminAccount::KEY_PASSWORD));
252  $option3 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
253  $option3
254  ->expects($this->any())
255  ->method('getName')
256  ->will($this->returnValue(AdminAccount::KEY_EMAIL));
257  $option4 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
258  $option4
259  ->expects($this->any())
260  ->method('getName')
261  ->will($this->returnValue(AdminAccount::KEY_FIRST_NAME));
262  $option5 = $this->createMock(\Magento\Framework\Setup\Option\TextConfigOption::class);
263  $option5
264  ->expects($this->any())
265  ->method('getName')
266  ->will($this->returnValue(AdminAccount::KEY_LAST_NAME));
267 
268  return [$option1, $option2, $option3, $option4, $option5];
269  }
270 
277  public function testValidate($prefixValue)
278  {
279  $this->adminUserMock
280  ->expects($this->never())
281  ->method('validate');
282  $this->installerFactory->expects($this->once())
283  ->method('create')
284  ->will($this->returnValue($this->installer));
285  $this->installer->expects($this->once())->method('install');
286  $this->input['--' . InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX] = $prefixValue;
287 
288  $commandTester = new CommandTester($this->command);
289  $commandTester->execute($this->input);
290  }
291 
299  public function testValidateWithException($prefixValue)
300  {
301  $this->adminUserMock
302  ->expects($this->never())
303  ->method('validate');
304  $this->installerFactory->expects($this->never())
305  ->method('create')
306  ->will($this->returnValue($this->installer));
307  $this->installer->expects($this->never())->method('install');
308  $this->input['--' . InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX] = $prefixValue;
309 
310  $commandTester = new CommandTester($this->command);
311  $commandTester->execute($this->input);
312  }
313 
317  public function validateDataProvider()
318  {
319  return [
320  'without option' => ['', ''],
321  'normal case' => ['abcde', ''],
322  '20 chars' => ['12345678901234567890', ''],
323  ];
324  }
325 
330  {
331  return [
332  ['123456789012345678901', 'InvalidArgumentException'],
333  ['abcdefghijk12345678fdgsdfgsdfgsdfsgsdfg90abcdefgdfddgsdfg', 'InvalidArgumentException'],
334  ];
335  }
336 }