Skip to content

Commit 33ebc24

Browse files
committed
MAGETWO-33080: Preferences, Shared Instance creation and Compiled Factory optimization
- shared instances are passed by reference - compiled factory is using shared instances directly
1 parent 3318028 commit 33ebc24

File tree

18 files changed

+389
-117
lines changed

18 files changed

+389
-117
lines changed

dev/tests/unit/testsuite/Magento/Framework/App/FactoryStub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function __construct($config, $objectManager = null, $definitions = null,
2525
/**
2626
* Create instance with call time arguments
2727
*
28-
* @param string $requestedType
28+
* @param string $type
2929
* @param array $arguments
3030
* @return object
3131
* @throws \BadMethodCallException
3232
*/
33-
public function create($requestedType, array $arguments = [])
33+
public function create($type, array $arguments = [])
3434
{
3535
throw new \BadMethodCallException(__METHOD__);
3636
}

dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Environment/CompiledTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\ObjectManager\Environment;
77

8+
require '_files/CompiledTesting.php';
9+
810
class CompiledTest extends \PHPUnit_Framework_TestCase
911
{
1012
/**
@@ -15,7 +17,7 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
1517
protected function setUp()
1618
{
1719
$envFactoryMock = $this->getMock('Magento\Framework\ObjectManager\EnvironmentFactory', [], [], '', false);
18-
$this->_compiled = new \Magento\Framework\ObjectManager\Environment\Compiled($envFactoryMock);
20+
$this->_compiled = new \Magento\Framework\ObjectManager\Environment\CompiledTesting($envFactoryMock);
1921
}
2022

2123
public function testGetFilePath()
@@ -27,4 +29,12 @@ public function testGetMode()
2729
{
2830
$this->assertEquals(Compiled::MODE, $this->_compiled->getMode());
2931
}
32+
33+
public function testGetObjectManagerFactory()
34+
{
35+
$this->assertInstanceOf(
36+
'Magento\Framework\ObjectManager\Factory\Compiled',
37+
$this->_compiled->getObjectManagerFactory(['shared_instances' => []])
38+
);
39+
}
3040
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
4+
*/
5+
6+
namespace Magento\Framework\ObjectManager\Environment;
7+
8+
require 'ConfigTesting.php';
9+
10+
class CompiledTesting extends Compiled
11+
{
12+
/**
13+
* @return array
14+
*/
15+
protected function getConfigData()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @return \Magento\Framework\Interception\ObjectManager\ConfigInterface
22+
*/
23+
public function getDiConfig()
24+
{
25+
return new ConfigTesting();
26+
}
27+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
4+
*/
5+
6+
namespace Magento\Framework\ObjectManager\Environment;
7+
8+
use Magento\Framework\Interception\ObjectManager\ConfigInterface;
9+
use Magento\Framework\ObjectManager\ConfigCacheInterface;
10+
use Magento\Framework\ObjectManager\RelationsInterface;
11+
12+
class ConfigTesting implements ConfigInterface
13+
{
14+
15+
/**
16+
* Set class relations
17+
*
18+
* @param RelationsInterface $relations
19+
*
20+
* @return void
21+
*/
22+
public function setRelations(RelationsInterface $relations)
23+
{
24+
return;
25+
}
26+
27+
/**
28+
* Set configuration cache instance
29+
*
30+
* @param ConfigCacheInterface $cache
31+
*
32+
* @return void
33+
*/
34+
public function setCache(ConfigCacheInterface $cache)
35+
{
36+
return;
37+
}
38+
39+
/**
40+
* Retrieve list of arguments per type
41+
*
42+
* @param string $type
43+
* @return array
44+
*/
45+
public function getArguments($type)
46+
{
47+
return [];
48+
}
49+
50+
/**
51+
* Check whether type is shared
52+
*
53+
* @param string $type
54+
* @return bool
55+
*/
56+
public function isShared($type)
57+
{
58+
return true;
59+
}
60+
61+
/**
62+
* Retrieve instance type
63+
*
64+
* @param string $instanceName
65+
* @return mixed
66+
*/
67+
public function getInstanceType($instanceName)
68+
{
69+
return $instanceName;
70+
}
71+
72+
/**
73+
* Retrieve preference for type
74+
*
75+
* @param string $type
76+
* @return string
77+
* @throws \LogicException
78+
*/
79+
public function getPreference($type)
80+
{
81+
return $type;
82+
}
83+
84+
/**
85+
* Returns list of virtual types
86+
*
87+
* @return array
88+
*/
89+
public function getVirtualTypes()
90+
{
91+
return [];
92+
}
93+
94+
/**
95+
* Extend configuration
96+
*
97+
* @param array $configuration
98+
* @return void
99+
*/
100+
public function extend(array $configuration)
101+
{
102+
return;
103+
}
104+
105+
/**
106+
* Returns list on preferences
107+
*
108+
* @return array
109+
*/
110+
public function getPreferences()
111+
{
112+
return [];
113+
}
114+
115+
/**
116+
* Set Interception config
117+
*
118+
* @param \Magento\Framework\Interception\ConfigInterface $interceptionConfig
119+
* @return void
120+
*/
121+
public function setInterceptionConfig(\Magento\Framework\Interception\ConfigInterface $interceptionConfig)
122+
{
123+
return;
124+
}
125+
126+
/**
127+
* Retrieve instance type without interception processing
128+
*
129+
* @param string $instanceName
130+
* @return string
131+
*/
132+
public function getOriginalInstanceType($instanceName)
133+
{
134+
return '';
135+
}
136+
}

dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
2323
protected $config;
2424

2525
/**
26-
* Definition list
27-
*
28-
* @var \Magento\Framework\ObjectManager\DefinitionInterface | \PHPUnit_Framework_MockObject_MockObject
26+
* @var Compiled
2927
*/
30-
protected $definitions;
28+
protected $factory;
3129

3230
/**
33-
* @var Compiled
31+
* @var array
3432
*/
35-
protected $factory;
33+
private $sharedInstances;
3634

3735
public function setUp()
3836
{
@@ -44,36 +42,29 @@ public function setUp()
4442
->setMethods([])
4543
->getMock();
4644

47-
$this->definitions = $this->getMockBuilder('Magento\Framework\ObjectManager\DefinitionInterface')
48-
->setMethods([])
49-
->getMock();
50-
51-
$this->factory = new Compiled($this->config, $this->objectManager, $this->definitions, []);
45+
$this->sharedInstances = [];
46+
$this->factory = new Compiled($this->config, $this->sharedInstances, []);
47+
$this->factory->setObjectManager($this->objectManager);
5248
}
5349

5450
public function testCreateSimple()
5551
{
5652
$expectedConfig = $this->getSimpleConfig();
5753

5854
$requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting';
55+
$sharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting';
56+
$nonSharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting';
5957

60-
$this->config->expects($this->once())
61-
->method('getInstanceType')
62-
->with($requestedType)
63-
->willReturn($requestedType);
64-
$this->config->expects($this->once())
58+
$this->config->expects($this->any())
6559
->method('getArguments')
66-
->with($requestedType)
67-
->willReturn($expectedConfig);
68-
69-
$this->objectManager->expects($this->once())
70-
->method('create')
71-
->with('Dependency\StdClass')
72-
->willReturn(new \StdClass);
73-
$this->objectManager->expects($this->once())
74-
->method('get')
75-
->with('Dependency\Shared\StdClass')
76-
->willReturn(new \StdClass);
60+
->willReturnMap(
61+
[
62+
[$requestedType, $expectedConfig],
63+
[$sharedType, null],
64+
[$nonSharedType, null]
65+
]
66+
);
67+
7768
$this->factory->setArguments(
7869
[
7970
'globalValue' => 'GLOBAL_ARGUMENT',
@@ -87,8 +78,8 @@ public function testCreateSimple()
8778
'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting',
8879
$result
8980
);
90-
$this->assertInstanceOf('StdClass', $result->getSharedDependency());
91-
$this->assertInstanceOf('StdClass', $result->getNonSharedDependency());
81+
$this->assertInstanceOf($sharedType, $result->getSharedDependency());
82+
$this->assertInstanceOf($nonSharedType, $result->getNonSharedDependency());
9283
$this->assertEquals('value', $result->getValue());
9384
$this->assertEquals(['default_value1', 'default_value2'], $result->getValueArray());
9485
$this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue());
@@ -100,24 +91,19 @@ public function testCreateSimpleConfiguredArguments()
10091
$expectedConfig = $this->getSimpleNestedConfig();
10192

10293
$requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting';
94+
$sharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting';
95+
$nonSharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting';
10396

104-
$this->config->expects($this->once())
105-
->method('getInstanceType')
106-
->with($requestedType)
107-
->willReturn($requestedType);
108-
$this->config->expects($this->once())
97+
$this->config->expects($this->any())
10998
->method('getArguments')
110-
->with($requestedType)
111-
->willReturn($expectedConfig);
112-
113-
$this->objectManager->expects($this->exactly(2))
114-
->method('create')
115-
->with('Dependency\StdClass')
116-
->willReturn(new \StdClass);
117-
$this->objectManager->expects($this->exactly(2))
118-
->method('get')
119-
->with('Dependency\Shared\StdClass')
120-
->willReturn(new \StdClass);
99+
->willReturnMap(
100+
[
101+
[$requestedType, $expectedConfig],
102+
[$sharedType, null],
103+
[$nonSharedType, null]
104+
]
105+
);
106+
121107
$this->factory->setArguments(
122108
[
123109
'array_global_existing_argument' => 'GLOBAL_ARGUMENT',
@@ -132,16 +118,16 @@ public function testCreateSimpleConfiguredArguments()
132118
'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting',
133119
$result
134120
);
135-
$this->assertInstanceOf('StdClass', $result->getSharedDependency());
136-
$this->assertInstanceOf('StdClass', $result->getNonSharedDependency());
121+
$this->assertInstanceOf($sharedType, $result->getSharedDependency());
122+
$this->assertInstanceOf($nonSharedType, $result->getNonSharedDependency());
137123
$this->assertEquals('value', $result->getValue());
138124
$this->assertEquals(
139125
[
140126
'array_value' => 'value',
141-
'array_configured_instance' => new \StdClass,
127+
'array_configured_instance' => new $sharedType,
142128
'array_configured_array' => [
143129
'array_array_value' => 'value',
144-
'array_array_configured_instance' => new \StdClass,
130+
'array_array_configured_instance' => new $nonSharedType,
145131
],
146132
'array_global_argument' => null,
147133
'array_global_existing_argument' => 'GLOBAL_ARGUMENT',
@@ -162,10 +148,10 @@ private function getSimpleConfig()
162148
{
163149
return [
164150
'nonSharedDependency' => [
165-
'_ins_' => 'Dependency\StdClass',
151+
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
166152
],
167153
'sharedDependency' => [
168-
'_i_' => 'Dependency\Shared\StdClass',
154+
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
169155
],
170156
'value' => [
171157
'_v_' => 'value',
@@ -192,10 +178,10 @@ private function getSimpleNestedConfig()
192178
{
193179
return [
194180
'nonSharedDependency' => [
195-
'_ins_' => 'Dependency\StdClass',
181+
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
196182
],
197183
'sharedDependency' => [
198-
'_i_' => 'Dependency\Shared\StdClass',
184+
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
199185
],
200186
'value' => [
201187
'_v_' => 'value',
@@ -204,12 +190,12 @@ private function getSimpleNestedConfig()
204190
'_vac_' => [
205191
'array_value' => 'value',
206192
'array_configured_instance' => [
207-
'_i_' => 'Dependency\Shared\StdClass',
193+
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
208194
],
209195
'array_configured_array' => [
210196
'array_array_value' => 'value',
211197
'array_array_configured_instance' => [
212-
'_ins_' => 'Dependency\StdClass',
198+
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
213199
],
214200
],
215201
'array_global_argument' => [

0 commit comments

Comments
 (0)