Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminUserCreateCommandTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Symfony\Component\Console\Application;
13 use Symfony\Component\Console\Helper\QuestionHelper;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Tester\CommandTester;
16 
20 class AdminUserCreateCommandTest extends \PHPUnit\Framework\TestCase
21 {
25  private $questionHelperMock;
26 
30  private $installerFactoryMock;
31 
35  private $command;
36 
37  public function setUp()
38  {
39  $this->installerFactoryMock = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
40  $this->command = new AdminUserCreateCommand($this->installerFactoryMock, new UserValidationRules());
41 
42  $this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
43  ->setMethods(['ask'])
44  ->getMock();
45  }
46 
47  public function testExecute()
48  {
49  $options = [
50  '--' . AdminAccount::KEY_USER => 'user',
51  '--' . AdminAccount::KEY_PASSWORD => '123123q',
53  '--' . AdminAccount::KEY_FIRST_NAME => 'John',
54  '--' . AdminAccount::KEY_LAST_NAME => 'Doe',
55  ];
56  $data = [
57  AdminAccount::KEY_USER => 'user',
58  AdminAccount::KEY_PASSWORD => '123123q',
63  ];
64  $commandTester = new CommandTester($this->command);
65  $installerMock = $this->createMock(\Magento\Setup\Model\Installer::class);
66  $installerMock->expects($this->once())->method('installAdminUser')->with($data);
67  $this->installerFactoryMock->expects($this->once())->method('create')->willReturn($installerMock);
68  $commandTester->execute($options, ['interactive' => false]);
69  $this->assertEquals('Created Magento administrator user named user' . PHP_EOL, $commandTester->getDisplay());
70  }
71 
72  public function testInteraction()
73  {
74  $application = new Application();
75  $application->add($this->command);
76 
77  $this->questionHelperMock->expects($this->at(0))
78  ->method('ask')
79  ->will($this->returnValue('admin'));
80 
81  $this->questionHelperMock->expects($this->at(1))
82  ->method('ask')
83  ->will($this->returnValue('Password123'));
84 
85  $this->questionHelperMock->expects($this->at(2))
86  ->method('ask')
87  ->will($this->returnValue('[email protected]'));
88 
89  $this->questionHelperMock->expects($this->at(3))
90  ->method('ask')
91  ->will($this->returnValue('John'));
92 
93  $this->questionHelperMock->expects($this->at(4))
94  ->method('ask')
95  ->will($this->returnValue('Doe'));
96 
97  // We override the standard helper with our mock
98  $this->command->getHelperSet()->set($this->questionHelperMock, 'question');
99 
100  $installerMock = $this->createMock(\Magento\Setup\Model\Installer::class);
101 
102  $expectedData = [
103  'admin-user' => 'admin',
104  'admin-password' => 'Password123',
105  'admin-email' => '[email protected]',
106  'admin-firstname' => 'John',
107  'admin-lastname' => 'Doe',
108  'magento-init-params' => null,
109  'help' => false,
110  'quiet' => false,
111  'verbose' => false,
112  'version' => false,
113  'ansi' => false,
114  'no-ansi' => false,
115  'no-interaction' => false,
116  ];
117 
118  $installerMock->expects($this->once())->method('installAdminUser')->with($expectedData);
119  $this->installerFactoryMock->expects($this->once())->method('create')->willReturn($installerMock);
120 
121  $commandTester = new CommandTester($this->command);
122  $commandTester->execute([
123  'command' => $this->command->getName(),
124  ]);
125 
126  $this->assertEquals(
127  'Created Magento administrator user named admin' . PHP_EOL,
128  $commandTester->getDisplay()
129  );
130  }
131 
138  {
139  /* @var $argsList \Symfony\Component\Console\Input\InputArgument[] */
140  $argsList = $this->command->getOptionsList($mode);
141  $this->assertEquals(AdminAccount::KEY_EMAIL, $argsList[2]->getName());
142  $this->assertEquals($description, $argsList[2]->getDescription());
143  }
144 
148  public function getOptionListDataProvider()
149  {
150  return [
151  [
152  'mode' => InputOption::VALUE_REQUIRED,
153  'description' => '(Required) Admin email',
154  ],
155  [
156  'mode' => InputOption::VALUE_OPTIONAL,
157  'description' => 'Admin email',
158  ],
159  ];
160  }
161 
167  public function testValidate(array $options, array $errors)
168  {
169  $inputMock = $this->getMockForAbstractClass(
170  \Symfony\Component\Console\Input\InputInterface::class,
171  [],
172  '',
173  false
174  );
175  $index = 0;
176  foreach ($options as $option) {
177  $inputMock->expects($this->at($index++))->method('getOption')->willReturn($option);
178  }
179  $this->assertEquals($errors, $this->command->validate($inputMock));
180  }
181 
185  public function validateDataProvider()
186  {
187  return [
188  [
189  [null, 'Doe', 'admin', '[email protected]', '123123q', '123123q'],
190  ['"First Name" is required. Enter and try again.']
191  ],
192  [
193  ['John', null, null, '[email protected]', '123123q', '123123q'],
194  ['"User Name" is required. Enter and try again.', '"Last Name" is required. Enter and try again.'],
195  ],
196  [['John', 'Doe', 'admin', null, '123123q', '123123q'], ['Please enter a valid email.']],
197  [
198  ['John', 'Doe', 'admin', 'test', '123123q', '123123q'],
199  ["'test' is not a valid email address in the basic format local-part@hostname"]
200  ],
201  [
202  ['John', 'Doe', 'admin', '[email protected]', '', ''],
203  [
204  'Password is required field.',
205  'Your password must be at least 7 characters.',
206  'Your password must include both numeric and alphabetic characters.'
207  ]
208  ],
209  [
210  ['John', 'Doe', 'admin', '[email protected]', '123123', '123123'],
211  [
212  'Your password must be at least 7 characters.',
213  'Your password must include both numeric and alphabetic characters.'
214  ]
215  ],
216  [
217  ['John', 'Doe', 'admin', '[email protected]', '1231231', '1231231'],
218  ['Your password must include both numeric and alphabetic characters.']
219  ],
220  [['John', 'Doe', 'admin', '[email protected]', '123123q', '123123q'], []],
221  ];
222  }
223 }
$application
Definition: bootstrap.php:58
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$index
Definition: list.phtml:44
$errors
Definition: overview.phtml:9