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