Skip to content

Commit 3318028

Browse files
committed
MAGETWO-33080: Preferences, Shared Instance creation and Compiled Factory optimization
- refactored arguments config processing - added chain - added preferences resolving node
1 parent cebbe1e commit 3318028

File tree

11 files changed

+475
-21
lines changed

11 files changed

+475
-21
lines changed

dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class AreaTest extends \PHPUnit_Framework_TestCase
3333
*/
3434
private $configWriterMock;
3535

36+
/**
37+
* @var Config\Chain\ModificationChain | \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $configChain;
40+
3641
protected function setUp()
3742
{
3843
$this->areaListMock = $this->getMockBuilder('Magento\Framework\App\AreaList')
@@ -47,6 +52,9 @@ protected function setUp()
4752
$this->configWriterMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\WriterInterface')
4853
->disableOriginalConstructor()
4954
->getMock();
55+
$this->configChain = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationChain')
56+
->disableOriginalConstructor()
57+
->getMock();
5058
}
5159

5260
public function testDoOperationEmptyPath()
@@ -55,7 +63,8 @@ public function testDoOperationEmptyPath()
5563
$this->areaListMock,
5664
$this->areaInstancesNamesList,
5765
$this->configReaderMock,
58-
$this->configWriterMock
66+
$this->configWriterMock,
67+
$this->configChain
5968
);
6069

6170
$this->assertNull($areaOperation->doOperation());
@@ -70,22 +79,13 @@ public function testDoOperationGlobalArea()
7079
'preferences' => [],
7180
'instanceTypes' => []
7281
];
73-
$expectedConfig = [
74-
'arguments' => array_map(
75-
function ($arguments) {
76-
return serialize($arguments);
77-
},
78-
$arguments
79-
),
80-
'preferences' => [],
81-
'instanceTypes' => []
82-
];
8382

8483
$areaOperation = new Area(
8584
$this->areaListMock,
8685
$this->areaInstancesNamesList,
8786
$this->configReaderMock,
8887
$this->configWriterMock,
88+
$this->configChain,
8989
[$path]
9090
);
9191

@@ -103,11 +103,16 @@ function ($arguments) {
103103
App\Area::AREA_GLOBAL
104104
)
105105
->willReturn($generatedConfig);
106+
$this->configChain->expects($this->once())
107+
->method('modify')
108+
->with($generatedConfig)
109+
->willReturn($generatedConfig);
110+
106111
$this->configWriterMock->expects($this->once())
107112
->method('write')
108113
->with(
109114
App\Area::AREA_GLOBAL,
110-
$expectedConfig
115+
$generatedConfig
111116
);
112117

113118
$areaOperation->doOperation();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tools\Di\Compiler\Config\Chain;
8+
9+
class ArgumentsSerializationTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testModifyArgumentsDoNotExist()
12+
{
13+
$inputConfig = [
14+
'data' => []
15+
];
16+
$modifier = new ArgumentsSerialization();
17+
$this->assertSame($inputConfig, $modifier->modify($inputConfig));
18+
}
19+
20+
public function testModifyArguments()
21+
{
22+
$inputConfig = [
23+
'arguments' => [
24+
'argument1' => [],
25+
'argument2' => null,
26+
]
27+
];
28+
29+
$expected = [
30+
'arguments' => [
31+
'argument1' => serialize([]),
32+
'argument2' => null,
33+
]
34+
];
35+
36+
$modifier = new ArgumentsSerialization();
37+
$this->assertEquals($expected, $modifier->modify($inputConfig));
38+
}
39+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tools\Di\Compiler\Config\Chain;
8+
9+
class ModificationChainTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testConstructor()
12+
{
13+
$modificationsList = [];
14+
$modificationsList[] = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
15+
->getMock();
16+
$modificationsList[] = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
17+
->getMock();
18+
19+
new ModificationChain($modificationsList);
20+
}
21+
22+
public function testConstructorException()
23+
{
24+
$this->setExpectedException('InvalidArgumentException', 'Wrong modifier provided');
25+
$modificationsList = [];
26+
$modificationsList[] = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
27+
->getMock();
28+
$modificationsList[] = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
29+
->getMock();
30+
$modificationsList[] = 'banana';
31+
32+
new ModificationChain($modificationsList);
33+
}
34+
35+
public function testModify()
36+
{
37+
$inputArray = [
38+
'data' => [1, 2, 3]
39+
];
40+
41+
$expectedArray1 = [
42+
'data' => [1, 2, 3, 1]
43+
];
44+
45+
$expectedArray2 = [
46+
'data' => [1, 2, 3, 1, 1]
47+
];
48+
49+
$modifier1 = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
50+
->getMock();
51+
$modifier2 = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Chain\ModificationInterface')
52+
->getMock();
53+
54+
$modificationsList = [$modifier1, $modifier2];
55+
56+
$modifier1->expects($this->once())
57+
->method('modify')
58+
->with($inputArray)
59+
->willReturn($expectedArray1);
60+
61+
$modifier2->expects($this->once())
62+
->method('modify')
63+
->with($expectedArray1)
64+
->willReturn($expectedArray2);
65+
66+
$chain = new ModificationChain($modificationsList);
67+
68+
$this->assertEquals($expectedArray2, $chain->modify($inputArray));
69+
}
70+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tools\Di\Compiler\Config\Chain;
8+
9+
class PreferencesResolvingTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testEmptyConfigModify()
12+
{
13+
$inputConfig = [
14+
'data' => []
15+
];
16+
17+
$modification = new PreferencesResolving();
18+
$this->assertSame($inputConfig, $modification->modify($inputConfig));
19+
}
20+
21+
public function testPreferencesResolvingModify()
22+
{
23+
$inputConfig = [
24+
'arguments' => $this->getInputArguments(),
25+
'preferences' => $this->getPreferences()
26+
];
27+
$outputConfig = [
28+
'arguments' => $this->getOutputArguments(),
29+
'preferences' => $this->getPreferences()
30+
];
31+
32+
$modification = new PreferencesResolving();
33+
$this->assertEquals($outputConfig, $modification->modify($inputConfig));
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
private function getInputArguments()
40+
{
41+
return [
42+
'SimpleClass' => [
43+
'type_dependency' => [
44+
'_ins_' => 'Type\DependencyInterface',
45+
],
46+
'type_dependency_shared' => [
47+
'_i_' => 'Type\Dependency\SharedInterface',
48+
],
49+
'value' => [
50+
'_v_' => 'value',
51+
],
52+
'value_array' => [
53+
'_v_' => ['default_value1', 'default_value2'],
54+
],
55+
'value_null' => [
56+
'_vn_' => true,
57+
],
58+
],
59+
'ComplexClass' => [
60+
'type_dependency_configured' => [
61+
'_ins_' => 'Type\Dependency\ConfiguredInterface',
62+
],
63+
'type_dependency_shared_configured' => [
64+
'_i_' => 'Type\Dependency\Shared\Configured',
65+
],
66+
'global_argument' => [
67+
'_a_' => 'global_argument_configured',
68+
'_d_' => null
69+
],
70+
'global_argument_def' => [
71+
'_a_' => 'global_argument_configured',
72+
'_d_' => []
73+
],
74+
'value_configured' => [
75+
'_v_' => 'value_configured',
76+
],
77+
'value_array_configured' => [
78+
'_vac_' => [
79+
'array_value' => 'value',
80+
'array_configured_instance' => [
81+
'_i_' => 'Type\Dependency\Shared\ConfiguredInterface',
82+
],
83+
'array_configured_array' => [
84+
'array_array_value' => 'value',
85+
'array_array_configured_instance' => [
86+
'_ins_' => 'Type\Dependency\Shared\Configured',
87+
],
88+
],
89+
'array_global_argument' => [
90+
'_a_' => 'global_argument_configured',
91+
'_d_' => null
92+
]
93+
],
94+
],
95+
'value_null' => [
96+
'_vn_' => true,
97+
],
98+
]
99+
];
100+
}
101+
102+
/**
103+
* @return array
104+
*/
105+
private function getOutputArguments()
106+
{
107+
return [
108+
'SimpleClass' => [
109+
'type_dependency' => [
110+
'_ins_' => 'Type\Dependency',
111+
],
112+
'type_dependency_shared' => [
113+
'_i_' => 'Type\Dependency\Shared',
114+
],
115+
'value' => [
116+
'_v_' => 'value',
117+
],
118+
'value_array' => [
119+
'_v_' => ['default_value1', 'default_value2'],
120+
],
121+
'value_null' => [
122+
'_vn_' => true,
123+
],
124+
],
125+
'ComplexClass' => [
126+
'type_dependency_configured' => [
127+
'_ins_' => 'Type\Dependency\Configured',
128+
],
129+
'type_dependency_shared_configured' => [
130+
'_i_' => 'Type\Dependency\Shared\Configured',
131+
],
132+
'global_argument' => [
133+
'_a_' => 'global_argument_configured',
134+
'_d_' => null
135+
],
136+
'global_argument_def' => [
137+
'_a_' => 'global_argument_configured',
138+
'_d_' => []
139+
],
140+
'value_configured' => [
141+
'_v_' => 'value_configured',
142+
],
143+
'value_array_configured' => [
144+
'_vac_' => [
145+
'array_value' => 'value',
146+
'array_configured_instance' => [
147+
'_i_' => 'Type\Dependency\Shared\Configured',
148+
],
149+
'array_configured_array' => [
150+
'array_array_value' => 'value',
151+
'array_array_configured_instance' => [
152+
'_ins_' => 'Type\Dependency\Shared\Configured',
153+
],
154+
],
155+
'array_global_argument' => [
156+
'_a_' => 'global_argument_configured',
157+
'_d_' => null
158+
]
159+
],
160+
],
161+
'value_null' => [
162+
'_vn_' => true,
163+
],
164+
]
165+
];
166+
}
167+
168+
/**
169+
* @return array
170+
*/
171+
private function getPreferences()
172+
{
173+
return [
174+
'Type\DependencyInterface' => 'Type\Dependency',
175+
'Type\Dependency\SharedInterface' => 'Type\Dependency\Shared',
176+
'Type\Dependency\ConfiguredInterface' => 'Type\Dependency\Configured',
177+
'Type\Dependency\Shared\ConfiguredInterface' => 'Type\Dependency\Shared\Configured',
178+
];
179+
}
180+
}

dev/tools/Magento/Tools/Di/App/Compiler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public function launch()
6262
'Magento\Tools\Di\Compiler\Config\Writer\Filesystem',
6363
'Magento\Tools\Di\Compiler\Log\Writer\WriterInterface' =>
6464
'Magento\Tools\Di\Compiler\Log\Writer\Console'
65+
],
66+
'Magento\Tools\Di\Compiler\Config\Chain\ModificationChain' => [
67+
'arguments' => [
68+
'modificationsList' => [
69+
'ArgumentsSerialization' =>
70+
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\ArgumentsSerialization'],
71+
'PreferencesResolving' =>
72+
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\PreferencesResolving'],
73+
]
74+
]
6575
]
6676
]
6777
);

0 commit comments

Comments
 (0)