@@ -22,38 +22,96 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
22
22
*/
23
23
private $ packagesData ;
24
24
25
+ /**
26
+ * @var ComposerInformation|MockObject
27
+ */
28
+ private $ composerInformation ;
29
+
30
+ /**
31
+ * @var \Magento\Setup\Model\DateTime\TimeZoneProvider|MockObject
32
+ */
33
+ private $ timeZoneProvider ;
34
+
35
+ /**
36
+ * @var \Magento\Setup\Model\PackagesAuth|MockObject
37
+ */
38
+ private $ packagesAuth ;
39
+
40
+ /**
41
+ * @var \Magento\Framework\Filesystem|MockObject
42
+ */
43
+ private $ filesystem ;
44
+
45
+ /**
46
+ * @var \Magento\Setup\Model\ObjectManagerProvider|MockObject
47
+ */
48
+ private $ objectManagerProvider ;
49
+
50
+ /**
51
+ * @var \Magento\Setup\Model\Grid\TypeMapper|MockObject
52
+ */
53
+ private $ typeMapper ;
54
+
25
55
public function setUp ()
26
56
{
27
- $ composerInformation = $ this ->getComposerInformation ();
28
- $ timeZoneProvider = $ this ->getMock (\Magento \Setup \Model \DateTime \TimeZoneProvider::class, [], [], '' , false );
57
+ $ this ->composerInformation = $ this ->getComposerInformation ();
58
+ $ this ->timeZoneProvider = $ this ->getMockBuilder (\Magento \Setup \Model \DateTime \TimeZoneProvider::class)
59
+ ->disableOriginalConstructor ()
60
+ ->getMock ();
29
61
$ timeZone = $ this ->getMock (\Magento \Framework \Stdlib \DateTime \Timezone::class, [], [], '' , false );
30
- $ timeZoneProvider ->expects ($ this ->any ())->method ('get ' )->willReturn ($ timeZone );
31
- $ packagesAuth = $ this ->getMock (\Magento \Setup \Model \PackagesAuth::class, [], [], '' , false );
32
- $ filesystem = $ this ->getMock (\Magento \Framework \Filesystem::class, [], [], '' , false );
33
- $ objectManagerProvider = $ this ->getMock (\Magento \Setup \Model \ObjectManagerProvider::class, [], [], '' , false );
62
+ $ this ->timeZoneProvider ->expects ($ this ->any ())->method ('get ' )->willReturn ($ timeZone );
63
+ $ this ->packagesAuth = $ this ->getMock (\Magento \Setup \Model \PackagesAuth::class, [], [], '' , false );
64
+ $ this ->filesystem = $ this ->getMock (\Magento \Framework \Filesystem::class, [], [], '' , false );
65
+ $ this ->objectManagerProvider = $ this ->getMockBuilder (\Magento \Setup \Model \ObjectManagerProvider::class)
66
+ ->disableOriginalConstructor ()
67
+ ->getMock ();
34
68
$ objectManager = $ this ->getMockForAbstractClass (\Magento \Framework \ObjectManagerInterface::class);
35
- $ applicationFactory = $ this ->getMock (
36
- \Magento \Framework \Composer \MagentoComposerApplicationFactory::class,
37
- [],
38
- [],
39
- '' ,
40
- false
41
- );
69
+ $ appFactory = $ this ->getMockBuilder (\Magento \Framework \Composer \MagentoComposerApplicationFactory::class)
70
+ ->disableOriginalConstructor ()
71
+ ->getMock ();
42
72
$ application = $ this ->getMock (\Magento \Composer \MagentoComposerApplication::class, [], [], '' , false );
43
73
$ application ->expects ($ this ->any ())
44
74
->method ('runComposerCommand ' )
45
- ->willReturn ('versions: 2.0.1 ' );
46
- $ applicationFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ application );
75
+ ->willReturnMap ([
76
+ [
77
+ [
78
+ PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW ,
79
+ PackagesData::PARAM_PACKAGE => 'magento/package-1 ' ,
80
+ PackagesData::PARAM_AVAILABLE => true ,
81
+ ],
82
+ null ,
83
+ 'versions: 2.0.1 '
84
+ ],
85
+ [
86
+ [
87
+ PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW ,
88
+ PackagesData::PARAM_PACKAGE => 'magento/package-2 ' ,
89
+ PackagesData::PARAM_AVAILABLE => true ,
90
+ ],
91
+ null ,
92
+ 'versions: 2.0.1 '
93
+ ],
94
+ [
95
+ [
96
+ PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW ,
97
+ PackagesData::PARAM_PACKAGE => 'partner/package-3 ' ,
98
+ PackagesData::PARAM_AVAILABLE => true ,
99
+ ],
100
+ null ,
101
+ 'versions: 3.0.1 '
102
+ ],
103
+ ]);
104
+ $ appFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ application );
47
105
$ objectManager ->expects ($ this ->any ())
48
106
->method ('get ' )
49
107
->with (\Magento \Framework \Composer \MagentoComposerApplicationFactory::class)
50
- ->willReturn ($ applicationFactory );
51
- $ objectManagerProvider ->expects ($ this ->any ())->method ('get ' )->willReturn ($ objectManager );
108
+ ->willReturn ($ appFactory );
109
+ $ this -> objectManagerProvider ->expects ($ this ->any ())->method ('get ' )->willReturn ($ objectManager );
52
110
53
111
$ directoryWrite = $ this ->getMockForAbstractClass (\Magento \Framework \Filesystem \Directory \WriteInterface::class);
54
112
$ directoryRead = $ this ->getMockForAbstractClass (\Magento \Framework \Filesystem \Directory \ReadInterface::class);
55
- $ filesystem ->expects ($ this ->any ())->method ('getDirectoryRead ' )->will ($ this ->returnValue ($ directoryRead ));
56
- $ filesystem ->expects ($ this ->any ())
113
+ $ this -> filesystem ->expects ($ this ->any ())->method ('getDirectoryRead ' )->will ($ this ->returnValue ($ directoryRead ));
114
+ $ this -> filesystem ->expects ($ this ->any ())
57
115
->method ('getDirectoryWrite ' )
58
116
->will ($ this ->returnValue ($ directoryWrite ));
59
117
$ directoryWrite ->expects ($ this ->any ())->method ('isExist ' )->willReturn (true );
@@ -81,32 +139,41 @@ public function setUp()
81
139
. '}}} '
82
140
);
83
141
84
- $ typeMapper = $ this ->getMockBuilder (\Magento \Setup \Model \Grid \TypeMapper::class)
142
+ $ this -> typeMapper = $ this ->getMockBuilder (\Magento \Setup \Model \Grid \TypeMapper::class)
85
143
->disableOriginalConstructor ()
86
144
->getMock ();
87
- $ typeMapper ->expects (static ::any ())
145
+ $ this -> typeMapper ->expects (static ::any ())
88
146
->method ('map ' )
89
147
->willReturnMap ([
90
148
[ComposerInformation::MODULE_PACKAGE_TYPE , \Magento \Setup \Model \Grid \TypeMapper::MODULE_PACKAGE_TYPE ],
91
149
]);
92
150
151
+ $ this ->createPackagesData ();
152
+ }
153
+
154
+ private function createPackagesData ()
155
+ {
93
156
$ this ->packagesData = new PackagesData (
94
- $ composerInformation ,
95
- $ timeZoneProvider ,
96
- $ packagesAuth ,
97
- $ filesystem ,
98
- $ objectManagerProvider ,
99
- $ typeMapper
157
+ $ this -> composerInformation ,
158
+ $ this -> timeZoneProvider ,
159
+ $ this -> packagesAuth ,
160
+ $ this -> filesystem ,
161
+ $ this -> objectManagerProvider ,
162
+ $ this -> typeMapper
100
163
);
101
164
}
102
165
103
166
/**
167
+ * @param array $requiredPackages
168
+ * @param array $installedPackages
169
+ * @param array $repo
104
170
* @return ComposerInformation|MockObject
105
171
*/
106
- private function getComposerInformation ()
172
+ private function getComposerInformation ($ requiredPackages = [], $ installedPackages = [], $ repo = [] )
107
173
{
108
174
$ composerInformation = $ this ->getMock (ComposerInformation::class, [], [], '' , false );
109
175
$ composerInformation ->expects ($ this ->any ())->method ('getInstalledMagentoPackages ' )->willReturn (
176
+ $ installedPackages ?:
110
177
[
111
178
'magento/package-1 ' => [
112
179
'name ' => 'magento/package-1 ' ,
@@ -117,21 +184,30 @@ private function getComposerInformation()
117
184
'name ' => 'magento/package-2 ' ,
118
185
'type ' => 'magento2-module ' ,
119
186
'version ' => '1.0.1 '
120
- ]
187
+ ],
188
+ 'partner/package-3 ' => [
189
+ 'name ' => 'partner/package-3 ' ,
190
+ 'type ' => 'magento2-module ' ,
191
+ 'version ' => '3.0.0 '
192
+ ],
121
193
]
122
194
);
123
195
124
196
$ composerInformation ->expects ($ this ->any ())->method ('getRootRepositories ' )
125
- ->willReturn (['repo1 ' , 'repo2 ' ]);
197
+ ->willReturn ($ repo ?: ['repo1 ' , 'repo2 ' ]);
126
198
$ composerInformation ->expects ($ this ->any ())->method ('getPackagesTypes ' )
127
199
->willReturn (['magento2-module ' ]);
128
200
$ rootPackage = $ this ->getMock (RootPackage::class, [], ['magento/project ' , '2.1.0 ' , '2 ' ]);
129
201
$ rootPackage ->expects ($ this ->any ())
130
202
->method ('getRequires ' )
131
- ->willReturn ([
132
- 'magento/package-1 ' => '1.0.0 ' ,
133
- 'magento/package-2 ' => '1.0.1 '
134
- ]);
203
+ ->willReturn (
204
+ $ requiredPackages ?:
205
+ [
206
+ 'magento/package-1 ' => '1.0.0 ' ,
207
+ 'magento/package-2 ' => '1.0.1 ' ,
208
+ 'partner/package-3 ' => '3.0.0 ' ,
209
+ ]
210
+ );
135
211
$ composerInformation ->expects ($ this ->any ())
136
212
->method ('getRootPackage ' )
137
213
->willReturn ($ rootPackage );
@@ -146,19 +222,57 @@ public function testSyncPackagesData()
146
222
$ this ->assertArrayHasKey ('date ' , $ latestData ['lastSyncDate ' ]);
147
223
$ this ->assertArrayHasKey ('time ' , $ latestData ['lastSyncDate ' ]);
148
224
$ this ->assertArrayHasKey ('packages ' , $ latestData );
149
- $ this ->assertSame (2 , count ($ latestData ['packages ' ]));
150
- $ this ->assertSame (2 , $ latestData ['countOfUpdate ' ]);
225
+ $ this ->assertSame (3 , count ($ latestData ['packages ' ]));
226
+ $ this ->assertSame (3 , $ latestData ['countOfUpdate ' ]);
151
227
$ this ->assertArrayHasKey ('installPackages ' , $ latestData );
152
228
$ this ->assertSame (1 , count ($ latestData ['installPackages ' ]));
153
229
$ this ->assertSame (1 , $ latestData ['countOfInstall ' ]);
154
230
}
155
231
156
- public function testGetPackagesForUpdate ()
232
+ /**
233
+ * @expectedException \RuntimeException
234
+ * @expectedExceptionMessage Couldn't get available versions for package partner/package-4
235
+ */
236
+ public function testGetPackagesForUpdateWithException ()
157
237
{
238
+ $ requiredPackages = [
239
+ 'partner/package-4 ' => '4.0.4 ' ,
240
+ ];
241
+ $ installedPackages = [
242
+ 'partner/package-4 ' => [
243
+ 'name ' => 'partner/package-4 ' ,
244
+ 'type ' => 'magento2-module ' ,
245
+ 'version ' => '4.0.4 '
246
+ ],
247
+ ];
248
+ $ this ->composerInformation = $ this ->getComposerInformation ($ requiredPackages , $ installedPackages );
249
+ $ this ->createPackagesData ();
250
+ $ this ->packagesData ->getPackagesForUpdate ();
251
+ }
252
+
253
+ public function testPackagesForUpdateFromJson ()
254
+ {
255
+ $ this ->composerInformation = $ this ->getComposerInformation ([], [], ['https://repo1 ' ]);
256
+ $ this ->packagesAuth ->expects ($ this ->atLeastOnce ())
257
+ ->method ('getCredentialBaseUrl ' )
258
+ ->willReturn ('repo1 ' );
259
+ $ this ->createPackagesData ();
158
260
$ packages = $ this ->packagesData ->getPackagesForUpdate ();
159
261
$ this ->assertEquals (2 , count ($ packages ));
160
262
$ this ->assertArrayHasKey ('magento/package-1 ' , $ packages );
263
+ $ this ->assertArrayHasKey ('partner/package-3 ' , $ packages );
264
+ $ firstPackage = array_values ($ packages )[0 ];
265
+ $ this ->assertArrayHasKey ('latestVersion ' , $ firstPackage );
266
+ $ this ->assertArrayHasKey ('versions ' , $ firstPackage );
267
+ }
268
+
269
+ public function testGetPackagesForUpdate ()
270
+ {
271
+ $ packages = $ this ->packagesData ->getPackagesForUpdate ();
272
+ $ this ->assertEquals (3 , count ($ packages ));
273
+ $ this ->assertArrayHasKey ('magento/package-1 ' , $ packages );
161
274
$ this ->assertArrayHasKey ('magento/package-2 ' , $ packages );
275
+ $ this ->assertArrayHasKey ('partner/package-3 ' , $ packages );
162
276
$ firstPackage = array_values ($ packages )[0 ];
163
277
$ this ->assertArrayHasKey ('latestVersion ' , $ firstPackage );
164
278
$ this ->assertArrayHasKey ('versions ' , $ firstPackage );
@@ -167,9 +281,10 @@ public function testGetPackagesForUpdate()
167
281
public function testGetInstalledPackages ()
168
282
{
169
283
$ installedPackages = $ this ->packagesData ->getInstalledPackages ();
170
- $ this ->assertEquals (2 , count ($ installedPackages ));
284
+ $ this ->assertEquals (3 , count ($ installedPackages ));
171
285
$ this ->assertArrayHasKey ('magento/package-1 ' , $ installedPackages );
172
286
$ this ->assertArrayHasKey ('magento/package-2 ' , $ installedPackages );
287
+ $ this ->assertArrayHasKey ('partner/package-3 ' , $ installedPackages );
173
288
}
174
289
175
290
public function testGetMetaPackagesMap ()
0 commit comments