1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Customer; |
4
|
|
|
|
5
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
6
|
|
|
use N98\Magento\Command\TestCase; |
7
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CreateCommandTest |
11
|
|
|
* @package N98\Magento\Command\Customer |
12
|
|
|
*/ |
13
|
|
|
class CreateCommandTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testExecute() |
16
|
|
|
{ |
17
|
|
|
$generatedEmail = uniqid('', true) . '@example.com'; |
18
|
|
|
|
19
|
|
|
$input = [ |
20
|
|
|
'command' => 'customer:create', |
21
|
|
|
'email' => $generatedEmail, |
22
|
|
|
'password' => 'Password123', |
23
|
|
|
'firstname' => 'John', |
24
|
|
|
'lastname' => 'Doe', |
25
|
|
|
'website' => $this->getWebsiteCode(), |
26
|
|
|
]; |
27
|
|
|
$this->assertDisplayContains($input, 'successfully created'); |
28
|
|
|
|
29
|
|
|
// Format option |
30
|
|
|
$generatedEmail = uniqid('', true) . '@example.com'; |
31
|
|
|
$input['email'] = $generatedEmail; |
32
|
|
|
$input['--format'] = 'csv'; |
33
|
|
|
|
34
|
|
|
$this->assertDisplayContains($input, 'email,password,firstname,lastname'); |
35
|
|
|
$this->assertdisplayContains($input, $generatedEmail . ',Password123,John,Doe'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testExecuteAdditionalFields() |
39
|
|
|
{ |
40
|
|
|
$generatedEmail = uniqid('', true) . '@example.com'; |
41
|
|
|
|
42
|
|
|
$input = [ |
43
|
|
|
'command' => 'customer:create', |
44
|
|
|
'email' => $generatedEmail, |
45
|
|
|
'password' => 'Password123', |
46
|
|
|
'firstname' => 'John', |
47
|
|
|
'lastname' => 'Doe', |
48
|
|
|
'website' => $this->getWebsiteCode(), |
49
|
|
|
'additionalFields' => ['prefix', 'Mr'] |
50
|
|
|
]; |
51
|
|
|
$this->assertDisplayContains($input, 'successfully created'); |
52
|
|
|
|
53
|
|
|
// Format option |
54
|
|
|
$generatedEmail = uniqid('', true) . '@example.com'; |
55
|
|
|
$input['email'] = $generatedEmail; |
56
|
|
|
$input['--format'] = 'csv'; |
57
|
|
|
|
58
|
|
|
$this->assertDisplayContains($input, 'email,password,firstname,lastname'); |
59
|
|
|
$this->assertdisplayContains($input, $generatedEmail . ',Password123,John,Doe'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
private function getWebsiteCode() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$storeManager = $this->getApplication()->getObjectManager()->get(StoreManagerInterface::class); |
68
|
|
|
$website = $storeManager->getWebsite('base'); |
69
|
|
|
|
70
|
|
|
return $website->getCode(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testWithWrongPassword() |
74
|
|
|
{ |
75
|
|
|
$this->markTestIncomplete('We currently cannot deal with interactive commands'); |
76
|
|
|
|
77
|
|
|
$application = $this->getApplication(); |
78
|
|
|
$application->add(new CreateCommand()); |
79
|
|
|
|
80
|
|
|
// try to create a customer with a password < 6 chars |
81
|
|
|
$command = $this->getApplication()->find('customer:create'); |
82
|
|
|
|
83
|
|
|
$generatedEmail = uniqid('', true) . '@example.com'; |
84
|
|
|
|
85
|
|
|
// mock dialog |
86
|
|
|
// We mock the DialogHelper |
87
|
|
|
$dialog = $this->createMock('N98\Util\Console\Helper\ParameterHelper', ['askPassword']); |
|
|
|
|
88
|
|
|
$dialog->expects($this->at(0)) |
89
|
|
|
->method('askPassword') |
90
|
|
|
->willReturn(true); // The user confirms |
91
|
|
|
|
92
|
|
|
// We override the standard helper with our mock |
93
|
|
|
$command->getHelperSet()->set($dialog, 'parameter'); |
94
|
|
|
|
95
|
|
|
$options = [ |
96
|
|
|
'command' => $command->getName(), |
97
|
|
|
'email' => $generatedEmail, |
98
|
|
|
'password' => 'pass', |
99
|
|
|
'firstname' => 'John', |
100
|
|
|
'lastname' => 'Doe', |
101
|
|
|
'website' => 1 |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$commandTester = new CommandTester($command); |
105
|
|
|
$commandTester->execute($options); |
106
|
|
|
$this->assertRegExp( |
107
|
|
|
'/The password must have at least 6 characters. Leading or trailing spaces will be ignored./', |
108
|
|
|
$commandTester->getDisplay() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.