Skip to content

Commit 67b3f6e

Browse files
Unit tests for variadic arguments in constructor
1 parent 47a3228 commit 67b3f6e

File tree

3 files changed

+177
-11
lines changed

3 files changed

+177
-11
lines changed

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class CompiledTest extends \PHPUnit\Framework\TestCase
3838
/** @var ObjectManager */
3939
private $objectManager;
4040

41+
/**
42+
* Setup tests
43+
*/
4144
protected function setUp()
4245
{
4346
$this->objectManager = new ObjectManager($this);
@@ -57,6 +60,9 @@ protected function setUp()
5760
$this->objectManager->setBackwardCompatibleProperty($this->factory, 'definitions', $this->definitionsMock);
5861
}
5962

63+
/**
64+
* Test create simple
65+
*/
6066
public function testCreateSimple()
6167
{
6268
$expectedConfig = $this->getSimpleConfig();
@@ -106,6 +112,9 @@ public function testCreateSimple()
106112
$this->assertNull($result->getNullValue());
107113
}
108114

115+
/**
116+
* Test create simple configured arguments
117+
*/
109118
public function testCreateSimpleConfiguredArguments()
110119
{
111120
$expectedConfig = $this->getSimpleNestedConfig();
@@ -170,6 +179,9 @@ public function testCreateSimpleConfiguredArguments()
170179
$this->assertNull($result->getNullValue());
171180
}
172181

182+
/**
183+
* Test create get arguments in runtime
184+
*/
173185
public function testCreateGetArgumentsInRuntime()
174186
{
175187
// Stub OM to create test assets
@@ -308,18 +320,21 @@ private function getRuntimeParameters()
308320
1 => DependencyTesting::class,
309321
2 => true,
310322
3 => null,
323+
4 => false,
311324
],
312325
1 => [
313326
0 => 'sharedDependency',
314327
1 => DependencySharedTesting::class,
315328
2 => true,
316329
3 => null,
330+
4 => false,
317331
],
318332
2 => [
319333
0 => 'value',
320334
1 => null,
321335
2 => false,
322336
3 => 'value',
337+
4 => false,
323338
],
324339
3 => [
325340
0 => 'valueArray',
@@ -329,18 +344,21 @@ private function getRuntimeParameters()
329344
0 => 'default_value1',
330345
1 => 'default_value2',
331346
],
347+
4 => false,
332348
],
333349
4 => [
334350
0 => 'globalValue',
335351
1 => null,
336352
2 => false,
337353
3 => '',
354+
4 => false,
338355
],
339356
5 => [
340357
0 => 'nullValue',
341358
1 => null,
342359
2 => false,
343360
3 => null,
361+
4 => false,
344362
],
345363
];
346364
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/FactoryTest.php

Lines changed: 124 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Framework\ObjectManager\Factory\Dynamic\Developer;
1111
use Magento\Framework\ObjectManager\ObjectManager;
1212

13+
/**
14+
* Class FactoryTest
15+
*/
1316
class FactoryTest extends \PHPUnit\Framework\TestCase
1417
{
1518
/**
@@ -27,6 +30,9 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
2730
*/
2831
private $objectManager;
2932

33+
/**
34+
* Setup tests
35+
*/
3036
protected function setUp()
3137
{
3238
$this->config = new Config();
@@ -35,6 +41,9 @@ protected function setUp()
3541
$this->factory->setObjectManager($this->objectManager);
3642
}
3743

44+
/**
45+
* Test create without args
46+
*/
3847
public function testCreateNoArgs()
3948
{
4049
$this->assertInstanceOf('StdClass', $this->factory->create(\StdClass::class));
@@ -55,7 +64,7 @@ public function testResolveArgumentsException()
5564
$definitionsMock = $this->createMock(\Magento\Framework\ObjectManager\DefinitionInterface::class);
5665
$definitionsMock->expects($this->once())->method('getParameters')
5766
->will($this->returnValue([[
58-
'firstParam', 'string', true, 'default_val',
67+
'firstParam', 'string', true, 'default_val', false
5968
]]));
6069

6170
$this->factory = new Developer(
@@ -136,16 +145,16 @@ public function testCreateUsingReflection()
136145
$definitions = $this->createMock(\Magento\Framework\ObjectManager\DefinitionInterface::class);
137146
// should be more than defined in "switch" of create() method
138147
$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],
149158
]));
150159
$factory = new Developer($this->config, null, $definitions);
151160
$result = $factory->create(
@@ -165,4 +174,108 @@ public function testCreateUsingReflection()
165174
);
166175
$this->assertSame(10, $result->getArg(9));
167176
}
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+
}
168281
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture;
7+
8+
/**
9+
* Constructor with variadic argument in constructor
10+
*/
11+
class Variadic
12+
{
13+
/**
14+
* @var OneScalar[]
15+
*/
16+
private $oneScalars;
17+
18+
/**
19+
* Variadic constructor.
20+
* @param OneScalar[] ...$oneScalars
21+
*/
22+
public function __construct(OneScalar ...$oneScalars)
23+
{
24+
$this->oneScalars = $oneScalars;
25+
}
26+
27+
/**
28+
* @param string $key
29+
* @return mixed
30+
*/
31+
public function getOneScalarByKey($key)
32+
{
33+
return $this->oneScalars[$key] ?? null;
34+
}
35+
}

0 commit comments

Comments
 (0)