Skip to content

Commit

Permalink
MAGETWO-33080: Preferences, Shared Instance creation and Compiled Fac…
Browse files Browse the repository at this point in the history
…tory optimization

- shared instances are passed by reference
- compiled factory is using shared instances directly
  • Loading branch information
dkvashninbay committed Feb 9, 2015
1 parent 3318028 commit 33ebc24
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function __construct($config, $objectManager = null, $definitions = null,
/**
* Create instance with call time arguments
*
* @param string $requestedType
* @param string $type
* @param array $arguments
* @return object
* @throws \BadMethodCallException
*/
public function create($requestedType, array $arguments = [])
public function create($type, array $arguments = [])
{
throw new \BadMethodCallException(__METHOD__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Framework\ObjectManager\Environment;

require '_files/CompiledTesting.php';

class CompiledTest extends \PHPUnit_Framework_TestCase
{
/**
Expand All @@ -15,7 +17,7 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$envFactoryMock = $this->getMock('Magento\Framework\ObjectManager\EnvironmentFactory', [], [], '', false);
$this->_compiled = new \Magento\Framework\ObjectManager\Environment\Compiled($envFactoryMock);
$this->_compiled = new \Magento\Framework\ObjectManager\Environment\CompiledTesting($envFactoryMock);
}

public function testGetFilePath()
Expand All @@ -27,4 +29,12 @@ public function testGetMode()
{
$this->assertEquals(Compiled::MODE, $this->_compiled->getMode());
}

public function testGetObjectManagerFactory()
{
$this->assertInstanceOf(
'Magento\Framework\ObjectManager\Factory\Compiled',
$this->_compiled->getObjectManagerFactory(['shared_instances' => []])
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
*/

namespace Magento\Framework\ObjectManager\Environment;

require 'ConfigTesting.php';

class CompiledTesting extends Compiled
{
/**
* @return array
*/
protected function getConfigData()
{
return [];
}

/**
* @return \Magento\Framework\Interception\ObjectManager\ConfigInterface
*/
public function getDiConfig()
{
return new ConfigTesting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
*/

namespace Magento\Framework\ObjectManager\Environment;

use Magento\Framework\Interception\ObjectManager\ConfigInterface;
use Magento\Framework\ObjectManager\ConfigCacheInterface;
use Magento\Framework\ObjectManager\RelationsInterface;

class ConfigTesting implements ConfigInterface
{

/**
* Set class relations
*
* @param RelationsInterface $relations
*
* @return void
*/
public function setRelations(RelationsInterface $relations)
{
return;
}

/**
* Set configuration cache instance
*
* @param ConfigCacheInterface $cache
*
* @return void
*/
public function setCache(ConfigCacheInterface $cache)
{
return;
}

/**
* Retrieve list of arguments per type
*
* @param string $type
* @return array
*/
public function getArguments($type)
{
return [];
}

/**
* Check whether type is shared
*
* @param string $type
* @return bool
*/
public function isShared($type)
{
return true;
}

/**
* Retrieve instance type
*
* @param string $instanceName
* @return mixed
*/
public function getInstanceType($instanceName)
{
return $instanceName;
}

/**
* Retrieve preference for type
*
* @param string $type
* @return string
* @throws \LogicException
*/
public function getPreference($type)
{
return $type;
}

/**
* Returns list of virtual types
*
* @return array
*/
public function getVirtualTypes()
{
return [];
}

/**
* Extend configuration
*
* @param array $configuration
* @return void
*/
public function extend(array $configuration)
{
return;
}

/**
* Returns list on preferences
*
* @return array
*/
public function getPreferences()
{
return [];
}

/**
* Set Interception config
*
* @param \Magento\Framework\Interception\ConfigInterface $interceptionConfig
* @return void
*/
public function setInterceptionConfig(\Magento\Framework\Interception\ConfigInterface $interceptionConfig)
{
return;
}

/**
* Retrieve instance type without interception processing
*
* @param string $instanceName
* @return string
*/
public function getOriginalInstanceType($instanceName)
{
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
protected $config;

/**
* Definition list
*
* @var \Magento\Framework\ObjectManager\DefinitionInterface | \PHPUnit_Framework_MockObject_MockObject
* @var Compiled
*/
protected $definitions;
protected $factory;

/**
* @var Compiled
* @var array
*/
protected $factory;
private $sharedInstances;

public function setUp()
{
Expand All @@ -44,36 +42,29 @@ public function setUp()
->setMethods([])
->getMock();

$this->definitions = $this->getMockBuilder('Magento\Framework\ObjectManager\DefinitionInterface')
->setMethods([])
->getMock();

$this->factory = new Compiled($this->config, $this->objectManager, $this->definitions, []);
$this->sharedInstances = [];
$this->factory = new Compiled($this->config, $this->sharedInstances, []);
$this->factory->setObjectManager($this->objectManager);
}

public function testCreateSimple()
{
$expectedConfig = $this->getSimpleConfig();

$requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting';
$sharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting';
$nonSharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting';

$this->config->expects($this->once())
->method('getInstanceType')
->with($requestedType)
->willReturn($requestedType);
$this->config->expects($this->once())
$this->config->expects($this->any())
->method('getArguments')
->with($requestedType)
->willReturn($expectedConfig);

$this->objectManager->expects($this->once())
->method('create')
->with('Dependency\StdClass')
->willReturn(new \StdClass);
$this->objectManager->expects($this->once())
->method('get')
->with('Dependency\Shared\StdClass')
->willReturn(new \StdClass);
->willReturnMap(
[
[$requestedType, $expectedConfig],
[$sharedType, null],
[$nonSharedType, null]
]
);

$this->factory->setArguments(
[
'globalValue' => 'GLOBAL_ARGUMENT',
Expand All @@ -87,8 +78,8 @@ public function testCreateSimple()
'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting',
$result
);
$this->assertInstanceOf('StdClass', $result->getSharedDependency());
$this->assertInstanceOf('StdClass', $result->getNonSharedDependency());
$this->assertInstanceOf($sharedType, $result->getSharedDependency());
$this->assertInstanceOf($nonSharedType, $result->getNonSharedDependency());
$this->assertEquals('value', $result->getValue());
$this->assertEquals(['default_value1', 'default_value2'], $result->getValueArray());
$this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue());
Expand All @@ -100,24 +91,19 @@ public function testCreateSimpleConfiguredArguments()
$expectedConfig = $this->getSimpleNestedConfig();

$requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting';
$sharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting';
$nonSharedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting';

$this->config->expects($this->once())
->method('getInstanceType')
->with($requestedType)
->willReturn($requestedType);
$this->config->expects($this->once())
$this->config->expects($this->any())
->method('getArguments')
->with($requestedType)
->willReturn($expectedConfig);

$this->objectManager->expects($this->exactly(2))
->method('create')
->with('Dependency\StdClass')
->willReturn(new \StdClass);
$this->objectManager->expects($this->exactly(2))
->method('get')
->with('Dependency\Shared\StdClass')
->willReturn(new \StdClass);
->willReturnMap(
[
[$requestedType, $expectedConfig],
[$sharedType, null],
[$nonSharedType, null]
]
);

$this->factory->setArguments(
[
'array_global_existing_argument' => 'GLOBAL_ARGUMENT',
Expand All @@ -132,16 +118,16 @@ public function testCreateSimpleConfiguredArguments()
'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting',
$result
);
$this->assertInstanceOf('StdClass', $result->getSharedDependency());
$this->assertInstanceOf('StdClass', $result->getNonSharedDependency());
$this->assertInstanceOf($sharedType, $result->getSharedDependency());
$this->assertInstanceOf($nonSharedType, $result->getNonSharedDependency());
$this->assertEquals('value', $result->getValue());
$this->assertEquals(
[
'array_value' => 'value',
'array_configured_instance' => new \StdClass,
'array_configured_instance' => new $sharedType,
'array_configured_array' => [
'array_array_value' => 'value',
'array_array_configured_instance' => new \StdClass,
'array_array_configured_instance' => new $nonSharedType,
],
'array_global_argument' => null,
'array_global_existing_argument' => 'GLOBAL_ARGUMENT',
Expand All @@ -162,10 +148,10 @@ private function getSimpleConfig()
{
return [
'nonSharedDependency' => [
'_ins_' => 'Dependency\StdClass',
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
],
'sharedDependency' => [
'_i_' => 'Dependency\Shared\StdClass',
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
],
'value' => [
'_v_' => 'value',
Expand All @@ -192,10 +178,10 @@ private function getSimpleNestedConfig()
{
return [
'nonSharedDependency' => [
'_ins_' => 'Dependency\StdClass',
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
],
'sharedDependency' => [
'_i_' => 'Dependency\Shared\StdClass',
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
],
'value' => [
'_v_' => 'value',
Expand All @@ -204,12 +190,12 @@ private function getSimpleNestedConfig()
'_vac_' => [
'array_value' => 'value',
'array_configured_instance' => [
'_i_' => 'Dependency\Shared\StdClass',
'_i_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencySharedTesting',
],
'array_configured_array' => [
'array_array_value' => 'value',
'array_array_configured_instance' => [
'_ins_' => 'Dependency\StdClass',
'_ins_' => 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\DependencyTesting',
],
],
'array_global_argument' => [
Expand Down
Loading

0 comments on commit 33ebc24

Please sign in to comment.