10
10
use Magento \Framework \ObjectManager \Factory \Dynamic \Developer ;
11
11
use Magento \Framework \ObjectManager \ObjectManager ;
12
12
13
+ /**
14
+ * Class FactoryTest
15
+ */
13
16
class FactoryTest extends \PHPUnit \Framework \TestCase
14
17
{
15
18
/**
@@ -27,6 +30,9 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
27
30
*/
28
31
private $ objectManager ;
29
32
33
+ /**
34
+ * Setup tests
35
+ */
30
36
protected function setUp ()
31
37
{
32
38
$ this ->config = new Config ();
@@ -35,6 +41,9 @@ protected function setUp()
35
41
$ this ->factory ->setObjectManager ($ this ->objectManager );
36
42
}
37
43
44
+ /**
45
+ * Test create without args
46
+ */
38
47
public function testCreateNoArgs ()
39
48
{
40
49
$ this ->assertInstanceOf ('StdClass ' , $ this ->factory ->create (\StdClass::class));
@@ -55,7 +64,7 @@ public function testResolveArgumentsException()
55
64
$ definitionsMock = $ this ->createMock (\Magento \Framework \ObjectManager \DefinitionInterface::class);
56
65
$ definitionsMock ->expects ($ this ->once ())->method ('getParameters ' )
57
66
->will ($ this ->returnValue ([[
58
- 'firstParam ' , 'string ' , true , 'default_val ' ,
67
+ 'firstParam ' , 'string ' , true , 'default_val ' , false
59
68
]]));
60
69
61
70
$ this ->factory = new Developer (
@@ -136,16 +145,16 @@ public function testCreateUsingReflection()
136
145
$ definitions = $ this ->createMock (\Magento \Framework \ObjectManager \DefinitionInterface::class);
137
146
// should be more than defined in "switch" of create() method
138
147
$ definitions ->expects ($ this ->once ())->method ('getParameters ' )->with ($ type )->will ($ this ->returnValue ([
139
- ['one ' , null , false , null ],
140
- ['two ' , null , false , null ],
141
- ['three ' , null , false , null ],
142
- ['four ' , null , false , null ],
143
- ['five ' , null , false , null ],
144
- ['six ' , null , false , null ],
145
- ['seven ' , null , false , null ],
146
- ['eight ' , null , false , null ],
147
- ['nine ' , null , false , null ],
148
- ['ten ' , null , false , null ],
148
+ ['one ' , null , false , null , false ],
149
+ ['two ' , null , false , null , false ],
150
+ ['three ' , null , false , null , false ],
151
+ ['four ' , null , false , null , false ],
152
+ ['five ' , null , false , null , false ],
153
+ ['six ' , null , false , null , false ],
154
+ ['seven ' , null , false , null , false ],
155
+ ['eight ' , null , false , null , false ],
156
+ ['nine ' , null , false , null , false ],
157
+ ['ten ' , null , false , null , false ],
149
158
]));
150
159
$ factory = new Developer ($ this ->config , null , $ definitions );
151
160
$ result = $ factory ->create (
@@ -165,4 +174,108 @@ public function testCreateUsingReflection()
165
174
);
166
175
$ this ->assertSame (10 , $ result ->getArg (9 ));
167
176
}
177
+
178
+ /**
179
+ * Test create objects with variadic argument in constructor
180
+ *
181
+ * @param $createArgs
182
+ * @param $expectedArg0
183
+ * @param $expectedArg1
184
+ * @dataProvider testCreateUsingVariadicDataProvider
185
+ */
186
+ public function testCreateUsingVariadic (
187
+ $ createArgs ,
188
+ $ expectedArg0 ,
189
+ $ expectedArg1
190
+ ) {
191
+ $ type = \Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \Variadic::class;
192
+ $ definitions = $ this ->createMock (\Magento \Framework \ObjectManager \DefinitionInterface::class);
193
+
194
+ $ definitions ->expects ($ this ->once ())->method ('getParameters ' )->with ($ type )->will ($ this ->returnValue ([
195
+ [
196
+ 'oneScalars ' ,
197
+ \Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \OneScalar::class,
198
+ false ,
199
+ [],
200
+ true
201
+ ],
202
+ ]));
203
+ $ factory = new Developer ($ this ->config , null , $ definitions );
204
+
205
+
206
+
207
+ /** @var \Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\Variadic $variadic */
208
+ $ variadic = is_null ($ createArgs )
209
+ ? $ factory ->create ($ type )
210
+ : $ factory ->create ($ type , $ createArgs );
211
+
212
+ $ this ->assertSame ($ expectedArg0 , $ variadic ->getOneScalarByKey (0 ));
213
+ $ this ->assertSame ($ expectedArg1 , $ variadic ->getOneScalarByKey (1 ));
214
+ }
215
+
216
+ /**
217
+ * @return array
218
+ */
219
+ public function testCreateUsingVariadicDataProvider () {
220
+ $ oneScalar1 = $ this ->createMock (\Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \OneScalar::class);
221
+ $ oneScalar2 = $ this ->createMock (\Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \OneScalar::class);
222
+
223
+ return [
224
+ 'without_args ' => [
225
+ null ,
226
+ null ,
227
+ null ,
228
+ ],
229
+ 'with_empty_args ' => [
230
+ [],
231
+ null ,
232
+ null ,
233
+ ],
234
+ 'with_empty_args_value ' => [
235
+ [
236
+ 'oneScalars ' => []
237
+ ],
238
+ null ,
239
+ null ,
240
+ ],
241
+ 'with_args ' => [
242
+ [
243
+ 'oneScalars ' => [
244
+ $ oneScalar1 ,
245
+ $ oneScalar2 ,
246
+ ]
247
+ ],
248
+ $ oneScalar1 ,
249
+ $ oneScalar2 ,
250
+ ],
251
+ ];
252
+ }
253
+
254
+ /**
255
+ * Test data can be injected into variadic arguments from di config
256
+ */
257
+ public function testCreateVariadicFromDiConfig ()
258
+ {
259
+ $ oneScalar1 = $ this ->createMock (\Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \OneScalar::class);
260
+ $ oneScalar2 = $ this ->createMock (\Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \OneScalar::class);
261
+
262
+ // let's imitate that Variadic is configured by providing DI configuration for it
263
+ $ this ->config ->extend (
264
+ [
265
+ \Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \Variadic::class => [
266
+ 'arguments ' => [
267
+ 'oneScalars ' => [
268
+ $ oneScalar1 ,
269
+ $ oneScalar2 ,
270
+ ]
271
+ ]
272
+ ],
273
+ ]
274
+ );
275
+ /** @var \Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\Variadic $variadic */
276
+ $ variadic = $ this ->factory ->create (\Magento \Framework \ObjectManager \Test \Unit \Factory \Fixture \Variadic::class);
277
+
278
+ $ this ->assertSame ($ oneScalar1 , $ variadic ->getOneScalarByKey (0 ));
279
+ $ this ->assertSame ($ oneScalar2 , $ variadic ->getOneScalarByKey (1 ));
280
+ }
168
281
}
0 commit comments