7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
9
use Magento \SampleData \Console \Command \SampleDataDeployCommand ;
10
+ use Magento \Setup \Model \PackagesAuth ;
10
11
use Symfony \Component \Console \Tester \CommandTester ;
12
+ use Magento \Framework \Filesystem ;
13
+ use Magento \Framework \Filesystem \Directory \ReadInterface ;
14
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
15
+ use Magento \SampleData \Model \Dependency ;
16
+ use Symfony \Component \Console \Input \ArrayInputFactory ;
17
+ use Composer \Console \ApplicationFactory ;
18
+ use Composer \Console \Application ;
11
19
12
20
class SampleDataDeployCommandTest extends \PHPUnit_Framework_TestCase
13
21
{
22
+ /**
23
+ * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
24
+ */
25
+ private $ directoryReadMock ;
26
+
27
+ /**
28
+ * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
29
+ */
30
+ private $ directoryWriteMock ;
31
+
32
+ /**
33
+ * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
34
+ */
35
+ private $ filesystemMock ;
36
+
37
+ /**
38
+ * @var Dependency|\PHPUnit_Framework_MockObject_MockObject
39
+ */
40
+ private $ sampleDataDependencyMock ;
41
+
42
+ /**
43
+ * @var ArrayInputFactory|\PHPUnit_Framework_MockObject_MockObject
44
+ */
45
+ private $ arrayInputFactoryMock ;
46
+
47
+ /**
48
+ * @var Application|\PHPUnit_Framework_MockObject_MockObject
49
+ */
50
+ private $ applicationMock ;
51
+
52
+ /**
53
+ * @var ApplicationFactory|\PHPUnit_Framework_MockObject_MockObject
54
+ */
55
+ private $ applicationFactoryMock ;
56
+
57
+ /**
58
+ * @return void
59
+ */
60
+ protected function setUp ()
61
+ {
62
+ $ this ->directoryReadMock = $ this ->getMock (ReadInterface::class, [], [], '' , false );
63
+ $ this ->directoryWriteMock = $ this ->getMock (WriteInterface::class, [], [], '' , false );
64
+ $ this ->filesystemMock = $ this ->getMock (Filesystem::class, [], [], '' , false );
65
+ $ this ->sampleDataDependencyMock = $ this ->getMock (Dependency::class, [], [], '' , false );
66
+ $ this ->arrayInputFactoryMock = $ this ->getMock (ArrayInputFactory::class, [], [], '' , false );
67
+ $ this ->applicationMock = $ this ->getMock (Application::class, [], [], '' , false );
68
+ $ this ->applicationFactoryMock = $ this ->getMock (ApplicationFactory::class, ['create ' ], [], '' , false );
69
+ }
70
+
14
71
/**
15
72
* @param array $sampleDataPackages
16
73
* @param int $appRunResult - int 0 if everything went fine, or an error code
17
74
* @param string $expectedMsg
75
+ * @param bool $authExist
18
76
* @return void
19
77
*
20
78
* @dataProvider processDataProvider
21
79
*/
22
- public function testExecute (array $ sampleDataPackages , $ appRunResult , $ expectedMsg )
80
+ public function testExecute (array $ sampleDataPackages , $ appRunResult , $ expectedMsg, $ authExist )
23
81
{
24
- $ directoryRead = $ this ->getMock ('\Magento\Framework\Filesystem\Directory\ReadInterface ' , [], [], '' , false );
25
- $ directoryRead ->expects ($ this ->any ())->method ('getAbsolutePath ' )->willReturn ('/path/to/composer.json ' );
26
-
27
- $ filesystem = $ this ->getMock ('Magento\Framework\Filesystem ' , [], [], '' , false );
28
- $ filesystem ->expects ($ this ->any ())->method ('getDirectoryRead ' )->with (DirectoryList::ROOT )
29
- ->willReturn ($ directoryRead );
82
+ $ pathToComposerJson = '/path/to/composer.json ' ;
30
83
31
- $ sampleDataDependency = $ this ->getMock ('Magento\SampleData\Model\Dependency ' , [], [], '' , false );
32
- $ sampleDataDependency
33
- ->expects ($ this ->any ())
84
+ $ this ->directoryReadMock ->expects ($ this ->any ())
85
+ ->method ('getAbsolutePath ' )
86
+ ->willReturn ($ pathToComposerJson );
87
+ $ this ->directoryWriteMock ->expects ($ this ->once ())
88
+ ->method ('isExist ' )
89
+ ->with (PackagesAuth::PATH_TO_AUTH_FILE )
90
+ ->willReturn ($ authExist );
91
+ $ this ->directoryWriteMock ->expects ($ authExist ? $ this ->never () : $ this ->once ())
92
+ ->method ('writeFile ' )
93
+ ->with (PackagesAuth::PATH_TO_AUTH_FILE , '{} ' );
94
+ $ this ->filesystemMock ->expects ($ this ->any ())
95
+ ->method ('getDirectoryRead ' )
96
+ ->with (DirectoryList::ROOT )
97
+ ->willReturn ($ this ->directoryReadMock );
98
+ $ this ->filesystemMock ->expects ($ this ->once ())
99
+ ->method ('getDirectoryWrite ' )
100
+ ->with (DirectoryList::COMPOSER_HOME )
101
+ ->willReturn ($ this ->directoryWriteMock );
102
+ $ this ->sampleDataDependencyMock ->expects ($ this ->any ())
34
103
->method ('getSampleDataPackages ' )
35
104
->willReturn ($ sampleDataPackages );
36
-
37
- $ arrayInputFactory = $ this
38
- ->getMock ('Symfony\Component\Console\Input\ArrayInputFactory ' , ['create ' ], [], '' , false );
39
- $ arrayInputFactory ->expects ($ this ->never ())->method ('create ' );
105
+ $ this ->arrayInputFactoryMock ->expects ($ this ->never ())
106
+ ->method ('create ' );
40
107
41
108
array_walk ($ sampleDataPackages , function (&$ v , $ k ) {
42
109
$ v = "$ k: $ v " ;
@@ -46,24 +113,32 @@ public function testExecute(array $sampleDataPackages, $appRunResult, $expectedM
46
113
47
114
$ requireArgs = [
48
115
'command ' => 'require ' ,
49
- '--working-dir ' => ' /path/to/composer.json ' ,
116
+ '--working-dir ' => $ pathToComposerJson ,
50
117
'--no-progress ' => 1 ,
51
118
'packages ' => $ packages ,
52
119
];
53
120
$ commandInput = new \Symfony \Component \Console \Input \ArrayInput ($ requireArgs );
54
121
55
- $ application = $ this ->getMock ( ' Composer\Console\Application ' , [], [], '' , false );
56
- $ application -> expects ( $ this -> any ()) ->method ('run ' )
122
+ $ this -> applicationMock -> expects ( $ this ->any ())
123
+ ->method ('run ' )
57
124
->with ($ commandInput , $ this ->anything ())
58
125
->willReturn ($ appRunResult );
126
+
59
127
if (($ appRunResult !== 0 ) && !empty ($ sampleDataPackages )) {
60
- $ application ->expects ($ this ->once ())->method ('resetComposer ' )->willReturnSelf ();
128
+ $ this -> applicationMock ->expects ($ this ->once ())->method ('resetComposer ' )->willReturnSelf ();
61
129
}
62
- $ applicationFactory = $ this ->getMock ('Composer\Console\ApplicationFactory ' , ['create ' ], [], '' , false );
63
- $ applicationFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ application );
130
+
131
+ $ this ->applicationFactoryMock ->expects ($ this ->any ())
132
+ ->method ('create ' )
133
+ ->willReturn ($ this ->applicationMock );
64
134
65
135
$ commandTester = new CommandTester (
66
- new SampleDataDeployCommand ($ filesystem , $ sampleDataDependency , $ arrayInputFactory , $ applicationFactory )
136
+ new SampleDataDeployCommand (
137
+ $ this ->filesystemMock ,
138
+ $ this ->sampleDataDependencyMock ,
139
+ $ this ->arrayInputFactoryMock ,
140
+ $ this ->applicationFactoryMock
141
+ )
67
142
);
68
143
$ commandTester ->execute ([]);
69
144
@@ -80,6 +155,7 @@ public function processDataProvider()
80
155
'sampleDataPackages ' => [],
81
156
'appRunResult ' => 1 ,
82
157
'expectedMsg ' => 'There is no sample data for current set of modules. ' . PHP_EOL ,
158
+ 'authExist ' => true ,
83
159
],
84
160
[
85
161
'sampleDataPackages ' => [
@@ -88,14 +164,47 @@ public function processDataProvider()
88
164
'appRunResult ' => 1 ,
89
165
'expectedMsg ' => 'There is an error during sample data deployment. Composer file will be reverted. '
90
166
. PHP_EOL ,
167
+ 'authExist ' => false ,
91
168
],
92
169
[
93
170
'sampleDataPackages ' => [
94
171
'magento/module-cms-sample-data ' => '1.0.0-beta ' ,
95
172
],
96
173
'appRunResult ' => 0 ,
97
174
'expectedMsg ' => '' ,
175
+ 'authExist ' => true ,
98
176
],
99
177
];
100
178
}
179
+
180
+ /**
181
+ * @expectedException \Exception
182
+ * @expectedExceptionMessage Error in writing Auth file. Please check permissions for writing.
183
+ * @return void
184
+ */
185
+ public function testExecuteWithException ()
186
+ {
187
+ $ this ->directoryWriteMock ->expects ($ this ->once ())
188
+ ->method ('isExist ' )
189
+ ->with (PackagesAuth::PATH_TO_AUTH_FILE )
190
+ ->willReturn (false );
191
+ $ this ->directoryWriteMock ->expects ($ this ->once ())
192
+ ->method ('writeFile ' )
193
+ ->with (PackagesAuth::PATH_TO_AUTH_FILE , '{} ' )
194
+ ->willThrowException (new \Exception ('Something went wrong... ' ));
195
+ $ this ->filesystemMock ->expects ($ this ->once ())
196
+ ->method ('getDirectoryWrite ' )
197
+ ->with (DirectoryList::COMPOSER_HOME )
198
+ ->willReturn ($ this ->directoryWriteMock );
199
+
200
+ $ commandTester = new CommandTester (
201
+ new SampleDataDeployCommand (
202
+ $ this ->filesystemMock ,
203
+ $ this ->sampleDataDependencyMock ,
204
+ $ this ->arrayInputFactoryMock ,
205
+ $ this ->applicationFactoryMock
206
+ )
207
+ );
208
+ $ commandTester ->execute ([]);
209
+ }
101
210
}
0 commit comments