Skip to content

Commit 0870235

Browse files
Add tests for classes with non variadic and variadic arguments in constructor
1 parent 5d3d6aa commit 0870235

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ protected function resolveArgumentsInRuntime($requestedType, array $parameters,
251251
}
252252

253253
if ($isVariadic && is_array($argument)) {
254-
$resolvedArguments += $argument;
254+
$resolvedArguments = array_merge($resolvedArguments, $argument);
255255
} else {
256256
$this->resolveArgument($argument, $paramType, $paramDefault, $paramName, $requestedType);
257257
$resolvedArguments[] = $argument;

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,131 @@ public function testCreateVariadicFromDiConfig()
314314
$this->assertSame($oneScalar1, $variadic->getOneScalarByKey(0));
315315
$this->assertSame($oneScalar2, $variadic->getOneScalarByKey(1));
316316
}
317+
318+
/**
319+
* Test create objects with non variadic and variadic argument in constructor
320+
*
321+
* @param $createArgs
322+
* @param $expectedFooValue
323+
* @param $expectedArg0
324+
* @param $expectedArg1
325+
* @dataProvider testCreateUsingSemiVariadicDataProvider
326+
*/
327+
public function testCreateUsingSemiVariadic(
328+
$createArgs,
329+
$expectedFooValue,
330+
$expectedArg0,
331+
$expectedArg1
332+
) {
333+
$type = \Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::class;
334+
$definitions = $this->createMock(\Magento\Framework\ObjectManager\DefinitionInterface::class);
335+
336+
$definitions->expects($this->once())->method('getParameters')->with($type)->will(
337+
$this->returnValue(
338+
[
339+
[
340+
'foo',
341+
null,
342+
false,
343+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
344+
false
345+
],
346+
[
347+
'oneScalars',
348+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\OneScalar::class,
349+
false,
350+
[],
351+
true
352+
],
353+
]
354+
)
355+
);
356+
$factory = new Developer($this->config, null, $definitions);
357+
358+
/**
359+
* @var \Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic $semiVariadic
360+
*/
361+
$semiVariadic = is_null($createArgs)
362+
? $factory->create($type)
363+
: $factory->create($type, $createArgs);
364+
365+
$this->assertSame($expectedFooValue, $semiVariadic->getFoo());
366+
$this->assertSame($expectedArg0, $semiVariadic->getOneScalarByKey(0));
367+
$this->assertSame($expectedArg1, $semiVariadic->getOneScalarByKey(1));
368+
}
369+
370+
/**
371+
* @return array
372+
*/
373+
public function testCreateUsingSemiVariadicDataProvider()
374+
{
375+
$oneScalar1 = $this->createMock(\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\OneScalar::class);
376+
$oneScalar2 = $this->createMock(\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\OneScalar::class);
377+
378+
return [
379+
'without_args' => [
380+
null,
381+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
382+
null,
383+
null,
384+
],
385+
'with_empty_args' => [
386+
[],
387+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
388+
null,
389+
null,
390+
],
391+
'only_with_foo_value' => [
392+
[
393+
'foo' => 'baz'
394+
],
395+
'baz',
396+
null,
397+
null,
398+
],
399+
'only_with_oneScalars_empty_value' => [
400+
[
401+
'oneScalars' => []
402+
],
403+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
404+
null,
405+
null,
406+
],
407+
'only_with_oneScalars_full_value' => [
408+
[
409+
'oneScalars' => [
410+
$oneScalar1,
411+
$oneScalar2,
412+
]
413+
],
414+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
415+
$oneScalar1,
416+
$oneScalar2,
417+
],
418+
'with_all_values_defined_in_right_order' => [
419+
[
420+
'foo' => 'baz',
421+
'oneScalars' => [
422+
$oneScalar1,
423+
$oneScalar2,
424+
]
425+
],
426+
'baz',
427+
$oneScalar1,
428+
$oneScalar2,
429+
],
430+
'with_all_values_defined_in_reverse_order' => [
431+
[
432+
'oneScalars' => [
433+
$oneScalar1,
434+
$oneScalar2,
435+
],
436+
'foo' => 'baz',
437+
],
438+
'baz',
439+
$oneScalar1,
440+
$oneScalar2,
441+
],
442+
];
443+
}
317444
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 non variadic and variadic argument in constructor
10+
*/
11+
class SemiVariadic
12+
{
13+
const DEFAULT_FOO_VALUE = 'bar';
14+
/**
15+
* @var OneScalar[]
16+
*/
17+
private $oneScalars;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $foo;
23+
24+
/**
25+
* SemiVariadic constructor.
26+
* @param string $foo
27+
* @param OneScalar[] ...$oneScalars
28+
*/
29+
public function __construct(
30+
string $foo = self::DEFAULT_FOO_VALUE,
31+
OneScalar ...$oneScalars
32+
) {
33+
$this->foo = $foo;
34+
$this->oneScalars = $oneScalars;
35+
}
36+
37+
/**
38+
* @param string $key
39+
* @return mixed
40+
*/
41+
public function getOneScalarByKey($key)
42+
{
43+
return $this->oneScalars[$key] ?? null;
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getFoo(): string
50+
{
51+
return $this->foo;
52+
}
53+
}

0 commit comments

Comments
 (0)