From c0264f317f308498a91005303ad7dad7e8d3ed47 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Thu, 29 Jan 2015 16:27:12 +0200 Subject: [PATCH 01/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - Cover with unit tests Arguments Resolver to fit new pattern behaviour --- .../Di/Compiler/ArgumentsResolverTest.php | 343 ++++++++++-------- dev/tools/Magento/Tools/Di/App/Compiler.php | 10 +- .../Tools/Di/Compiler/ArgumentsResolver.php | 91 +++-- 3 files changed, 252 insertions(+), 192 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index a916357b873e6..b0fd9e52adec1 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -8,6 +8,8 @@ namespace Magento\Tools\Di\Compiler; +use Magento\Tools\Di\Compiler\ConstructorArgument; + class ArgumentsResolverTest extends \PHPUnit_Framework_TestCase { /** @@ -22,203 +24,230 @@ class ArgumentsResolverTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->diContainerConfig = $this->getMock('Magento\Framework\ObjectManager\ConfigInterface', [], [], '', false); + $this->diContainerConfig = $this->getMock( + 'Magento\Framework\ObjectManager\ConfigInterface', + [], + [], + '', + false + ); $this->model = new \Magento\Tools\Di\Compiler\ArgumentsResolver($this->diContainerConfig); } - /** - * @dataProvider getResolvedConstructorArgumentsNoTypeDataProvider - */ - public function testGetResolvedConstructorArgumentsNoType($constructor, $isRequired, $getType, $isShared, $expected) + public function testGetResolvedArgumentsConstructorFormat() { - $instanceType = ['instance' => 'Magento\Framework\Api\Config\MetadataConfig', 'argument' => 'object']; - $this->diContainerConfig->expects($this->any()) - ->method('getArguments') - ->willReturn(['virtualType' => $instanceType]); - $constructor->expects($this->any()) - ->method('isRequired') - ->willReturn($isRequired); - $constructor->expects($this->any()) - ->method('getDefaultValue') - ->willReturn('Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder'); - $constructor->expects($this->any()) - ->method('getType') - ->willReturn($getType); - $constructor->expects($this->any()) - ->method('getName') - ->willReturn('attributeMetadataBuilder'); + $expectedResultDefault = $this->getResolvedSimpleConfigExpectation(); + + $constructor = [ + new ConstructorArgument(['type_dependency', 'Type\Dependency', true, null]), + new ConstructorArgument(['type_dependency_shared', 'Type\Dependency\Shared', true, null]), + new ConstructorArgument(['value', null, false, 'value']), + new ConstructorArgument(['value_array', null, false, ['default_value1', 'default_value2']]), + ]; $this->diContainerConfig->expects($this->any()) ->method('isShared') - ->willReturn($isShared); + ->willReturnMap( + [ + ['Type\Dependency', false], + ['Type\Dependency\Shared', true] + ] + ); + + $type = 'Class'; + $this->diContainerConfig->expects($this->any()) + ->method('getArguments') + ->with($type) + ->willReturn([]); - $this->assertEquals( - $expected, - $this->model->getResolvedConstructorArguments($instanceType, [$constructor]) + $this->assertSame( + $expectedResultDefault, + $this->model->getResolvedConstructorArguments($type, $constructor) ); } - /** - * @return array - */ - public function getResolvedConstructorArgumentsNoTypeDataProvider() + public function testGetResolvedArgumentsConstructorConfiguredFormat() { - $constructor = $this->getMock('Magento\Tools\Di\Compiler\ConstructorArgument', [], [], '', false); - $expected = [ - 'attributeMetadataBuilder' => ['__val__' => 'Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder'] + $expectedResultConfigured = $this->getResolvedConfigurableConfigExpectation(); + + $constructor = [ + new ConstructorArgument(['type_dependency_configured', 'Type\Dependency', true, null]), + new ConstructorArgument(['type_dependency_shared_configured', 'Type\Dependency\Shared', true, null]), + new ConstructorArgument(['global_argument', null, false, null]), + new ConstructorArgument(['global_argument_def', null, false, []]), + new ConstructorArgument(['value_configured', null, false, 'value']), + new ConstructorArgument(['value_array_configured', null, false, []]), ]; - return [ - [$constructor, false, false, true, $expected] - ]; - } - /** - * @dataProvider getResolvedConstructorArgumentsWithTypeDataProvider - */ - public function testGetResolvedConstructorArgumentsWithType( - $constructor, $isRequired, $getType, $isShared, $expected - ) { - $instanceType = ['instance' => 'Magento\Framework\Api\Config\MetadataConfig']; - if (!$constructor) { - $this->assertNull($this->model->getResolvedConstructorArguments('virtualType', $constructor)); - return; - } - $this->diContainerConfig->expects($this->any()) - ->method('getArguments') - ->willReturn(['virtualType' => $instanceType]); - $constructor->expects($this->any()) - ->method('isRequired') - ->willReturn($isRequired); - $constructor->expects($this->any()) - ->method('getDefaultValue') - ->willReturn('Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder'); - $constructor->expects($this->any()) - ->method('getType') - ->willReturn($getType); - $constructor->expects($this->any()) - ->method('getName') - ->willReturn('virtualType'); + $this->diContainerConfig->expects($this->any()) ->method('isShared') - ->willReturn($isShared); - - $this->assertEquals( - $expected, - $this->model->getResolvedConstructorArguments($instanceType, [$constructor]) + ->willReturnMap( + [ + ['Type\Dependency', false], + ['Type\Dependency\Shared', true], + ['Type\Dependency\Configured', false], + ['Type\Dependency\Shared\Configured', true] + ] + ); + + $type = 'Class'; + $this->diContainerConfig->expects($this->any()) + ->method('getArguments') + ->with($type) + ->willReturn( + $this->getConfiguredArguments() + ); + + $this->assertSame( + $expectedResultConfigured, + $this->model->getResolvedConstructorArguments($type, $constructor) ); } /** + * Returns resolved simple config expectation + * * @return array */ - public function getResolvedConstructorArgumentsWithTypeDataProvider() + private function getResolvedSimpleConfigExpectation() { - $constructor = $this->getMock('Magento\Tools\Di\Compiler\ConstructorArgument', [], [], '', false); return [ - [$constructor, true, 'virtualType', true, ['virtualType' => 'Magento\Framework\Api\Config\MetadataConfig']], + 'type_dependency' => [ + '_i_' => 'Type\Dependency', + '_s_' => false, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + 'type_dependency_shared' => [ + '_i_' => 'Type\Dependency\Shared', + '_s_' => true, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + 'value' => [ + '_i_' => null, + '_s_' => false, + '_v_' => 'value', + '_a_' => null, + '_d_' => null + ], + 'value_array' => [ + '_i_' => null, + '_s_' => false, + '_v_' => ['default_value1', 'default_value2'], + '_a_' => null, + '_d_' => null + ], ]; } - public function testGetResolvedConstructorArgumentsConstructorNull() - { - $this->assertNull($this->model->getResolvedConstructorArguments('virtualType', [])); - } - - /** - * @dataProvider getResolvedConstructorArgumentsWithArgumentDataProvider - */ - public function testGetResolvedConstructorArgumentsWithArgument( - $constructor, $isRequired, $getType, $isShared, $expected - ) { - $instanceType = ['instance' => 'Magento\Framework\Api\Config\MetadataConfig', 'argument' => 'object']; - $this->diContainerConfig->expects($this->any()) - ->method('getArguments') - ->willReturn(['virtualType' => $instanceType]); - $constructor->expects($this->any()) - ->method('isRequired') - ->willReturn($isRequired); - $constructor->expects($this->any()) - ->method('getDefaultValue') - ->willReturn('Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder'); - $constructor->expects($this->any()) - ->method('getType') - ->willReturn($getType); - $constructor->expects($this->any()) - ->method('getName') - ->willReturn('virtualType'); - $this->diContainerConfig->expects($this->any()) - ->method('isShared') - ->willReturn($isShared); - - $this->assertEquals( - $expected, - $this->model->getResolvedConstructorArguments($instanceType, [$constructor]) - ); - } - /** + * Returns configured arguments expectation + * * @return array */ - public function getResolvedConstructorArgumentsWithArgumentDataProvider() + private function getConfiguredArguments() { - $constructor = $this->getMock('Magento\Tools\Di\Compiler\ConstructorArgument', [], [], '', false); - $expected = [ - 'virtualType' => [ - '__arg__' => 'object', - '__default__' => 'Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder' - ] - ]; return [ - [$constructor, false, false, true, $expected] + 'type_dependency_configured' => ['instance' => 'Type\Dependency\Configured'], + 'type_dependency_shared_configured' => ['instance' => 'Type\Dependency\Shared\Configured'], + 'global_argument' => ['argument' => 'global_argument_configured'], + 'global_argument_def' => ['argument' => 'global_argument_configured'], + 'value_configured' => 'value_configured', + 'value_array_configured' => [ + 'array_value' => 'value', + 'array_configured_instance' => ['instance' => 'Type\Dependency\Shared\Configured'], + 'array_configured_array' => [ + 'array_array_value' => 'value', + 'array_array_configured_instance' => [ + 'instance' => 'Type\Dependency\Shared\Configured', + 'shared' => false + ] + ], + 'array_global_argument' => ['argument' => 'global_argument_configured'] + ] ]; } /** - * @dataProvider getResolvedConstructorArgumentsNoSharedDataProvider - */ - public function testGetResolvedConstructorArgumentsNoShared( - $constructor, $isRequired, $getType, $isShared, $expected - ) { - $instanceType = ['instance' => 'Magento\Framework\Api\Config\MetadataConfig', 'argument' => 'object']; - $this->diContainerConfig->expects($this->any()) - ->method('getArguments') - ->willReturn(['virtualType' => $instanceType]); - $constructor->expects($this->any()) - ->method('isRequired') - ->willReturn($isRequired); - $constructor->expects($this->any()) - ->method('getDefaultValue') - ->willReturn('Magento\Customer\Api\Data\Eav\AttributeMetadataDataBuilder'); - $constructor->expects($this->any()) - ->method('getType') - ->willReturn($getType); - $constructor->expects($this->any()) - ->method('getName') - ->willReturn('virtualType'); - $this->diContainerConfig->expects($this->any()) - ->method('isShared') - ->willReturn($isShared); - - $this->assertEquals( - $expected, - $this->model->getResolvedConstructorArguments($instanceType, [$constructor]) - ); - } - - /** + * Returns resolved configurable config expectation + * * @return array */ - public function getResolvedConstructorArgumentsNoSharedDataProvider() + private function getResolvedConfigurableConfigExpectation() { - $constructor = $this->getMock('Magento\Tools\Di\Compiler\ConstructorArgument', [], [], '', false); - $expected = [ - 'virtualType' => [ - '__non_shared__' => true, - '__instance__' => 'Magento\Framework\Api\Config\MetadataConfig' - ] - ]; return [ - [$constructor, false, true, false, $expected] + 'type_dependency_configured' => [ + '_i_' => 'Type\Dependency\Configured', + '_s_' => false, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + 'type_dependency_shared_configured' => [ + '_i_' => 'Type\Dependency\Shared\Configured', + '_s_' => true, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + 'global_argument' => [ + '_i_' => null, + '_s_' => false, + '_v_' => null, + '_a_' => 'global_argument_configured', + '_d_' => null + ], + 'global_argument_def' => [ + '_i_' => null, + '_s_' => false, + '_v_' => null, + '_a_' => 'global_argument_configured', + '_d_' => [] + ], + 'value_configured' => [ + '_i_' => null, + '_s_' => false, + '_v_' => 'value_configured', + '_a_' => null, + '_d_' => null + ], + 'value_array_configured' => [ + '_i_' => null, + '_s_' => false, + '_v_' => [ + 'array_value' => 'value', + 'array_configured_instance' => [ + '_i_' => 'Type\Dependency\Shared\Configured', + '_s_' => true, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + 'array_configured_array' => [ + 'array_array_value' => 'value', + 'array_array_configured_instance' => [ + '_i_' => 'Type\Dependency\Shared\Configured', + '_s_' => false, + '_v_' => null, + '_a_' => null, + '_d_' => null + ], + ], + 'array_global_argument' => [ + '_i_' => null, + '_s_' => false, + '_v_' => null, + '_a_' => 'global_argument_configured', + '_d_' => null + ] + ], + '_a_' => null, + '_d_' => null + ] ]; } } diff --git a/dev/tools/Magento/Tools/Di/App/Compiler.php b/dev/tools/Magento/Tools/Di/App/Compiler.php index f57b1ae9cbb77..25c910eba92ac 100644 --- a/dev/tools/Magento/Tools/Di/App/Compiler.php +++ b/dev/tools/Magento/Tools/Di/App/Compiler.php @@ -68,11 +68,11 @@ public function launch() Task\OperationFactory::AREA => [ BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' ], - Task\OperationFactory::INTERCEPTION => - BP . '/var/generation', - Task\OperationFactory::INTERCEPTION_CACHE => [ - BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' - ] + //Task\OperationFactory::INTERCEPTION => + // BP . '/var/generation', + //Task\OperationFactory::INTERCEPTION_CACHE => [ + // BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' + //] ]; $responseCode = Response::SUCCESS; diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index 2499e8a7d7dbc..e9d9dffd7aa59 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -14,6 +14,19 @@ class ArgumentsResolver */ private $diContainerConfig; + /** + * Argument pattern used for configuration + * + * @var array + */ + private $argumentPattern = [ + '_i_' => null, + '_s_' => false, + '_v_' => null, + '_a_' => null, + '_d_' => null + ]; + /** * @param \Magento\Framework\ObjectManager\ConfigInterface $diContainerConfig */ @@ -39,9 +52,9 @@ public function getResolvedConstructorArguments($instanceType, $constructor) $arguments = []; /** @var ConstructorArgument $constructorArgument */ foreach ($constructor as $constructorArgument) { - $argument = self::getNonObjectArgument(null); + $argument = $this->getNonObjectArgument(null); if (!$constructorArgument->isRequired()) { - $argument = self::getNonObjectArgument($constructorArgument->getDefaultValue()); + $argument = $this->getNonObjectArgument($constructorArgument->getDefaultValue()); } elseif ($constructorArgument->getType()) { $argument = $this->getInstanceArgument($constructorArgument->getType()); } @@ -67,12 +80,38 @@ public function getResolvedConstructorArguments($instanceType, $constructor) private function getConfiguredArgument($configuredArgument, ConstructorArgument $constructorArgument) { if ($constructorArgument->getType()) { - return $this->getInstanceArgument($configuredArgument['instance']); + $argument = $this->getInstanceArgument($configuredArgument['instance']); + $argument['_s_'] = isset($configuredArgument['shared']) ? $configuredArgument['shared'] : $argument['_s_']; + return $argument; } elseif (isset($configuredArgument['argument'])) { - return self::getGlobalArgument($configuredArgument['argument'], $constructorArgument->getDefaultValue()); + return $this->getGlobalArgument($configuredArgument['argument'], $constructorArgument->getDefaultValue()); + } + + return $this->getNonObjectArgument($configuredArgument); + } + + private function getConfiguredArrayAttribute($array) + { + foreach ($array as $key => $value) { + if (!is_array($value)) { + continue; + } + + if (isset($value['instance'])) { + $array[$key] = $this->getInstanceArgument($value['instance']); + $array[$key]['_s_'] = isset($value['shared']) ? $value['shared'] : $array[$key]['_s_']; + continue; + } + + if (isset($value['argument'])) { + $array[$key] = $this->getGlobalArgument($value['argument'], null); + continue; + } + + $array[$key] = $this->getConfiguredArrayAttribute($value); } - return self::getNonObjectArgument($configuredArgument); + return $array; } /** @@ -104,23 +143,10 @@ function ($type) { */ private function getInstanceArgument($instanceType) { - return $this->diContainerConfig->isShared($instanceType) - ? $instanceType - : self::getNonSharedInstance($instanceType); - } - - /** - * Returns argument of non shared instance - * - * @param string $instanceType - * @return array - */ - private static function getNonSharedInstance($instanceType) - { - return [ - '__non_shared__' => true, - '__instance__' => $instanceType - ]; + $argument = $this->argumentPattern; + $argument['_i_'] = $instanceType; + $argument['_s_'] = $this->diContainerConfig->isShared($instanceType); + return $argument; } /** @@ -129,23 +155,28 @@ private static function getNonSharedInstance($instanceType) * @param mixed $value * @return array */ - private static function getNonObjectArgument($value) + private function getNonObjectArgument($value) { - return ['__val__' => $value]; + $argument = $this->argumentPattern; + if (is_array($value)) { + $value = $this->getConfiguredArrayAttribute($value); + } + $argument['_v_'] = $value; + return $argument; } /** * Returns global argument * - * @param string $argument + * @param string $value * @param string $default * @return array */ - private static function getGlobalArgument($argument, $default) + private function getGlobalArgument($value, $default) { - return [ - '__arg__' => $argument, - '__default__' => $default - ]; + $argument = $this->argumentPattern; + $argument['_a_'] = $value; + $argument['_d_'] = $default; + return $argument; } } From f373e551d0458190718e9dab4d7e10368b0fd7e9 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Thu, 29 Jan 2015 16:58:30 +0200 Subject: [PATCH 02/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - removed excessive config --- .../Di/Compiler/ArgumentsResolverTest.php | 43 ------------------- .../Tools/Di/Compiler/ArgumentsResolver.php | 30 ++++++++++--- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index b0fd9e52adec1..0d58964ead29c 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -116,30 +116,16 @@ private function getResolvedSimpleConfigExpectation() 'type_dependency' => [ '_i_' => 'Type\Dependency', '_s_' => false, - '_v_' => null, - '_a_' => null, - '_d_' => null ], 'type_dependency_shared' => [ '_i_' => 'Type\Dependency\Shared', '_s_' => true, - '_v_' => null, - '_a_' => null, - '_d_' => null ], 'value' => [ - '_i_' => null, - '_s_' => false, '_v_' => 'value', - '_a_' => null, - '_d_' => null ], 'value_array' => [ - '_i_' => null, - '_s_' => false, '_v_' => ['default_value1', 'default_value2'], - '_a_' => null, - '_d_' => null ], ]; } @@ -183,70 +169,41 @@ private function getResolvedConfigurableConfigExpectation() 'type_dependency_configured' => [ '_i_' => 'Type\Dependency\Configured', '_s_' => false, - '_v_' => null, - '_a_' => null, - '_d_' => null ], 'type_dependency_shared_configured' => [ '_i_' => 'Type\Dependency\Shared\Configured', '_s_' => true, - '_v_' => null, - '_a_' => null, - '_d_' => null ], 'global_argument' => [ - '_i_' => null, - '_s_' => false, - '_v_' => null, '_a_' => 'global_argument_configured', '_d_' => null ], 'global_argument_def' => [ - '_i_' => null, - '_s_' => false, - '_v_' => null, '_a_' => 'global_argument_configured', '_d_' => [] ], 'value_configured' => [ - '_i_' => null, - '_s_' => false, '_v_' => 'value_configured', - '_a_' => null, - '_d_' => null ], 'value_array_configured' => [ - '_i_' => null, - '_s_' => false, '_v_' => [ 'array_value' => 'value', 'array_configured_instance' => [ '_i_' => 'Type\Dependency\Shared\Configured', '_s_' => true, - '_v_' => null, - '_a_' => null, - '_d_' => null ], 'array_configured_array' => [ 'array_array_value' => 'value', 'array_array_configured_instance' => [ '_i_' => 'Type\Dependency\Shared\Configured', '_s_' => false, - '_v_' => null, - '_a_' => null, - '_d_' => null ], ], 'array_global_argument' => [ - '_i_' => null, - '_s_' => false, - '_v_' => null, '_a_' => 'global_argument_configured', '_d_' => null ] ], - '_a_' => null, - '_d_' => null ] ]; } diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index e9d9dffd7aa59..e981fbecf0a34 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -15,14 +15,30 @@ class ArgumentsResolver private $diContainerConfig; /** - * Argument pattern used for configuration + * Instance argument pattern used for configuration * * @var array */ - private $argumentPattern = [ + private $instanceArgumentPattern = [ '_i_' => null, '_s_' => false, + ]; + + /** + * Value argument pattern used for configuration + * + * @var array + */ + private $valueArgumentPattern = [ '_v_' => null, + ]; + + /** + * Configured argument pattern used for configuration + * + * @var array + */ + private $configuredArgumentPattern = [ '_a_' => null, '_d_' => null ]; @@ -90,6 +106,10 @@ private function getConfiguredArgument($configuredArgument, ConstructorArgument return $this->getNonObjectArgument($configuredArgument); } + /** + * @param array $array + * @return mixed + */ private function getConfiguredArrayAttribute($array) { foreach ($array as $key => $value) { @@ -143,7 +163,7 @@ function ($type) { */ private function getInstanceArgument($instanceType) { - $argument = $this->argumentPattern; + $argument = $this->instanceArgumentPattern; $argument['_i_'] = $instanceType; $argument['_s_'] = $this->diContainerConfig->isShared($instanceType); return $argument; @@ -157,7 +177,7 @@ private function getInstanceArgument($instanceType) */ private function getNonObjectArgument($value) { - $argument = $this->argumentPattern; + $argument = $this->valueArgumentPattern; if (is_array($value)) { $value = $this->getConfiguredArrayAttribute($value); } @@ -174,7 +194,7 @@ private function getNonObjectArgument($value) */ private function getGlobalArgument($value, $default) { - $argument = $this->argumentPattern; + $argument = $this->configuredArgumentPattern; $argument['_a_'] = $value; $argument['_d_'] = $default; return $argument; From 5c3276ddc58c74f1c93475381081a9d8a80896bc Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 11:56:53 +0200 Subject: [PATCH 03/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - Compiled config covered with test to support new config format --- .../ObjectManager/Factory/CompiledTest.php | 216 ++++++++++++++++++ .../Fixture/Compiled/SimpleClassTesting.php | 81 +++++++ .../Di/Compiler/ArgumentsResolverTest.php | 2 - .../ObjectManager/Factory/Compiled.php | 53 ++++- 4 files changed, 340 insertions(+), 12 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php new file mode 100644 index 0000000000000..5167bfa341972 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php @@ -0,0 +1,216 @@ +objectManager = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->setMethods([]) + ->getMock(); + + $this->config = $this->getMockBuilder('Magento\Framework\ObjectManager\ConfigInterface') + ->setMethods([]) + ->getMock(); + + $this->definitions = $this->getMockBuilder('Magento\Framework\ObjectManager\DefinitionInterface') + ->setMethods([]) + ->getMock(); + + $this->factory = new Compiled($this->config, $this->objectManager, $this->definitions, []); + } + + public function testCreateSimple() + { + $expectedConfig = $this->getSimpleConfig(); + + $requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting'; + + $this->config->expects($this->once()) + ->method('getInstanceType') + ->with($requestedType) + ->willReturn($requestedType); + $this->config->expects($this->once()) + ->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); + + /** @var \Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting $result */ + $result = $this->factory->create($requestedType, []); + + $this->assertInstanceOf( + 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting', + $result + ); + $this->assertInstanceOf('StdClass', $result->getSharedDependency()); + $this->assertInstanceOf('StdClass', $result->getNonSharedDependency()); + $this->assertEquals('value', $result->getValue()); + $this->assertEquals(['default_value1', 'default_value2'], $result->getValueArray()); + } + + public function testCreateSimpleConfiguredArguments() + { + $expectedConfig = $this->getSimpleNestedConfig(); + + $requestedType = 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting'; + + $this->config->expects($this->once()) + ->method('getInstanceType') + ->with($requestedType) + ->willReturn($requestedType); + $this->config->expects($this->once()) + ->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); + $this->factory->setArguments(['array_global_existing_argument' => 'GLOBAL_ARGUMENT']); + + /** @var \Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting $result */ + $result = $this->factory->create($requestedType, []); + + $this->assertInstanceOf( + 'Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting', + $result + ); + $this->assertInstanceOf('StdClass', $result->getSharedDependency()); + $this->assertInstanceOf('StdClass', $result->getNonSharedDependency()); + $this->assertEquals('value', $result->getValue()); + $this->assertEquals( + [ + 'array_value' => 'value', + 'array_configured_instance' => new \StdClass, + 'array_configured_array' => [ + 'array_array_value' => 'value', + 'array_array_configured_instance' => new \StdClass, + ], + 'array_global_argument' => null, + 'array_global_existing_argument' => 'GLOBAL_ARGUMENT', + 'array_global_argument_def' => 'DEFAULT_VALUE' + ], + $result->getValueArray() + ); + } + + /** + * Returns simple config + * + * @return array + */ + private function getSimpleConfig() + { + return [ + 'type_dependency' => [ + '_i_' => 'Dependency\StdClass', + '_s_' => false, + ], + 'type_dependency_shared' => [ + '_i_' => 'Dependency\Shared\StdClass', + '_s_' => true, + ], + 'value' => [ + '_v_' => 'value', + ], + 'value_array' => [ + '_v_' => ['default_value1', 'default_value2'], + ], + ]; + } + + /** + * Returns nested config + * + * @return array + */ + private function getSimpleNestedConfig() + { + return [ + 'type_dependency' => [ + '_i_' => 'Dependency\StdClass', + '_s_' => false, + ], + 'type_dependency_shared' => [ + '_i_' => 'Dependency\Shared\StdClass', + '_s_' => true, + ], + 'value' => [ + '_v_' => 'value', + ], + 'value_array' => [ + '_v_' => [ + 'array_value' => 'value', + 'array_configured_instance' => [ + '_i_' => 'Dependency\Shared\StdClass', + '_s_' => true, + ], + 'array_configured_array' => [ + 'array_array_value' => 'value', + 'array_array_configured_instance' => [ + '_i_' => 'Dependency\StdClass', + '_s_' => false, + ], + ], + 'array_global_argument' => [ + '_a_' => 'global_argument_configured', + '_d_' => null + ], + 'array_global_existing_argument' => [ + '_a_' => 'array_global_existing_argument', + '_d_' => null + ], + 'array_global_argument_def' => [ + '_a_' => 'array_global_argument_def', + '_d_' => 'DEFAULT_VALUE' + ] + ], + ] + ]; + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php new file mode 100644 index 0000000000000..049034de1a8ac --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -0,0 +1,81 @@ +nonSharedDependency = $nonSharedDependency; + $this->sharedDependency = $sharedDependency; + $this->value = $value; + $this->valueArray = $valueArray; + } + + /** + * @return \StdClass + */ + public function getNonSharedDependency() + { + return $this->nonSharedDependency; + } + + /** + * @return \StdClass + */ + public function getSharedDependency() + { + return $this->sharedDependency; + } + + /** + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * @return array + */ + public function getValueArray() + { + return $this->valueArray; + } +} diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index 0d58964ead29c..2a4833aede14b 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -8,8 +8,6 @@ namespace Magento\Tools\Di\Compiler; -use Magento\Tools\Di\Compiler\ConstructorArgument; - class ArgumentsResolverTest extends \PHPUnit_Framework_TestCase { /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 859c4fcde8615..5d46650934042 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -31,22 +31,24 @@ public function create($requestedType, array $arguments = []) $argument = $arguments[$key]; } else { if ($argument === (array)$argument) { - if (isset($argument['__val__']) || array_key_exists('__val__', $argument)) { - $argument = $argument['__val__']; + if (isset($argument['_v_'])) { + $argument = $argument['_v_']; if ($argument === (array)$argument) { $this->parseArray($argument); } - } elseif (isset($argument['__non_shared__'])) { - $argument = $this->objectManager->create($argument['__instance__']); - } elseif (isset($argument['__arg__'])) { - if (isset($this->globalArguments[$argument['__arg__']])) { - $argument = $this->globalArguments[$argument['__arg__']]; + } elseif (isset($argument['_i_'])) { + if ($argument['_s_']) { + $argument = $this->objectManager->get($argument['_i_']); } else { - $argument = $argument['__default__']; + $argument = $this->objectManager->create($argument['_i_']); + } + } elseif (isset($argument['_a_'])) { + if (isset($this->globalArguments[$argument['_a_']])) { + $argument = $this->globalArguments[$argument['_a_']]; + } else { + $argument = $argument['_d_']; } } - } else { - $argument = $this->objectManager->get($argument); } } } @@ -61,4 +63,35 @@ public function create($requestedType, array $arguments = []) return $this->createObject($type, $args); } + + /** + * Parse array argument + * + * @param array $array + * + * @return void + */ + protected function parseArray(&$array) + { + foreach ($array as $key => $item) { + if ($item === (array)$item) { + if (isset($item['_i_'])) { + if ($item['_s_']) { + $array[$key] = $this->objectManager->get($item['_i_']); + } else { + $array[$key] = $this->objectManager->create($item['_i_']); + } + + } elseif (isset($item['_a_'])) { + if (isset($this->globalArguments[$item['_a_']])) { + $array[$key] = $this->globalArguments[$item['_a_']]; + } else { + $array[$key] = $item['_d_']; + } + } else { + $this->parseArray($array[$key]); + } + } + } + } } From 664d257d443b837703d5405c2385cbe9ebecead2 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 13:35:40 +0200 Subject: [PATCH 04/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from framework --- .../Framework/App/ObjectManagerFactory.php | 8 ++++---- .../Definition/Compiled/Binary.php | 5 +++++ .../Definition/Compiled/Serialized.php | 5 +++++ .../ObjectManager/DefinitionFactory.php | 17 ++++++----------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index d00b67e94cf22..f6b1139be92ea 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -10,6 +10,7 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Code\Generator\FileResolver; use Magento\Framework\Filesystem\DriverPool; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; use Magento\Framework\ObjectManager\Environment; use Magento\Framework\ObjectManager\EnvironmentFactory; use Magento\Framework\ObjectManager\EnvironmentInterface; @@ -88,12 +89,11 @@ public function __construct(DirectoryList $directoryList, DriverPool $driverPool * Create ObjectManager * * @param array $arguments - * @param bool $useCompiled * @return \Magento\Framework\ObjectManagerInterface * * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function create(array $arguments, $useCompiled = true) + public function create(array $arguments) { $deploymentConfig = $this->createDeploymentConfig($this->directoryList, $arguments); @@ -101,10 +101,10 @@ public function create(array $arguments, $useCompiled = true) $this->driverPool->getDriver(DriverPool::FILE), $this->directoryList->getPath(DirectoryList::DI), $this->directoryList->getPath(DirectoryList::GENERATION), - $deploymentConfig->get('definition/format', 'serialized') + $deploymentConfig->get('definition/format', Serialized::MODE_NAME) ); - $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions'), $useCompiled); + $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions')); $relations = $definitionFactory->createRelations(); /** @var \Magento\Framework\ObjectManager\EnvironmentFactory $enFactory */ diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php index cb4ab796f4bf6..9d87746615b0e 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php @@ -9,6 +9,11 @@ class Binary extends \Magento\Framework\ObjectManager\Definition\Compiled { + /** + * Mode name + */ + const MODE_NAME = 'igbinary'; + /** * Unpack signature * diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php index 5c6e62ebdf6c1..ffe5c39b9d716 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php @@ -9,6 +9,11 @@ class Serialized extends \Magento\Framework\ObjectManager\Definition\Compiled { + /** + * Mode name + */ + const MODE_NAME = 'serialized'; + /** * Unpack signature * diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index d735a93154406..b2aa552b499e5 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -19,6 +19,8 @@ use Magento\Framework\Interception\Code\Generator as InterceptionGenerator; use Magento\Framework\ObjectManager\Code\Generator; use Magento\Framework\ObjectManager\Code\Generator\Converter as ConverterGenerator; +use Magento\Framework\ObjectManager\Definition\Compiled\Binary; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; use Magento\Framework\ObjectManager\Definition\Runtime; use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; @@ -61,8 +63,8 @@ class DefinitionFactory * @var array */ protected $_definitionClasses = [ - 'igbinary' => 'Magento\Framework\ObjectManager\Definition\Compiled\Binary', - 'serialized' => 'Magento\Framework\ObjectManager\Definition\Compiled\Serialized', + Binary::MODE_NAME => '\Magento\Framework\ObjectManager\Definition\Compiled\Binary', + Serialized::MODE_NAME => '\Magento\Framework\ObjectManager\Definition\Compiled\Serialized', ]; /** @@ -83,17 +85,10 @@ public function __construct(DriverInterface $filesystemDriver, $definitionDir, $ * Create class definitions * * @param mixed $definitions - * @param bool $useCompiled * @return Runtime */ - public function createClassDefinition($definitions, $useCompiled = true) + public function createClassDefinition($definitions = false) { - if (!$definitions && $useCompiled) { - $path = $this->_definitionDir . '/definitions.php'; - if ($this->_filesystemDriver->isReadable($path)) { - $definitions = $this->_filesystemDriver->fileGetContents($path); - } - } if ($definitions) { if (is_string($definitions)) { $definitions = $this->_unpack($definitions); @@ -172,7 +167,7 @@ public function createRelations() */ protected function _unpack($definitions) { - $extractor = $this->_definitionFormat == 'igbinary' ? 'igbinary_unserialize' : 'unserialize'; + $extractor = $this->_definitionFormat == Binary::MODE_NAME ? 'igbinary_unserialize' : 'unserialize'; return $extractor($definitions); } } From 03ec65c4c119a562e5b08148bcf7ba7e6f18917e Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 13:45:17 +0200 Subject: [PATCH 05/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from compilation tool --- .../Tools/Di/App/Task/Operation/Area.php | 13 +- .../App/Task/Operation/InterceptionCache.php | 33 +++-- .../Tools/Di/Code/Reader/ClassesScanner.php | 39 ++---- .../Di/Code/Reader/InstancesNamesList.php | 21 ++++ .../Code/Reader/InstancesNamesList/Area.php | 52 ++++++++ .../Reader/InstancesNamesList/Directory.php | 113 ++++++++++++++++++ .../InstancesNamesList/Interceptions.php | 86 +++++++++++++ .../Di/Code/Scanner/DirectoryScanner.php | 7 +- .../Magento/Tools/Di/Compiler/Directory.php | 113 ------------------ .../Di/Definition/Serializer/Igbinary.php | 5 + .../Di/Definition/Serializer/Standard.php | 5 + dev/tools/Magento/Tools/Di/compiler.php | 42 +++---- 12 files changed, 333 insertions(+), 196 deletions(-) create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php delete mode 100644 dev/tools/Magento/Tools/Di/Compiler/Directory.php diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php index b57279ea62cca..4a8e7a8562542 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php @@ -8,6 +8,7 @@ use Magento\Tools\Di\App\Task\OperationInterface; use Magento\Framework\App; use Magento\Tools\Di\Code\Reader\ClassesScanner; +use Magento\Tools\Di\Code\Reader\InstancesNamesList; use Magento\Tools\Di\Compiler\Config; use Magento\Tools\Di\Definition\Collection as DefinitionsCollection; @@ -19,9 +20,9 @@ class Area implements OperationInterface private $areaList; /** - * @var ClassesScanner + * @var InstancesNamesList */ - private $classesScanner; + private $areaInstancesNamesList; /** * @var Config\Reader @@ -40,20 +41,20 @@ class Area implements OperationInterface /** * @param App\AreaList $areaList - * @param ClassesScanner $classesScanner + * @param InstancesNamesList $instancesNamesList * @param Config\Reader $configReader * @param Config\WriterInterface $configWriter * @param array $data */ public function __construct( App\AreaList $areaList, - ClassesScanner $classesScanner, + InstancesNamesList\Area $areaInstancesNamesList, Config\Reader $configReader, Config\WriterInterface $configWriter, $data = [] ) { $this->areaList = $areaList; - $this->classesScanner = $classesScanner; + $this->areaInstancesNamesList = $areaInstancesNamesList; $this->configReader = $configReader; $this->configWriter = $configWriter; $this->data = $data; @@ -91,7 +92,7 @@ public function doOperation() protected function getDefinitionsCollection($path) { $definitions = new DefinitionsCollection(); - foreach ($this->classesScanner->getList($path) as $className => $constructorArguments) { + foreach ($this->areaInstancesNamesList->getList($path) as $className => $constructorArguments) { $definitions->addDefinition($className, $constructorArguments); } return $definitions; diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php index 68230f3b3c97e..f012f1a23701d 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php @@ -6,6 +6,8 @@ namespace Magento\Tools\Di\App\Task\Operation; use Magento\Tools\Di\App\Task\OperationInterface; +use Magento\Tools\Di\Code\Reader\ClassesScanner; +use Magento\Tools\Di\Code\Reader\InstancesNamesList; class InterceptionCache implements OperationInterface { @@ -19,16 +21,24 @@ class InterceptionCache implements OperationInterface */ private $configInterface; + private $interceptionsInstancesNamesList; + /** * @param \Magento\Framework\Interception\Config\Config $configInterface - * @param array $data + * @param ClassesScanner $classesScanner + * @param InstancesNamesList\Interceptions $interceptionsInstancesNamesList + * @param array $data */ public function __construct( \Magento\Framework\Interception\Config\Config $configInterface, - $data = [] + ClassesScanner $classesScanner, + InstancesNamesList\Interceptions $interceptionsInstancesNamesList, + array $data = [] ) { $this->data = $data; $this->configInterface = $configInterface; + $this->classesScanner = $classesScanner; + $this->interceptionsInstancesNamesList = $interceptionsInstancesNamesList; } /** @@ -42,24 +52,13 @@ public function doOperation() return; } - $logWriter = new \Magento\Tools\Di\Compiler\Log\Writer\Quiet(); - $errorWriter = new \Magento\Tools\Di\Compiler\Log\Writer\Console(); - - $log = new \Magento\Tools\Di\Compiler\Log\Log($logWriter, $errorWriter); - - $validator = new \Magento\Framework\Code\Validator(); - $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); - - $directoryCompiler = new \Magento\Tools\Di\Compiler\Directory($log, $validator); + $definitions = []; foreach ($this->data as $path) { if (is_readable($path)) { - $directoryCompiler->compile($path); - } + array_merge($definitions, $this->interceptionsInstancesNamesList->getList($path)); + } } - list($definitions, ) = $directoryCompiler->getResult(); - - $this->configInterface->initialize(array_keys($definitions)); + $this->configInterface->initialize($definitions); } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php index 97063a236a2f9..fca364412d562 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php @@ -13,24 +13,12 @@ class ClassesScanner { /** - * @var ClassReaderDecorator - */ - private $classReaderDecorator; - - /** - * @param ClassReaderDecorator $classReaderDecorator - */ - public function __construct(ClassReaderDecorator $classReaderDecorator) - { - $this->classReaderDecorator = $classReaderDecorator; - } - - /** - * Retrieves list of classes and arguments for given path - * [CLASS NAME => ConstructorArgument[]] + * Retrieves list of classes for given path * * @param string $path + * * @return array + * * @throws FilesystemException */ public function getList($path) @@ -39,14 +27,16 @@ public function getList($path) if (!(bool)$realPath) { throw new FilesystemException(); } - $classes = []; + $recursiveIterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS), \RecursiveIteratorIterator::SELF_FIRST ); - /** @var $fileItem \SplFileInfo */ + + $classes = []; foreach ($recursiveIterator as $fileItem) { - if (!$this->isPhpFile($fileItem)) { + /** @var $fileItem \SplFileInfo */ + if ($fileItem->getExtension() !== 'php') { continue; } $fileScanner = new FileScanner($fileItem->getRealPath()); @@ -55,20 +45,9 @@ public function getList($path) if (!class_exists($className)) { require_once $fileItem->getRealPath(); } - $classes[$className] = $this->classReaderDecorator->getConstructor($className); + $classes[] = $className; } } return $classes; } - - /** - * Whether file is .php file - * - * @param \SplFileInfo $item - * @return bool - */ - private function isPhpFile(\SplFileInfo $item) - { - return $item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php'; - } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php new file mode 100644 index 0000000000000..9698bb15cd476 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php @@ -0,0 +1,21 @@ +classReaderDecorator = $classReaderDecorator; + $this->classesScanner = $classesScanner; + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + $classes = []; + foreach ($this->classesScanner->getList($path) as $className) { + $classes[$className] = $this->classReaderDecorator->getConstructor($className); + } + return $classes; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php new file mode 100644 index 0000000000000..105be3a270045 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php @@ -0,0 +1,113 @@ +classReader = new ClassReader(); + $this->classesScanner = new \Magento\Tools\Di\Code\Reader\ClassesScanner(); + + $this->log = $log; + $this->generationDir = $generationDir; + + $this->validator = new \Magento\Framework\Code\Validator(); + $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); + $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + + set_error_handler([$this, 'errorHandler'], E_STRICT); + } + + /** + * ErrorHandler for logging + * + * @param int $errorNumber + * @param string $msg + * + * @return void + */ + public function errorHandler($errorNumber, $msg) + { + $this->log->add(Log::COMPILATION_ERROR, $this->current, '#'. $errorNumber .' '. $msg); + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + foreach ($this->classesScanner->getList($path) as $className) { + $this->current = $className; // for errorHandler function + try { + if ($path != $this->generationDir) { // validate all classes except classes in generation dir + $this->validator->validate($className); + } + $this->relations[$className] = $this->classReader->getParents($className); + } catch (\Magento\Framework\Code\ValidationException $exception) { + $this->log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); + } catch (\ReflectionException $e) { + $this->log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); + } + } + + return $this->relations; + } + + /** + * @return array + */ + public function getRelations() + { + return $this->relations; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php new file mode 100644 index 0000000000000..0e87d3e3dd939 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php @@ -0,0 +1,86 @@ +classReader = $classReader; + $this->classesScanner = $classesScanner; + + $this->log = new \Magento\Tools\Di\Compiler\Log\Log( + new \Magento\Tools\Di\Compiler\Log\Writer\Quiet(), + new \Magento\Tools\Di\Compiler\Log\Writer\Console() + ); + + $this->validator = new \Magento\Framework\Code\Validator(); + $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); + $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + $nameList = []; + foreach ($this->classesScanner->getList($path) as $className) { + try { + if (!strpos($path, 'generation')) { // validate all classes except classes in var/generation dir + $this->validator->validate($className); + } + $nameList[] = $className; + } catch (\Magento\Framework\Code\ValidationException $exception) { + $this->log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); + } catch (\ReflectionException $e) { + $this->log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); + } + } + + $this->log->report(); + + return $nameList; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php b/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php index e544b2785151c..193114179d2ec 100644 --- a/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php +++ b/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php @@ -16,9 +16,12 @@ class DirectoryScanner */ public function scan($dir, array $patterns = []) { + $recursiveIterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS) + ); $output = []; - /** @var $file \DirectoryIterator */ - foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)) as $file) { + foreach ($recursiveIterator as $file) { + /** @var $file \SplFileInfo */ if ($file->isDir()) { continue; } diff --git a/dev/tools/Magento/Tools/Di/Compiler/Directory.php b/dev/tools/Magento/Tools/Di/Compiler/Directory.php deleted file mode 100644 index cd3e3923b970a..0000000000000 --- a/dev/tools/Magento/Tools/Di/Compiler/Directory.php +++ /dev/null @@ -1,113 +0,0 @@ -_log = $log; - $this->_validator = $validator; - set_error_handler([$this, 'errorHandler'], E_STRICT); - } - - /** - * @param int $errno - * @param string $errstr - * @return void - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function errorHandler($errno, $errstr) - { - $this->_log->add(Log::COMPILATION_ERROR, $this->_current, $errstr); - } - - /** - * Compile class definitions - * - * @param string $path - * @param bool $validate - * @return void - */ - public function compile($path, $validate = true) - { - $rdi = new \RecursiveDirectoryIterator(realpath($path)); - $recursiveIterator = new \RecursiveIteratorIterator($rdi, 1); - /** @var $item \SplFileInfo */ - foreach ($recursiveIterator as $item) { - if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { - $fileScanner = new FileScanner($item->getRealPath()); - $classNames = $fileScanner->getClassNames(); - foreach ($classNames as $className) { - $this->_current = $className; - if (!class_exists($className)) { - require_once $item->getRealPath(); - } - try { - if ($validate) { - $this->_validator->validate($className); - } - $signatureReader = new \Magento\Framework\Code\Reader\ClassReader(); - $this->_definitions[$className] = $signatureReader->getConstructor($className); - $this->_relations[$className] = $signatureReader->getParents($className); - } catch (\Magento\Framework\Code\ValidationException $exception) { - $this->_log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); - } catch (\ReflectionException $e) { - $this->_log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); - } - $this->_processedClasses[$className] = 1; - } - } - } - } - - /** - * Retrieve compilation result - * - * @return array - */ - public function getResult() - { - return [$this->_definitions, $this->_relations]; - } -} diff --git a/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php b/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php index 4ccafca9dbcd1..bfcf90322aecb 100644 --- a/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php +++ b/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php @@ -7,6 +7,11 @@ class Igbinary implements SerializerInterface { + /** + * Serializer name + */ + const NAME = 'igbinary'; + /** * Igbinary constructor */ diff --git a/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php b/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php index e938a78876917..4862fce3e367e 100644 --- a/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php +++ b/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php @@ -7,6 +7,11 @@ class Standard implements SerializerInterface { + /** + * Serializer name + */ + const NAME = 'standard'; + /** * Serialize input data * diff --git a/dev/tools/Magento/Tools/Di/compiler.php b/dev/tools/Magento/Tools/Di/compiler.php index 7bc5ed4baf456..16e2c8730364f 100644 --- a/dev/tools/Magento/Tools/Di/compiler.php +++ b/dev/tools/Magento/Tools/Di/compiler.php @@ -18,14 +18,12 @@ use Magento\Framework\ObjectManager\Code\Generator\Repository; use Magento\Framework\ObjectManager\Code\Generator\Persistor; use Magento\Tools\Di\Code\Scanner; -use Magento\Tools\Di\Compiler\Directory; use Magento\Tools\Di\Compiler\Log\Log; use Magento\Tools\Di\Compiler\Log\Writer; use Magento\Tools\Di\Definition\Compressor; -use Magento\Tools\Di\Definition\Serializer; +use Magento\Tools\Di\Definition\Serializer\Igbinary; +use Magento\Tools\Di\Definition\Serializer\Standard; -$filePatterns = ['php' => '/.*\.php$/', 'di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']; -$codeScanDir = realpath($rootDir . '/app'); try { $opt = new Zend_Console_Getopt( [ @@ -40,7 +38,6 @@ $generationDir = $opt->getOption('generation') ? $opt->getOption('generation') : $rootDir . '/var/generation'; $diDir = $opt->getOption('di') ? $opt->getOption('di') : $rootDir . '/var/di'; - $compiledFile = $diDir . '/definitions.php'; $relationsFile = $diDir . '/relations.ser'; $pluginDefFile = $diDir . '/plugins.ser'; @@ -52,21 +49,16 @@ /** @var Writer\WriterInterface $logWriter Writer model for success messages */ $logWriter = $opt->getOption('v') ? new Writer\Console() : new Writer\Quiet(); + $log = new Log($logWriter, new Writer\Console()); - /** @var Writer\WriterInterface $logWriter Writer model for error messages */ - $errorWriter = new Writer\Console(); - - $log = new Log($logWriter, $errorWriter); - $serializer = $opt->getOption('serializer') == 'igbinary' ? new Serializer\Igbinary() : new Serializer\Standard(); - - $validator = new \Magento\Framework\Code\Validator(); - $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + $serializer = $opt->getOption('serializer') == Igbinary::NAME ? new Igbinary() : new Standard(); AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); // 1 Code generation // 1.1 Code scan + $filePatterns = ['php' => '/.*\.php$/', 'di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']; + $codeScanDir = realpath($rootDir . '/app'); $directoryScanner = new Scanner\DirectoryScanner(); $files = $directoryScanner->scan($codeScanDir, $filePatterns); $files['additional'] = [$opt->getOption('extra-classes-file')]; @@ -102,15 +94,13 @@ Persistor::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Persistor', Repository::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Repository', Converter::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Converter', - SearchResults::ENTITY_TYPE => 'Magento\Framework\Api\Code\Generator\SearchResults', + SearchResults::ENTITY_TYPE => 'Magento\Framework\Api\Code\Generator\SearchResults' ] ); $generatorAutoloader = new \Magento\Framework\Code\Generator\Autoloader($generator); spl_autoload_register([$generatorAutoloader, 'load']); - - foreach ($repositories as $entityName) { switch ($generator->generateClass($entityName)) { case \Magento\Framework\Code\Generator::GENERATION_SUCCESS: @@ -150,10 +140,10 @@ // 2. Compilation // 2.1 Code scan - $directoryCompiler = new Directory($log, $validator); + $directoryInstancesNamesList = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Directory($log, $generationDir); foreach ($compilationDirs as $path) { if (is_readable($path)) { - $directoryCompiler->compile($path); + $directoryInstancesNamesList->getList($path); } } @@ -183,20 +173,16 @@ } } - //2.1.2 Compile definitions for Proxy/Interceptor classes - $directoryCompiler->compile($generationDir, false); + //2.1.2 Compile relations for Proxy/Interceptor classes + $directoryInstancesNamesList->getList($generationDir); - list($definitions, $relations) = $directoryCompiler->getResult(); + $relations = $directoryInstancesNamesList->getRelations(); // 2.2 Compression - $compressor = new Compressor($serializer); - $output = $compressor->compress($definitions); - if (!file_exists(dirname($compiledFile))) { - mkdir(dirname($compiledFile), 0777, true); + if (!file_exists(dirname($relationsFile))) { + mkdir(dirname($relationsFile), 0777, true); } $relations = array_filter($relations); - - file_put_contents($compiledFile, $output); file_put_contents($relationsFile, $serializer->serialize($relations)); // 3. Plugin Definition Compilation From c318611d4d36c2310d28e9bd0e205f8d4985ae39 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 14:20:12 +0200 Subject: [PATCH 06/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - implemented general algorithm for using both for object arguments and configurable arrays --- .../ObjectManager/Factory/Compiled.php | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 5d46650934042..ea81c9c161e12 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -22,33 +22,28 @@ public function create($requestedType, array $arguments = []) $type = $this->config->getInstanceType($requestedType); $requestedType = ltrim($requestedType, '\\'); $args = $this->config->getArguments($requestedType); - if ($args == null) { + if ($args === null) { return new $type(); } foreach ($args as $key => &$argument) { if (isset($arguments[$key])) { $argument = $arguments[$key]; - } else { + } elseif (isset($argument['_i_'])) { + if ($argument['_s_']) { + $argument = $this->objectManager->get($argument['_i_']); + } else { + $argument = $this->objectManager->create($argument['_i_']); + } + } elseif (isset($argument['_v_'])) { + $argument = $argument['_v_']; if ($argument === (array)$argument) { - if (isset($argument['_v_'])) { - $argument = $argument['_v_']; - if ($argument === (array)$argument) { - $this->parseArray($argument); - } - } elseif (isset($argument['_i_'])) { - if ($argument['_s_']) { - $argument = $this->objectManager->get($argument['_i_']); - } else { - $argument = $this->objectManager->create($argument['_i_']); - } - } elseif (isset($argument['_a_'])) { - if (isset($this->globalArguments[$argument['_a_']])) { - $argument = $this->globalArguments[$argument['_a_']]; - } else { - $argument = $argument['_d_']; - } - } + $this->parseArray($argument); + } + } elseif (isset($argument['_a_'])) { + $argument = $argument['_d_']; + if (isset($this->globalArguments[$argument['_a_']])) { + $argument = $this->globalArguments[$argument['_a_']]; } } } @@ -73,23 +68,23 @@ public function create($requestedType, array $arguments = []) */ protected function parseArray(&$array) { - foreach ($array as $key => $item) { - if ($item === (array)$item) { - if (isset($item['_i_'])) { - if ($item['_s_']) { - $array[$key] = $this->objectManager->get($item['_i_']); + foreach ($array as $key => &$argument) { + if ($argument === (array)$argument) { + if (isset($argument['_i_'])) { + if ($argument['_s_']) { + $argument = $this->objectManager->get($argument['_i_']); } else { - $array[$key] = $this->objectManager->create($item['_i_']); + $argument = $this->objectManager->create($argument['_i_']); } - } elseif (isset($item['_a_'])) { - if (isset($this->globalArguments[$item['_a_']])) { - $array[$key] = $this->globalArguments[$item['_a_']]; + } elseif (isset($argument['_a_'])) { + if (isset($this->globalArguments[$argument['_a_']])) { + $argument = $this->globalArguments[$argument['_a_']]; } else { - $array[$key] = $item['_d_']; + $argument = $argument['_d_']; } } else { - $this->parseArray($array[$key]); + $this->parseArray($argument); } } } From aeae82ee1d43e4d236376b34a3266d7e0b91f794 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 15:02:26 +0200 Subject: [PATCH 07/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - fixed global argument resolving --- .../ObjectManager/Factory/CompiledTest.php | 31 ++++++++++++++++--- .../Fixture/Compiled/SimpleClassTesting.php | 21 ++++++++++++- .../ObjectManager/Factory/Compiled.php | 3 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php index 5167bfa341972..5a8fc1264acb2 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php @@ -74,6 +74,11 @@ public function testCreateSimple() ->method('get') ->with('Dependency\Shared\StdClass') ->willReturn(new \StdClass); + $this->factory->setArguments( + [ + 'globalValue' => 'GLOBAL_ARGUMENT', + ] + ); /** @var \Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting $result */ $result = $this->factory->create($requestedType, []); @@ -86,6 +91,7 @@ public function testCreateSimple() $this->assertInstanceOf('StdClass', $result->getNonSharedDependency()); $this->assertEquals('value', $result->getValue()); $this->assertEquals(['default_value1', 'default_value2'], $result->getValueArray()); + $this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue()); } public function testCreateSimpleConfiguredArguments() @@ -111,7 +117,12 @@ public function testCreateSimpleConfiguredArguments() ->method('get') ->with('Dependency\Shared\StdClass') ->willReturn(new \StdClass); - $this->factory->setArguments(['array_global_existing_argument' => 'GLOBAL_ARGUMENT']); + $this->factory->setArguments( + [ + 'array_global_existing_argument' => 'GLOBAL_ARGUMENT', + 'globalValue' => 'GLOBAL_ARGUMENT', + ] + ); /** @var \Magento\Framework\ObjectManager\Factory\Fixture\Compiled\SimpleClassTesting $result */ $result = $this->factory->create($requestedType, []); @@ -137,6 +148,8 @@ public function testCreateSimpleConfiguredArguments() ], $result->getValueArray() ); + $this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue()); + } /** @@ -147,11 +160,11 @@ public function testCreateSimpleConfiguredArguments() private function getSimpleConfig() { return [ - 'type_dependency' => [ + 'nonSharedDependency' => [ '_i_' => 'Dependency\StdClass', '_s_' => false, ], - 'type_dependency_shared' => [ + 'sharedDependency' => [ '_i_' => 'Dependency\Shared\StdClass', '_s_' => true, ], @@ -161,6 +174,10 @@ private function getSimpleConfig() 'value_array' => [ '_v_' => ['default_value1', 'default_value2'], ], + 'globalValue' => [ + '_a_' => 'globalValue', + '_d_' => null + ] ]; } @@ -172,11 +189,11 @@ private function getSimpleConfig() private function getSimpleNestedConfig() { return [ - 'type_dependency' => [ + 'nonSharedDependency' => [ '_i_' => 'Dependency\StdClass', '_s_' => false, ], - 'type_dependency_shared' => [ + 'sharedDependency' => [ '_i_' => 'Dependency\Shared\StdClass', '_s_' => true, ], @@ -210,6 +227,10 @@ private function getSimpleNestedConfig() '_d_' => 'DEFAULT_VALUE' ] ], + ], + 'globalValue' => [ + '_a_' => 'globalValue', + '_d_' => null ] ]; } diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php index 049034de1a8ac..f5acf85e3c418 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -12,24 +12,33 @@ class SimpleClassTesting * @var \StdClass */ private $nonSharedDependency; + /** * @var \StdClass */ private $sharedDependency; + /** * @var string */ private $value; + /** * @var array */ private $valueArray; + /** + * @var string + */ + private $globalValue; + /** * @param \StdClass $nonSharedDependency * @param \StdClass $sharedDependency * @param string $value * @param array $valueArray + * @param string $globalValue */ public function __construct( \StdClass $nonSharedDependency, @@ -38,13 +47,23 @@ public function __construct( array $valueArray = [ 'default_value1', 'default_value2' - ] + ], + $globalValue = '' ) { $this->nonSharedDependency = $nonSharedDependency; $this->sharedDependency = $sharedDependency; $this->value = $value; $this->valueArray = $valueArray; + $this->globalValue = $globalValue; + } + + /** + * @return string + */ + public function getGlobalValue() + { + return $this->globalValue; } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index ea81c9c161e12..139926cf31780 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -41,9 +41,10 @@ public function create($requestedType, array $arguments = []) $this->parseArray($argument); } } elseif (isset($argument['_a_'])) { - $argument = $argument['_d_']; if (isset($this->globalArguments[$argument['_a_']])) { $argument = $this->globalArguments[$argument['_a_']]; + } else { + $argument = $argument['_d_']; } } } From 21a159fa6134115cdda402d6860d370c1268f4d2 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 15:47:04 +0200 Subject: [PATCH 08/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - removed 'shared' key --- .../ObjectManager/Factory/CompiledTest.php | 12 ++---- .../Di/Compiler/ArgumentsResolverTest.php | 12 ++---- .../Tools/Di/Compiler/ArgumentsResolver.php | 43 +++++++++++++++---- .../ObjectManager/Factory/Compiled.php | 18 +++----- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php index 5a8fc1264acb2..ee6a8201ffe6b 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php @@ -161,12 +161,10 @@ private function getSimpleConfig() { return [ 'nonSharedDependency' => [ - '_i_' => 'Dependency\StdClass', - '_s_' => false, + '_ins_' => 'Dependency\StdClass', ], 'sharedDependency' => [ '_i_' => 'Dependency\Shared\StdClass', - '_s_' => true, ], 'value' => [ '_v_' => 'value', @@ -190,12 +188,10 @@ private function getSimpleNestedConfig() { return [ 'nonSharedDependency' => [ - '_i_' => 'Dependency\StdClass', - '_s_' => false, + '_ins_' => 'Dependency\StdClass', ], 'sharedDependency' => [ '_i_' => 'Dependency\Shared\StdClass', - '_s_' => true, ], 'value' => [ '_v_' => 'value', @@ -205,13 +201,11 @@ private function getSimpleNestedConfig() 'array_value' => 'value', 'array_configured_instance' => [ '_i_' => 'Dependency\Shared\StdClass', - '_s_' => true, ], 'array_configured_array' => [ 'array_array_value' => 'value', 'array_array_configured_instance' => [ - '_i_' => 'Dependency\StdClass', - '_s_' => false, + '_ins_' => 'Dependency\StdClass', ], ], 'array_global_argument' => [ diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index 2a4833aede14b..8c2cfba0f4f6b 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -112,12 +112,10 @@ private function getResolvedSimpleConfigExpectation() { return [ 'type_dependency' => [ - '_i_' => 'Type\Dependency', - '_s_' => false, + '_ins_' => 'Type\Dependency', ], 'type_dependency_shared' => [ '_i_' => 'Type\Dependency\Shared', - '_s_' => true, ], 'value' => [ '_v_' => 'value', @@ -165,12 +163,10 @@ private function getResolvedConfigurableConfigExpectation() { return [ 'type_dependency_configured' => [ - '_i_' => 'Type\Dependency\Configured', - '_s_' => false, + '_ins_' => 'Type\Dependency\Configured', ], 'type_dependency_shared_configured' => [ '_i_' => 'Type\Dependency\Shared\Configured', - '_s_' => true, ], 'global_argument' => [ '_a_' => 'global_argument_configured', @@ -188,13 +184,11 @@ private function getResolvedConfigurableConfigExpectation() 'array_value' => 'value', 'array_configured_instance' => [ '_i_' => 'Type\Dependency\Shared\Configured', - '_s_' => true, ], 'array_configured_array' => [ 'array_array_value' => 'value', 'array_array_configured_instance' => [ - '_i_' => 'Type\Dependency\Shared\Configured', - '_s_' => false, + '_ins_' => 'Type\Dependency\Shared\Configured', ], ], 'array_global_argument' => [ diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index e981fbecf0a34..0d970aa07903a 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -19,9 +19,17 @@ class ArgumentsResolver * * @var array */ - private $instanceArgumentPattern = [ + private $sharedInstanceArgumentPattern = [ '_i_' => null, - '_s_' => false, + ]; + + /** + * Instance argument pattern used for configuration + * + * @var array + */ + private $notSharedInstanceArgumentPattern = [ + '_ins_' => null, ]; /** @@ -96,8 +104,7 @@ public function getResolvedConstructorArguments($instanceType, $constructor) private function getConfiguredArgument($configuredArgument, ConstructorArgument $constructorArgument) { if ($constructorArgument->getType()) { - $argument = $this->getInstanceArgument($configuredArgument['instance']); - $argument['_s_'] = isset($configuredArgument['shared']) ? $configuredArgument['shared'] : $argument['_s_']; + $argument = $this->getConfiguredInstanceArgument($configuredArgument); return $argument; } elseif (isset($configuredArgument['argument'])) { return $this->getGlobalArgument($configuredArgument['argument'], $constructorArgument->getDefaultValue()); @@ -118,8 +125,7 @@ private function getConfiguredArrayAttribute($array) } if (isset($value['instance'])) { - $array[$key] = $this->getInstanceArgument($value['instance']); - $array[$key]['_s_'] = isset($value['shared']) ? $value['shared'] : $array[$key]['_s_']; + $array[$key] = $this->getConfiguredInstanceArgument($value); continue; } @@ -134,6 +140,21 @@ private function getConfiguredArrayAttribute($array) return $array; } + private function getConfiguredInstanceArgument($config) { + $argument = $this->getInstanceArgument($config['instance']); + if (isset($config['shared'])) { + if ($config['shared']) { + $pattern = $this->sharedInstanceArgumentPattern; + $pattern['_i_'] = current($argument); + } else { + $pattern = $this->notSharedInstanceArgumentPattern; + $pattern['_ins_'] = current($argument); + } + $argument = $pattern; + } + return $argument; + } + /** * Return configured arguments * @@ -163,9 +184,13 @@ function ($type) { */ private function getInstanceArgument($instanceType) { - $argument = $this->instanceArgumentPattern; - $argument['_i_'] = $instanceType; - $argument['_s_'] = $this->diContainerConfig->isShared($instanceType); + if ($this->diContainerConfig->isShared($instanceType)) { + $argument = $this->sharedInstanceArgumentPattern; + $argument['_i_'] = $instanceType; + } else { + $argument = $this->notSharedInstanceArgumentPattern; + $argument['_ins_'] = $instanceType; + } return $argument; } diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 139926cf31780..eb8e9a8def28d 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -20,7 +20,6 @@ class Compiled extends AbstractFactory public function create($requestedType, array $arguments = []) { $type = $this->config->getInstanceType($requestedType); - $requestedType = ltrim($requestedType, '\\'); $args = $this->config->getArguments($requestedType); if ($args === null) { return new $type(); @@ -30,11 +29,9 @@ public function create($requestedType, array $arguments = []) if (isset($arguments[$key])) { $argument = $arguments[$key]; } elseif (isset($argument['_i_'])) { - if ($argument['_s_']) { - $argument = $this->objectManager->get($argument['_i_']); - } else { - $argument = $this->objectManager->create($argument['_i_']); - } + $argument = $this->objectManager->get($argument['_i_']); + } elseif (isset($argument['_ins_'])) { + $argument = $this->objectManager->create($argument['_ins_']); } elseif (isset($argument['_v_'])) { $argument = $argument['_v_']; if ($argument === (array)$argument) { @@ -72,12 +69,9 @@ protected function parseArray(&$array) foreach ($array as $key => &$argument) { if ($argument === (array)$argument) { if (isset($argument['_i_'])) { - if ($argument['_s_']) { - $argument = $this->objectManager->get($argument['_i_']); - } else { - $argument = $this->objectManager->create($argument['_i_']); - } - + $argument = $this->objectManager->get($argument['_i_']); + } elseif (isset($argument['_ins_'])) { + $argument = $this->objectManager->create($argument['_ins_']); } elseif (isset($argument['_a_'])) { if (isset($this->globalArguments[$argument['_a_']])) { $argument = $this->globalArguments[$argument['_a_']]; From 75aa885263254a2ccb21419b64255598d45a28c6 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 16:24:36 +0200 Subject: [PATCH 09/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Test/Legacy/_files/obsolete_classes.php | 1 + .../Test/Legacy/_files/obsolete_methods.php | 3 +- .../ObjectManager/DefinitionFactoryTest.php | 28 +++---------------- .../Di/Code/Reader/ClassReaderDecorator.php | 1 - .../Tools/Di/Code/Reader/ClassesScanner.php | 2 -- .../Di/Code/Reader/InstancesNamesList.php | 12 ++++---- .../Code/Reader/InstancesNamesList/Area.php | 12 ++++---- .../Reader/InstancesNamesList/Directory.php | 12 ++++---- .../InstancesNamesList/Interceptions.php | 12 ++++---- 9 files changed, 35 insertions(+), 48 deletions(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php index f12e7d360fea2..132ccead20558 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php @@ -2855,4 +2855,5 @@ ['Magento\Sales\Model\Quote\Address\Total\Nominal\Shipping'], ['Magento\Sales\Model\Quote\Address\Total\Nominal\Subtotal'], ['Magento\Core\Model\Validator\Factory', 'Magento\Framework\Validator\Factory'], + ['Magento\Tools\Di\Compiler\Directory'], ]; diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php index d2109ce14c60e..f9e828a603a5b 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php @@ -2032,5 +2032,6 @@ ['isNominal', 'Magento\Sales\Model\Order\Item'], ['getIsNominal', 'Magento\Sales\Model\Quote\Item\AbbstractItem'], ['checkQuoteAmount', 'Magento\Sales\Helper\Data'], - ['getEntityTypeId', 'Magento\Customer\Model\Customer'] + ['getEntityTypeId', 'Magento\Customer\Model\Customer'], + ['isPhpFile', '\Magento\Tools\Di\Code\Reader\ClassesScanner'], ]; diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php index 5a3d33ab032df..6580e7448d7e3 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php @@ -6,10 +6,12 @@ namespace Magento\Framework\ObjectManager; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; + class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $filesystemDriverMock; @@ -37,30 +39,8 @@ protected function setUp() $this->filesystemDriverMock, 'DefinitionDir', 'GenerationDir', - 'serialized' - ); - } - - public function testCreateDefinitionsReadsCompiledDefinitions() - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable')->will($this->returnValue(false)); - $this->assertInstanceOf( - '\Magento\Framework\ObjectManager\Definition\Runtime', - $this->model->createClassDefinition(null, true) - ); - $autoloadFunctions = spl_autoload_functions(); - spl_autoload_unregister(array_pop($autoloadFunctions)); - } - - public function testCreateDefinitionsDoesNotReadCompiledDefinitionsIfUseCompiledIsFalse() - { - $this->filesystemDriverMock->expects($this->never())->method('isReadable'); - $this->assertInstanceOf( - '\Magento\Framework\ObjectManager\Definition\Runtime', - $this->model->createClassDefinition(null, false) + Serialized::MODE_NAME ); - $autoloadFunctions = spl_autoload_functions(); - spl_autoload_unregister(array_pop($autoloadFunctions)); } public function testCreateClassDefinitionFromString() diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php b/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php index d1c87272ace8f..2451975f2adc3 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php @@ -1,6 +1,5 @@ Date: Fri, 30 Jan 2015 18:12:09 +0200 Subject: [PATCH 10/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../testsuite/Magento/Tools/Di/App/Task/AreaTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php index 867c8c3c2d905..54d76607d066c 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php @@ -19,9 +19,9 @@ class AreaTest extends \PHPUnit_Framework_TestCase private $areaListMock; /** - * @var ClassesScanner | \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area | \PHPUnit_Framework_MockObject_MockObject */ - private $classesScannerMock; + private $areaInstancesNamesList; /** * @var Config\Reader | \PHPUnit_Framework_MockObject_MockObject @@ -38,7 +38,7 @@ protected function setUp() $this->areaListMock = $this->getMockBuilder('Magento\Framework\App\AreaList') ->disableOriginalConstructor() ->getMock(); - $this->classesScannerMock = $this->getMockBuilder('Magento\Tools\Di\Code\Reader\ClassesScanner') + $this->areaInstancesNamesList = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\InstancesNamesList\Area') ->disableOriginalConstructor() ->getMock(); $this->configReaderMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Reader') @@ -53,7 +53,7 @@ public function testDoOperationEmptyPath() { $areaOperation = new Area( $this->areaListMock, - $this->classesScannerMock, + $this->areaInstancesNamesList, $this->configReaderMock, $this->configWriterMock ); @@ -74,7 +74,7 @@ public function testDoOperationGlobalArea() $definitions->addDefinition('class', []); $areaOperation = new Area( $this->areaListMock, - $this->classesScannerMock, + $this->areaInstancesNamesList, $this->configReaderMock, $this->configWriterMock, [$path] @@ -83,7 +83,7 @@ public function testDoOperationGlobalArea() $this->areaListMock->expects($this->once()) ->method('getCodes') ->willReturn([]); - $this->classesScannerMock->expects($this->once()) + $this->areaInstancesNamesList->expects($this->once()) ->method('getList') ->with($path) ->willReturn(['class' => []]); From 6d1e0841d037376213cf118766e57d5cc5399edb Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 13:35:40 +0200 Subject: [PATCH 11/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from framework --- .../Framework/App/ObjectManagerFactory.php | 8 ++++---- .../Definition/Compiled/Binary.php | 5 +++++ .../Definition/Compiled/Serialized.php | 5 +++++ .../ObjectManager/DefinitionFactory.php | 17 ++++++----------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index d00b67e94cf22..f6b1139be92ea 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -10,6 +10,7 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Code\Generator\FileResolver; use Magento\Framework\Filesystem\DriverPool; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; use Magento\Framework\ObjectManager\Environment; use Magento\Framework\ObjectManager\EnvironmentFactory; use Magento\Framework\ObjectManager\EnvironmentInterface; @@ -88,12 +89,11 @@ public function __construct(DirectoryList $directoryList, DriverPool $driverPool * Create ObjectManager * * @param array $arguments - * @param bool $useCompiled * @return \Magento\Framework\ObjectManagerInterface * * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function create(array $arguments, $useCompiled = true) + public function create(array $arguments) { $deploymentConfig = $this->createDeploymentConfig($this->directoryList, $arguments); @@ -101,10 +101,10 @@ public function create(array $arguments, $useCompiled = true) $this->driverPool->getDriver(DriverPool::FILE), $this->directoryList->getPath(DirectoryList::DI), $this->directoryList->getPath(DirectoryList::GENERATION), - $deploymentConfig->get('definition/format', 'serialized') + $deploymentConfig->get('definition/format', Serialized::MODE_NAME) ); - $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions'), $useCompiled); + $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions')); $relations = $definitionFactory->createRelations(); /** @var \Magento\Framework\ObjectManager\EnvironmentFactory $enFactory */ diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php index cb4ab796f4bf6..9d87746615b0e 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php @@ -9,6 +9,11 @@ class Binary extends \Magento\Framework\ObjectManager\Definition\Compiled { + /** + * Mode name + */ + const MODE_NAME = 'igbinary'; + /** * Unpack signature * diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php index 5c6e62ebdf6c1..ffe5c39b9d716 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php @@ -9,6 +9,11 @@ class Serialized extends \Magento\Framework\ObjectManager\Definition\Compiled { + /** + * Mode name + */ + const MODE_NAME = 'serialized'; + /** * Unpack signature * diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index d735a93154406..b2aa552b499e5 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -19,6 +19,8 @@ use Magento\Framework\Interception\Code\Generator as InterceptionGenerator; use Magento\Framework\ObjectManager\Code\Generator; use Magento\Framework\ObjectManager\Code\Generator\Converter as ConverterGenerator; +use Magento\Framework\ObjectManager\Definition\Compiled\Binary; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; use Magento\Framework\ObjectManager\Definition\Runtime; use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; @@ -61,8 +63,8 @@ class DefinitionFactory * @var array */ protected $_definitionClasses = [ - 'igbinary' => 'Magento\Framework\ObjectManager\Definition\Compiled\Binary', - 'serialized' => 'Magento\Framework\ObjectManager\Definition\Compiled\Serialized', + Binary::MODE_NAME => '\Magento\Framework\ObjectManager\Definition\Compiled\Binary', + Serialized::MODE_NAME => '\Magento\Framework\ObjectManager\Definition\Compiled\Serialized', ]; /** @@ -83,17 +85,10 @@ public function __construct(DriverInterface $filesystemDriver, $definitionDir, $ * Create class definitions * * @param mixed $definitions - * @param bool $useCompiled * @return Runtime */ - public function createClassDefinition($definitions, $useCompiled = true) + public function createClassDefinition($definitions = false) { - if (!$definitions && $useCompiled) { - $path = $this->_definitionDir . '/definitions.php'; - if ($this->_filesystemDriver->isReadable($path)) { - $definitions = $this->_filesystemDriver->fileGetContents($path); - } - } if ($definitions) { if (is_string($definitions)) { $definitions = $this->_unpack($definitions); @@ -172,7 +167,7 @@ public function createRelations() */ protected function _unpack($definitions) { - $extractor = $this->_definitionFormat == 'igbinary' ? 'igbinary_unserialize' : 'unserialize'; + $extractor = $this->_definitionFormat == Binary::MODE_NAME ? 'igbinary_unserialize' : 'unserialize'; return $extractor($definitions); } } From fe21f1be58efa6edd6051e816daa126d06c90b51 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 13:45:17 +0200 Subject: [PATCH 12/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from compilation tool --- .../Tools/Di/App/Task/Operation/Area.php | 13 +- .../App/Task/Operation/InterceptionCache.php | 33 +++-- .../Tools/Di/Code/Reader/ClassesScanner.php | 39 ++---- .../Di/Code/Reader/InstancesNamesList.php | 21 ++++ .../Code/Reader/InstancesNamesList/Area.php | 52 ++++++++ .../Reader/InstancesNamesList/Directory.php | 113 ++++++++++++++++++ .../InstancesNamesList/Interceptions.php | 86 +++++++++++++ .../Di/Code/Scanner/DirectoryScanner.php | 7 +- .../Magento/Tools/Di/Compiler/Directory.php | 113 ------------------ .../Di/Definition/Serializer/Igbinary.php | 5 + .../Di/Definition/Serializer/Standard.php | 5 + dev/tools/Magento/Tools/Di/compiler.php | 42 +++---- 12 files changed, 333 insertions(+), 196 deletions(-) create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php create mode 100644 dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php delete mode 100644 dev/tools/Magento/Tools/Di/Compiler/Directory.php diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php index b57279ea62cca..4a8e7a8562542 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php @@ -8,6 +8,7 @@ use Magento\Tools\Di\App\Task\OperationInterface; use Magento\Framework\App; use Magento\Tools\Di\Code\Reader\ClassesScanner; +use Magento\Tools\Di\Code\Reader\InstancesNamesList; use Magento\Tools\Di\Compiler\Config; use Magento\Tools\Di\Definition\Collection as DefinitionsCollection; @@ -19,9 +20,9 @@ class Area implements OperationInterface private $areaList; /** - * @var ClassesScanner + * @var InstancesNamesList */ - private $classesScanner; + private $areaInstancesNamesList; /** * @var Config\Reader @@ -40,20 +41,20 @@ class Area implements OperationInterface /** * @param App\AreaList $areaList - * @param ClassesScanner $classesScanner + * @param InstancesNamesList $instancesNamesList * @param Config\Reader $configReader * @param Config\WriterInterface $configWriter * @param array $data */ public function __construct( App\AreaList $areaList, - ClassesScanner $classesScanner, + InstancesNamesList\Area $areaInstancesNamesList, Config\Reader $configReader, Config\WriterInterface $configWriter, $data = [] ) { $this->areaList = $areaList; - $this->classesScanner = $classesScanner; + $this->areaInstancesNamesList = $areaInstancesNamesList; $this->configReader = $configReader; $this->configWriter = $configWriter; $this->data = $data; @@ -91,7 +92,7 @@ public function doOperation() protected function getDefinitionsCollection($path) { $definitions = new DefinitionsCollection(); - foreach ($this->classesScanner->getList($path) as $className => $constructorArguments) { + foreach ($this->areaInstancesNamesList->getList($path) as $className => $constructorArguments) { $definitions->addDefinition($className, $constructorArguments); } return $definitions; diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php index 68230f3b3c97e..f012f1a23701d 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php @@ -6,6 +6,8 @@ namespace Magento\Tools\Di\App\Task\Operation; use Magento\Tools\Di\App\Task\OperationInterface; +use Magento\Tools\Di\Code\Reader\ClassesScanner; +use Magento\Tools\Di\Code\Reader\InstancesNamesList; class InterceptionCache implements OperationInterface { @@ -19,16 +21,24 @@ class InterceptionCache implements OperationInterface */ private $configInterface; + private $interceptionsInstancesNamesList; + /** * @param \Magento\Framework\Interception\Config\Config $configInterface - * @param array $data + * @param ClassesScanner $classesScanner + * @param InstancesNamesList\Interceptions $interceptionsInstancesNamesList + * @param array $data */ public function __construct( \Magento\Framework\Interception\Config\Config $configInterface, - $data = [] + ClassesScanner $classesScanner, + InstancesNamesList\Interceptions $interceptionsInstancesNamesList, + array $data = [] ) { $this->data = $data; $this->configInterface = $configInterface; + $this->classesScanner = $classesScanner; + $this->interceptionsInstancesNamesList = $interceptionsInstancesNamesList; } /** @@ -42,24 +52,13 @@ public function doOperation() return; } - $logWriter = new \Magento\Tools\Di\Compiler\Log\Writer\Quiet(); - $errorWriter = new \Magento\Tools\Di\Compiler\Log\Writer\Console(); - - $log = new \Magento\Tools\Di\Compiler\Log\Log($logWriter, $errorWriter); - - $validator = new \Magento\Framework\Code\Validator(); - $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); - - $directoryCompiler = new \Magento\Tools\Di\Compiler\Directory($log, $validator); + $definitions = []; foreach ($this->data as $path) { if (is_readable($path)) { - $directoryCompiler->compile($path); - } + array_merge($definitions, $this->interceptionsInstancesNamesList->getList($path)); + } } - list($definitions, ) = $directoryCompiler->getResult(); - - $this->configInterface->initialize(array_keys($definitions)); + $this->configInterface->initialize($definitions); } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php index 97063a236a2f9..fca364412d562 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php @@ -13,24 +13,12 @@ class ClassesScanner { /** - * @var ClassReaderDecorator - */ - private $classReaderDecorator; - - /** - * @param ClassReaderDecorator $classReaderDecorator - */ - public function __construct(ClassReaderDecorator $classReaderDecorator) - { - $this->classReaderDecorator = $classReaderDecorator; - } - - /** - * Retrieves list of classes and arguments for given path - * [CLASS NAME => ConstructorArgument[]] + * Retrieves list of classes for given path * * @param string $path + * * @return array + * * @throws FilesystemException */ public function getList($path) @@ -39,14 +27,16 @@ public function getList($path) if (!(bool)$realPath) { throw new FilesystemException(); } - $classes = []; + $recursiveIterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS), \RecursiveIteratorIterator::SELF_FIRST ); - /** @var $fileItem \SplFileInfo */ + + $classes = []; foreach ($recursiveIterator as $fileItem) { - if (!$this->isPhpFile($fileItem)) { + /** @var $fileItem \SplFileInfo */ + if ($fileItem->getExtension() !== 'php') { continue; } $fileScanner = new FileScanner($fileItem->getRealPath()); @@ -55,20 +45,9 @@ public function getList($path) if (!class_exists($className)) { require_once $fileItem->getRealPath(); } - $classes[$className] = $this->classReaderDecorator->getConstructor($className); + $classes[] = $className; } } return $classes; } - - /** - * Whether file is .php file - * - * @param \SplFileInfo $item - * @return bool - */ - private function isPhpFile(\SplFileInfo $item) - { - return $item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php'; - } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php new file mode 100644 index 0000000000000..9698bb15cd476 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php @@ -0,0 +1,21 @@ +classReaderDecorator = $classReaderDecorator; + $this->classesScanner = $classesScanner; + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + $classes = []; + foreach ($this->classesScanner->getList($path) as $className) { + $classes[$className] = $this->classReaderDecorator->getConstructor($className); + } + return $classes; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php new file mode 100644 index 0000000000000..105be3a270045 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php @@ -0,0 +1,113 @@ +classReader = new ClassReader(); + $this->classesScanner = new \Magento\Tools\Di\Code\Reader\ClassesScanner(); + + $this->log = $log; + $this->generationDir = $generationDir; + + $this->validator = new \Magento\Framework\Code\Validator(); + $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); + $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + + set_error_handler([$this, 'errorHandler'], E_STRICT); + } + + /** + * ErrorHandler for logging + * + * @param int $errorNumber + * @param string $msg + * + * @return void + */ + public function errorHandler($errorNumber, $msg) + { + $this->log->add(Log::COMPILATION_ERROR, $this->current, '#'. $errorNumber .' '. $msg); + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + foreach ($this->classesScanner->getList($path) as $className) { + $this->current = $className; // for errorHandler function + try { + if ($path != $this->generationDir) { // validate all classes except classes in generation dir + $this->validator->validate($className); + } + $this->relations[$className] = $this->classReader->getParents($className); + } catch (\Magento\Framework\Code\ValidationException $exception) { + $this->log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); + } catch (\ReflectionException $e) { + $this->log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); + } + } + + return $this->relations; + } + + /** + * @return array + */ + public function getRelations() + { + return $this->relations; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php new file mode 100644 index 0000000000000..0e87d3e3dd939 --- /dev/null +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php @@ -0,0 +1,86 @@ +classReader = $classReader; + $this->classesScanner = $classesScanner; + + $this->log = new \Magento\Tools\Di\Compiler\Log\Log( + new \Magento\Tools\Di\Compiler\Log\Writer\Quiet(), + new \Magento\Tools\Di\Compiler\Log\Writer\Console() + ); + + $this->validator = new \Magento\Framework\Code\Validator(); + $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); + $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + } + + /** + * Retrieves list of classes for given path + * + * @param $path + * + * @return array + * + * @throws \Magento\Framework\Filesystem\FilesystemException + */ + public function getList($path) + { + $nameList = []; + foreach ($this->classesScanner->getList($path) as $className) { + try { + if (!strpos($path, 'generation')) { // validate all classes except classes in var/generation dir + $this->validator->validate($className); + } + $nameList[] = $className; + } catch (\Magento\Framework\Code\ValidationException $exception) { + $this->log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); + } catch (\ReflectionException $e) { + $this->log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); + } + } + + $this->log->report(); + + return $nameList; + } +} \ No newline at end of file diff --git a/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php b/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php index e544b2785151c..193114179d2ec 100644 --- a/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php +++ b/dev/tools/Magento/Tools/Di/Code/Scanner/DirectoryScanner.php @@ -16,9 +16,12 @@ class DirectoryScanner */ public function scan($dir, array $patterns = []) { + $recursiveIterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS) + ); $output = []; - /** @var $file \DirectoryIterator */ - foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)) as $file) { + foreach ($recursiveIterator as $file) { + /** @var $file \SplFileInfo */ if ($file->isDir()) { continue; } diff --git a/dev/tools/Magento/Tools/Di/Compiler/Directory.php b/dev/tools/Magento/Tools/Di/Compiler/Directory.php deleted file mode 100644 index cd3e3923b970a..0000000000000 --- a/dev/tools/Magento/Tools/Di/Compiler/Directory.php +++ /dev/null @@ -1,113 +0,0 @@ -_log = $log; - $this->_validator = $validator; - set_error_handler([$this, 'errorHandler'], E_STRICT); - } - - /** - * @param int $errno - * @param string $errstr - * @return void - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function errorHandler($errno, $errstr) - { - $this->_log->add(Log::COMPILATION_ERROR, $this->_current, $errstr); - } - - /** - * Compile class definitions - * - * @param string $path - * @param bool $validate - * @return void - */ - public function compile($path, $validate = true) - { - $rdi = new \RecursiveDirectoryIterator(realpath($path)); - $recursiveIterator = new \RecursiveIteratorIterator($rdi, 1); - /** @var $item \SplFileInfo */ - foreach ($recursiveIterator as $item) { - if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { - $fileScanner = new FileScanner($item->getRealPath()); - $classNames = $fileScanner->getClassNames(); - foreach ($classNames as $className) { - $this->_current = $className; - if (!class_exists($className)) { - require_once $item->getRealPath(); - } - try { - if ($validate) { - $this->_validator->validate($className); - } - $signatureReader = new \Magento\Framework\Code\Reader\ClassReader(); - $this->_definitions[$className] = $signatureReader->getConstructor($className); - $this->_relations[$className] = $signatureReader->getParents($className); - } catch (\Magento\Framework\Code\ValidationException $exception) { - $this->_log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage()); - } catch (\ReflectionException $e) { - $this->_log->add(Log::COMPILATION_ERROR, $className, $e->getMessage()); - } - $this->_processedClasses[$className] = 1; - } - } - } - } - - /** - * Retrieve compilation result - * - * @return array - */ - public function getResult() - { - return [$this->_definitions, $this->_relations]; - } -} diff --git a/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php b/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php index 4ccafca9dbcd1..bfcf90322aecb 100644 --- a/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php +++ b/dev/tools/Magento/Tools/Di/Definition/Serializer/Igbinary.php @@ -7,6 +7,11 @@ class Igbinary implements SerializerInterface { + /** + * Serializer name + */ + const NAME = 'igbinary'; + /** * Igbinary constructor */ diff --git a/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php b/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php index e938a78876917..4862fce3e367e 100644 --- a/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php +++ b/dev/tools/Magento/Tools/Di/Definition/Serializer/Standard.php @@ -7,6 +7,11 @@ class Standard implements SerializerInterface { + /** + * Serializer name + */ + const NAME = 'standard'; + /** * Serialize input data * diff --git a/dev/tools/Magento/Tools/Di/compiler.php b/dev/tools/Magento/Tools/Di/compiler.php index 7bc5ed4baf456..16e2c8730364f 100644 --- a/dev/tools/Magento/Tools/Di/compiler.php +++ b/dev/tools/Magento/Tools/Di/compiler.php @@ -18,14 +18,12 @@ use Magento\Framework\ObjectManager\Code\Generator\Repository; use Magento\Framework\ObjectManager\Code\Generator\Persistor; use Magento\Tools\Di\Code\Scanner; -use Magento\Tools\Di\Compiler\Directory; use Magento\Tools\Di\Compiler\Log\Log; use Magento\Tools\Di\Compiler\Log\Writer; use Magento\Tools\Di\Definition\Compressor; -use Magento\Tools\Di\Definition\Serializer; +use Magento\Tools\Di\Definition\Serializer\Igbinary; +use Magento\Tools\Di\Definition\Serializer\Standard; -$filePatterns = ['php' => '/.*\.php$/', 'di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']; -$codeScanDir = realpath($rootDir . '/app'); try { $opt = new Zend_Console_Getopt( [ @@ -40,7 +38,6 @@ $generationDir = $opt->getOption('generation') ? $opt->getOption('generation') : $rootDir . '/var/generation'; $diDir = $opt->getOption('di') ? $opt->getOption('di') : $rootDir . '/var/di'; - $compiledFile = $diDir . '/definitions.php'; $relationsFile = $diDir . '/relations.ser'; $pluginDefFile = $diDir . '/plugins.ser'; @@ -52,21 +49,16 @@ /** @var Writer\WriterInterface $logWriter Writer model for success messages */ $logWriter = $opt->getOption('v') ? new Writer\Console() : new Writer\Quiet(); + $log = new Log($logWriter, new Writer\Console()); - /** @var Writer\WriterInterface $logWriter Writer model for error messages */ - $errorWriter = new Writer\Console(); - - $log = new Log($logWriter, $errorWriter); - $serializer = $opt->getOption('serializer') == 'igbinary' ? new Serializer\Igbinary() : new Serializer\Standard(); - - $validator = new \Magento\Framework\Code\Validator(); - $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + $serializer = $opt->getOption('serializer') == Igbinary::NAME ? new Igbinary() : new Standard(); AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); // 1 Code generation // 1.1 Code scan + $filePatterns = ['php' => '/.*\.php$/', 'di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']; + $codeScanDir = realpath($rootDir . '/app'); $directoryScanner = new Scanner\DirectoryScanner(); $files = $directoryScanner->scan($codeScanDir, $filePatterns); $files['additional'] = [$opt->getOption('extra-classes-file')]; @@ -102,15 +94,13 @@ Persistor::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Persistor', Repository::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Repository', Converter::ENTITY_TYPE => 'Magento\Framework\ObjectManager\Code\Generator\Converter', - SearchResults::ENTITY_TYPE => 'Magento\Framework\Api\Code\Generator\SearchResults', + SearchResults::ENTITY_TYPE => 'Magento\Framework\Api\Code\Generator\SearchResults' ] ); $generatorAutoloader = new \Magento\Framework\Code\Generator\Autoloader($generator); spl_autoload_register([$generatorAutoloader, 'load']); - - foreach ($repositories as $entityName) { switch ($generator->generateClass($entityName)) { case \Magento\Framework\Code\Generator::GENERATION_SUCCESS: @@ -150,10 +140,10 @@ // 2. Compilation // 2.1 Code scan - $directoryCompiler = new Directory($log, $validator); + $directoryInstancesNamesList = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Directory($log, $generationDir); foreach ($compilationDirs as $path) { if (is_readable($path)) { - $directoryCompiler->compile($path); + $directoryInstancesNamesList->getList($path); } } @@ -183,20 +173,16 @@ } } - //2.1.2 Compile definitions for Proxy/Interceptor classes - $directoryCompiler->compile($generationDir, false); + //2.1.2 Compile relations for Proxy/Interceptor classes + $directoryInstancesNamesList->getList($generationDir); - list($definitions, $relations) = $directoryCompiler->getResult(); + $relations = $directoryInstancesNamesList->getRelations(); // 2.2 Compression - $compressor = new Compressor($serializer); - $output = $compressor->compress($definitions); - if (!file_exists(dirname($compiledFile))) { - mkdir(dirname($compiledFile), 0777, true); + if (!file_exists(dirname($relationsFile))) { + mkdir(dirname($relationsFile), 0777, true); } $relations = array_filter($relations); - - file_put_contents($compiledFile, $output); file_put_contents($relationsFile, $serializer->serialize($relations)); // 3. Plugin Definition Compilation From c4fec67e8d997c2075fedf6cbf3cdbf9d05bd43e Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 16:24:36 +0200 Subject: [PATCH 13/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Test/Legacy/_files/obsolete_classes.php | 1 + .../Test/Legacy/_files/obsolete_methods.php | 1 + .../ObjectManager/DefinitionFactoryTest.php | 28 +++---------------- .../Di/Code/Reader/ClassReaderDecorator.php | 1 - .../Tools/Di/Code/Reader/ClassesScanner.php | 2 -- .../Di/Code/Reader/InstancesNamesList.php | 12 ++++---- .../Code/Reader/InstancesNamesList/Area.php | 12 ++++---- .../Reader/InstancesNamesList/Directory.php | 12 ++++---- .../InstancesNamesList/Interceptions.php | 12 ++++---- 9 files changed, 34 insertions(+), 47 deletions(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php index f99505f07a1eb..64cf040b5f65d 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php @@ -2902,6 +2902,7 @@ ['Magento\Sales\Model\Quote\Address\Total\Nominal\Shipping'], ['Magento\Sales\Model\Quote\Address\Total\Nominal\Subtotal'], ['Magento\Core\Model\Validator\Factory', 'Magento\Framework\Validator\Factory'], + ['Magento\Tools\Di\Compiler\Directory'], [ 'Magento\Core\Model\TemplateEngine\Plugin\DebugHints', 'Magento\Developer\Model\TemplateEngine\Plugin\DebugHints' diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php index e65261334af42..ecfbd220abb5c 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php @@ -2042,4 +2042,5 @@ ['getAdvancedResultCollection', 'Magento\CatalogSearch\Model\Resource\EngineInterface'], ['getEntityTypeId', 'Magento\Customer\Model\Customer'], ['setIsConfigurable', 'Magento\Catalog\Api\Data\ProductAttributeDataBuilder'], + ['isPhpFile', '\Magento\Tools\Di\Code\Reader\ClassesScanner'], ]; diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php index 5a3d33ab032df..6580e7448d7e3 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/DefinitionFactoryTest.php @@ -6,10 +6,12 @@ namespace Magento\Framework\ObjectManager; +use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; + class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $filesystemDriverMock; @@ -37,30 +39,8 @@ protected function setUp() $this->filesystemDriverMock, 'DefinitionDir', 'GenerationDir', - 'serialized' - ); - } - - public function testCreateDefinitionsReadsCompiledDefinitions() - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable')->will($this->returnValue(false)); - $this->assertInstanceOf( - '\Magento\Framework\ObjectManager\Definition\Runtime', - $this->model->createClassDefinition(null, true) - ); - $autoloadFunctions = spl_autoload_functions(); - spl_autoload_unregister(array_pop($autoloadFunctions)); - } - - public function testCreateDefinitionsDoesNotReadCompiledDefinitionsIfUseCompiledIsFalse() - { - $this->filesystemDriverMock->expects($this->never())->method('isReadable'); - $this->assertInstanceOf( - '\Magento\Framework\ObjectManager\Definition\Runtime', - $this->model->createClassDefinition(null, false) + Serialized::MODE_NAME ); - $autoloadFunctions = spl_autoload_functions(); - spl_autoload_unregister(array_pop($autoloadFunctions)); } public function testCreateClassDefinitionFromString() diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php b/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php index d1c87272ace8f..2451975f2adc3 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/ClassReaderDecorator.php @@ -1,6 +1,5 @@ Date: Fri, 30 Jan 2015 18:12:09 +0200 Subject: [PATCH 14/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../testsuite/Magento/Tools/Di/App/Task/AreaTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php index 867c8c3c2d905..54d76607d066c 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/App/Task/AreaTest.php @@ -19,9 +19,9 @@ class AreaTest extends \PHPUnit_Framework_TestCase private $areaListMock; /** - * @var ClassesScanner | \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area | \PHPUnit_Framework_MockObject_MockObject */ - private $classesScannerMock; + private $areaInstancesNamesList; /** * @var Config\Reader | \PHPUnit_Framework_MockObject_MockObject @@ -38,7 +38,7 @@ protected function setUp() $this->areaListMock = $this->getMockBuilder('Magento\Framework\App\AreaList') ->disableOriginalConstructor() ->getMock(); - $this->classesScannerMock = $this->getMockBuilder('Magento\Tools\Di\Code\Reader\ClassesScanner') + $this->areaInstancesNamesList = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\InstancesNamesList\Area') ->disableOriginalConstructor() ->getMock(); $this->configReaderMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Config\Reader') @@ -53,7 +53,7 @@ public function testDoOperationEmptyPath() { $areaOperation = new Area( $this->areaListMock, - $this->classesScannerMock, + $this->areaInstancesNamesList, $this->configReaderMock, $this->configWriterMock ); @@ -74,7 +74,7 @@ public function testDoOperationGlobalArea() $definitions->addDefinition('class', []); $areaOperation = new Area( $this->areaListMock, - $this->classesScannerMock, + $this->areaInstancesNamesList, $this->configReaderMock, $this->configWriterMock, [$path] @@ -83,7 +83,7 @@ public function testDoOperationGlobalArea() $this->areaListMock->expects($this->once()) ->method('getCodes') ->willReturn([]); - $this->classesScannerMock->expects($this->once()) + $this->areaInstancesNamesList->expects($this->once()) ->method('getList') ->with($path) ->willReturn(['class' => []]); From 7dfbe4a0f07fdc779d6ae15b58efaeba5b165f3c Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 18:42:19 +0200 Subject: [PATCH 15/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - added value null type --- app/bootstrap.php | 2 +- .../ObjectManager/Factory/CompiledTest.php | 9 ++++++- .../Fixture/Compiled/SimpleClassTesting.php | 18 ++++++++++++- .../Di/Compiler/ArgumentsResolverTest.php | 15 ++++++++--- dev/tools/Magento/Tools/Di/App/Compiler.php | 10 +++---- .../Tools/Di/Compiler/ArgumentsResolver.php | 26 +++++++++++++++++-- .../ObjectManager/Factory/Compiled.php | 2 ++ 7 files changed, 68 insertions(+), 14 deletions(-) diff --git a/app/bootstrap.php b/app/bootstrap.php index 183edeceabf33..740ef5fbe9278 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -8,7 +8,7 @@ * Environment initialization */ error_reporting(E_ALL); -#ini_set('display_errors', 1); +ini_set('display_errors', 1); umask(0); /* PHP version validation */ diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php index ee6a8201ffe6b..37dd7fbb309b9 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php @@ -92,6 +92,7 @@ public function testCreateSimple() $this->assertEquals('value', $result->getValue()); $this->assertEquals(['default_value1', 'default_value2'], $result->getValueArray()); $this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue()); + $this->assertNull($result->getNullValue()); } public function testCreateSimpleConfiguredArguments() @@ -149,7 +150,7 @@ public function testCreateSimpleConfiguredArguments() $result->getValueArray() ); $this->assertEquals('GLOBAL_ARGUMENT', $result->getGlobalValue()); - + $this->assertNull($result->getNullValue()); } /** @@ -175,6 +176,9 @@ private function getSimpleConfig() 'globalValue' => [ '_a_' => 'globalValue', '_d_' => null + ], + 'nullValue' => [ + '_vn_' => true ] ]; } @@ -225,6 +229,9 @@ private function getSimpleNestedConfig() 'globalValue' => [ '_a_' => 'globalValue', '_d_' => null + ], + 'nullValue' => [ + '_vn_' => true ] ]; } diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php index f5acf85e3c418..a328092268fd4 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -32,6 +32,11 @@ class SimpleClassTesting * @var string */ private $globalValue; + + /** + * @var + */ + private $nullValue; /** * @param \StdClass $nonSharedDependency @@ -39,6 +44,7 @@ class SimpleClassTesting * @param string $value * @param array $valueArray * @param string $globalValue + * @param null $nullValue */ public function __construct( \StdClass $nonSharedDependency, @@ -48,7 +54,8 @@ public function __construct( 'default_value1', 'default_value2' ], - $globalValue = '' + $globalValue = '', + $nullValue = null ) { $this->nonSharedDependency = $nonSharedDependency; @@ -56,6 +63,15 @@ public function __construct( $this->value = $value; $this->valueArray = $valueArray; $this->globalValue = $globalValue; + $this->nullValue = $nullValue; + } + + /** + * @return mixed + */ + public function getNullValue() + { + return $this->nullValue; } /** diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index 8c2cfba0f4f6b..73e7645c307f2 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -4,8 +4,6 @@ * See COPYING.txt for license details. */ -// @codingStandardsIgnoreFile - namespace Magento\Tools\Di\Compiler; class ArgumentsResolverTest extends \PHPUnit_Framework_TestCase @@ -41,6 +39,7 @@ public function testGetResolvedArgumentsConstructorFormat() new ConstructorArgument(['type_dependency_shared', 'Type\Dependency\Shared', true, null]), new ConstructorArgument(['value', null, false, 'value']), new ConstructorArgument(['value_array', null, false, ['default_value1', 'default_value2']]), + new ConstructorArgument(['value_null', null, false, null]), ]; $this->diContainerConfig->expects($this->any()) ->method('isShared') @@ -74,6 +73,7 @@ public function testGetResolvedArgumentsConstructorConfiguredFormat() new ConstructorArgument(['global_argument_def', null, false, []]), new ConstructorArgument(['value_configured', null, false, 'value']), new ConstructorArgument(['value_array_configured', null, false, []]), + new ConstructorArgument(['value_null', null, false, null]), ]; @@ -123,6 +123,9 @@ private function getResolvedSimpleConfigExpectation() 'value_array' => [ '_v_' => ['default_value1', 'default_value2'], ], + 'value_null' => [ + '_vn_' => true, + ], ]; } @@ -150,7 +153,8 @@ private function getConfiguredArguments() ] ], 'array_global_argument' => ['argument' => 'global_argument_configured'] - ] + ], + 'value_null' => null, ]; } @@ -196,7 +200,10 @@ private function getResolvedConfigurableConfigExpectation() '_d_' => null ] ], - ] + ], + 'value_null' => [ + '_vn_' => true, + ], ]; } } diff --git a/dev/tools/Magento/Tools/Di/App/Compiler.php b/dev/tools/Magento/Tools/Di/App/Compiler.php index 25c910eba92ac..f57b1ae9cbb77 100644 --- a/dev/tools/Magento/Tools/Di/App/Compiler.php +++ b/dev/tools/Magento/Tools/Di/App/Compiler.php @@ -68,11 +68,11 @@ public function launch() Task\OperationFactory::AREA => [ BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' ], - //Task\OperationFactory::INTERCEPTION => - // BP . '/var/generation', - //Task\OperationFactory::INTERCEPTION_CACHE => [ - // BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' - //] + Task\OperationFactory::INTERCEPTION => + BP . '/var/generation', + Task\OperationFactory::INTERCEPTION_CACHE => [ + BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation' + ] ]; $responseCode = Response::SUCCESS; diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index 0d970aa07903a..72273ad223eff 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -15,7 +15,7 @@ class ArgumentsResolver private $diContainerConfig; /** - * Instance argument pattern used for configuration + * Shared instance argument pattern used for configuration * * @var array */ @@ -41,6 +41,15 @@ class ArgumentsResolver '_v_' => null, ]; + /** + * Value argument pattern used for configuration + * + * @var array + */ + private $nullValueArgumentPattern = [ + '_vn_' => true, + ]; + /** * Configured argument pattern used for configuration * @@ -114,6 +123,8 @@ private function getConfiguredArgument($configuredArgument, ConstructorArgument } /** + * Returns configured array attribute + * * @param array $array * @return mixed */ @@ -140,7 +151,14 @@ private function getConfiguredArrayAttribute($array) return $array; } - private function getConfiguredInstanceArgument($config) { + /** + * Returns configured instance argument + * + * @param $config + * @return array|mixed + */ + private function getConfiguredInstanceArgument($config) + { $argument = $this->getInstanceArgument($config['instance']); if (isset($config['shared'])) { if ($config['shared']) { @@ -202,6 +220,10 @@ private function getInstanceArgument($instanceType) */ private function getNonObjectArgument($value) { + if (is_null($value)) { + return $this->nullValueArgumentPattern; + } + $argument = $this->valueArgumentPattern; if (is_array($value)) { $value = $this->getConfiguredArrayAttribute($value); diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index eb8e9a8def28d..9fd88520ab1ce 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -37,6 +37,8 @@ public function create($requestedType, array $arguments = []) if ($argument === (array)$argument) { $this->parseArray($argument); } + } elseif (isset($argument['_vn_'])) { + $argument = null; } elseif (isset($argument['_a_'])) { if (isset($this->globalArguments[$argument['_a_']])) { $argument = $this->globalArguments[$argument['_a_']]; From e981e715b8a227df5fe38931d88d0149b3036bb1 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Fri, 30 Jan 2015 18:51:25 +0200 Subject: [PATCH 16/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - fixed Compiled Config to match new pattern --- .../Magento/Framework/ObjectManager/Config/Compiled.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index a748488206643..181a6b146c697 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -78,7 +78,7 @@ public function getArguments($type) } return $this->arguments[$type]; } else { - return ['Magento\Framework\ObjectManagerInterface']; + return [['_i_' => 'Magento\Framework\ObjectManagerInterface']]; } } From a1608cbbd85a2e9701439139a2111f5aa081886c Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 30 Jan 2015 19:08:27 +0200 Subject: [PATCH 17/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Magento/Tools/Di/App/Task/Operation/Area.php | 6 +++--- .../Di/App/Task/Operation/InterceptionCache.php | 7 +++++-- .../Di/Code/Reader/InstancesNamesList/Area.php | 6 +++--- .../Code/Reader/InstancesNamesList/Directory.php | 14 +++++++------- .../Reader/InstancesNamesList/Interceptions.php | 6 +++--- ...mesList.php => InstancesNamesListInterface.php} | 7 ++++--- dev/tools/Magento/Tools/Di/compiler.php | 2 +- 7 files changed, 26 insertions(+), 22 deletions(-) rename dev/tools/Magento/Tools/Di/Code/Reader/{InstancesNamesList.php => InstancesNamesListInterface.php} (79%) diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php index 4a8e7a8562542..46a0892cc8064 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php @@ -8,7 +8,7 @@ use Magento\Tools\Di\App\Task\OperationInterface; use Magento\Framework\App; use Magento\Tools\Di\Code\Reader\ClassesScanner; -use Magento\Tools\Di\Code\Reader\InstancesNamesList; +use Magento\Tools\Di\Code\Reader\InstancesNamesListInterface; use Magento\Tools\Di\Compiler\Config; use Magento\Tools\Di\Definition\Collection as DefinitionsCollection; @@ -20,7 +20,7 @@ class Area implements OperationInterface private $areaList; /** - * @var InstancesNamesList + * @var InstancesNamesListInterface */ private $areaInstancesNamesList; @@ -41,7 +41,7 @@ class Area implements OperationInterface /** * @param App\AreaList $areaList - * @param InstancesNamesList $instancesNamesList + * @param InstancesNamesListInterface $instancesNamesList * @param Config\Reader $configReader * @param Config\WriterInterface $configWriter * @param array $data diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php index f012f1a23701d..8c6adf686b848 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php @@ -7,7 +7,7 @@ use Magento\Tools\Di\App\Task\OperationInterface; use Magento\Tools\Di\Code\Reader\ClassesScanner; -use Magento\Tools\Di\Code\Reader\InstancesNamesList; +use Magento\Tools\Di\Code\Reader\InstancesNamesListInterface; class InterceptionCache implements OperationInterface { @@ -21,6 +21,9 @@ class InterceptionCache implements OperationInterface */ private $configInterface; + /** + * @var InstancesNamesList\Interceptions + */ private $interceptionsInstancesNamesList; /** @@ -56,7 +59,7 @@ public function doOperation() foreach ($this->data as $path) { if (is_readable($path)) { array_merge($definitions, $this->interceptionsInstancesNamesList->getList($path)); - } + } } $this->configInterface->initialize($definitions); diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php index 124c547b39e6a..bf43d1cc9edcd 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php @@ -10,7 +10,7 @@ * * @package Magento\Tools\Di\Code\Reader\InstancesNamesList */ -class Area implements \Magento\Tools\Di\Code\Reader\InstancesNamesList +class Area implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInterface { /** * @var \Magento\Tools\Di\Code\Reader\ClassReaderDecorator @@ -37,7 +37,7 @@ public function __construct( /** * Retrieves list of classes for given path * - * @param $path + * @param string $path path to dir with files * * @return array * @@ -51,4 +51,4 @@ public function getList($path) } return $classes; } -} \ No newline at end of file +} diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php index 8934c352e2095..310452b0283f2 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php @@ -13,7 +13,7 @@ * * @package Magento\Tools\Di\Code\Reader\InstancesNamesList */ -class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesList +class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInterface { /** * @var string @@ -46,11 +46,11 @@ class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesList private $classesScanner; /** - * @param Log $log - * @param $generationDir + * @param Log $log Logging object + * @param string $generationDir directory where generated files is */ - public function __construct(Log $log, $generationDir) { - + public function __construct(Log $log, $generationDir) + { $this->classReader = new ClassReader(); $this->classesScanner = new \Magento\Tools\Di\Code\Reader\ClassesScanner(); @@ -80,7 +80,7 @@ public function errorHandler($errorNumber, $msg) /** * Retrieves list of classes for given path * - * @param $path + * @param string $path path to dir with files * * @return array * @@ -112,4 +112,4 @@ public function getRelations() { return $this->relations; } -} \ No newline at end of file +} diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php index 9f369297eb7a2..51f67a5407b65 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php @@ -13,7 +13,7 @@ * * @package Magento\Tools\Di\Code\Reader\InstancesNamesList */ -class Interceptions implements \Magento\Tools\Di\Code\Reader\InstancesNamesList +class Interceptions implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInterface { /** * @var \Magento\Tools\Di\Code\Reader\ClassReaderDecorator @@ -59,7 +59,7 @@ public function __construct( /** * Retrieves list of classes for given path * - * @param $path + * @param string $path path to dir with files * * @return array * @@ -85,4 +85,4 @@ public function getList($path) return $nameList; } -} \ No newline at end of file +} diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesListInterface.php similarity index 79% rename from dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php rename to dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesListInterface.php index 4ea7a73ab6ddd..719a914f3556b 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesListInterface.php @@ -10,14 +10,15 @@ * * @package Magento\Tools\Di\Code\Reader */ -interface InstancesNamesList { +interface InstancesNamesListInterface +{ /** * Retrieves list of classes for given path * - * @param $path + * @param string $path path to dir with files * * @return array */ public function getList($path); -} \ No newline at end of file +} diff --git a/dev/tools/Magento/Tools/Di/compiler.php b/dev/tools/Magento/Tools/Di/compiler.php index 16e2c8730364f..8ea98c787042e 100644 --- a/dev/tools/Magento/Tools/Di/compiler.php +++ b/dev/tools/Magento/Tools/Di/compiler.php @@ -49,7 +49,7 @@ /** @var Writer\WriterInterface $logWriter Writer model for success messages */ $logWriter = $opt->getOption('v') ? new Writer\Console() : new Writer\Quiet(); - $log = new Log($logWriter, new Writer\Console()); + $log = new Log($logWriter, new Writer\Console()); $serializer = $opt->getOption('serializer') == Igbinary::NAME ? new Igbinary() : new Standard(); From 6229f0f68e7385845adae835aaa46ca2407dd78e Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Mon, 2 Feb 2015 12:07:04 +0200 Subject: [PATCH 18/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - removed trimming of starting backslash in compiled factory - changed plugin list to store instance names without starting backslash - changed interception config to store instance names without starting backslash --- .../Interception/Config/ConfigTest.php | 6 ++++++ .../Custom/Module/Model/StartingBackslash.php | 18 ++++++++++++++++++ .../Module/Model/StartingBackslash/Plugin.php | 18 ++++++++++++++++++ .../Interception/PluginList/PluginListTest.php | 11 +++++++++++ .../Interception/_files/reader_mock_map.php | 8 ++++++++ .../Framework/Interception/Config/Config.php | 5 +++-- .../Interception/PluginList/PluginList.php | 16 +++++++++++++++- 7 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/Interception/Custom/Module/Model/StartingBackslash.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/Interception/Custom/Module/Model/StartingBackslash/Plugin.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/Interception/Config/ConfigTest.php b/dev/tests/unit/testsuite/Magento/Framework/Interception/Config/ConfigTest.php index 6f47e039d8784..c4166640715ca 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/Interception/Config/ConfigTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/Interception/Config/ConfigTest.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/../Custom/Module/Model/ItemContainerPlugin/Simple.php'; require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Simple.php'; require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Advanced.php'; + class ConfigTest extends \PHPUnit_Framework_TestCase { /** @@ -98,6 +99,10 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type) 'Magento\Framework\Interception\Custom\Module\Model\ItemProxy', 'Magento\Framework\Interception\Custom\Module\Model\ItemProxy', ], + [ + 'Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy', + 'Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy' + ], [ 'virtual_custom_item', 'Magento\Framework\Interception\Custom\Module\Model\Item', @@ -107,6 +112,7 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type) $this->definitionMock->expects($this->any())->method('getClasses')->will($this->returnValue( [ 'Magento\Framework\Interception\Custom\Module\Model\ItemProxy', + '\Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy', ] )); $model = new \Magento\Framework\Interception\Config\Config( diff --git a/dev/tests/unit/testsuite/Magento/Framework/Interception/Custom/Module/Model/StartingBackslash.php b/dev/tests/unit/testsuite/Magento/Framework/Interception/Custom/Module/Model/StartingBackslash.php new file mode 100644 index 0000000000000..5a102c10fb2c6 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/Interception/Custom/Module/Model/StartingBackslash.php @@ -0,0 +1,18 @@ +_configScopeMock->expects($this->any())->method('getCurrentScope')->will($this->returnValue('backend')); $this->_model->getNext('Magento\Framework\Interception\Custom\Module\Model\Item', 'getName'); $this->_model->getNext('Magento\Framework\Interception\Custom\Module\Model\ItemContainer', 'getName'); + $this->_model->getNext('Magento\Framework\Interception\Custom\Module\Model\StartingBackslash', 'getName'); $this->assertEquals( 'Magento\Framework\Interception\Custom\Module\Model\ItemPlugin\Simple', @@ -91,6 +95,13 @@ public function testGetPlugin() 'simple_plugin' ) ); + $this->assertEquals( + 'Magento\Framework\Interception\Custom\Module\Model\StartingBackslash\Plugin', + $this->_model->getPlugin( + 'Magento\Framework\Interception\Custom\Module\Model\StartingBackslash', + 'simple_plugin' + ) + ); } /** diff --git a/dev/tests/unit/testsuite/Magento/Framework/Interception/_files/reader_mock_map.php b/dev/tests/unit/testsuite/Magento/Framework/Interception/_files/reader_mock_map.php index 6b5658356d0c0..48184c6e676ce 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/Interception/_files/reader_mock_map.php +++ b/dev/tests/unit/testsuite/Magento/Framework/Interception/_files/reader_mock_map.php @@ -35,6 +35,14 @@ 'instance' => 'Magento\Framework\Interception\Custom\Module\Model\ItemContainerPlugin\Simple', ], ], + ], + 'Magento\Framework\Interception\Custom\Module\Model\StartingBackslash' => [ + 'plugins' => [ + 'simple_plugin' => [ + 'sortOrder' => 20, + 'instance' => '\Magento\Framework\Interception\Custom\Module\Model\StartingBackslash\Plugin', + ], + ], ] ] ], diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index 24accda4ca3f5..edda3288ca18f 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -124,9 +124,9 @@ public function initialize($classDefinitions = []) } } foreach ($config as $typeName => $typeConfig) { - $this->hasPlugins(ltrim($typeName, '\\')); + $this->hasPlugins($typeName); } - foreach ($classDefinitions as $class) { + foreach ($classDefinitions as $key => $class) { $this->hasPlugins($class); } $this->_cache->save(serialize($this->_intercepted), $this->_cacheId); @@ -140,6 +140,7 @@ public function initialize($classDefinitions = []) */ protected function _inheritInterception($type) { + $type = ltrim($type, '\\'); if (!isset($this->_intercepted[$type])) { $realType = $this->_omConfig->getOriginalInstanceType($type); if ($type !== $realType) { diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index 0b95374f8eb90..7e330fa632b6f 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -121,6 +121,7 @@ public function __construct( */ protected function _inheritPlugins($type) { + $type = ltrim($type, '\\'); if (!array_key_exists($type, $this->_inherited)) { $realType = $this->_omConfig->getOriginalInstanceType($type); @@ -150,6 +151,7 @@ protected function _inheritPlugins($type) $this->_inherited[$type] = null; if (is_array($plugins) && count($plugins)) { uasort($plugins, [$this, '_sort']); + $this->trimInstanceStartingBackslash($plugins); $this->_inherited[$type] = $plugins; $lastPerMethod = []; foreach ($plugins as $key => $plugin) { @@ -183,6 +185,18 @@ protected function _inheritPlugins($type) return $this->_inherited[$type]; } + /** + * Trims starting backslash from plugin instance name + * + * @param array $plugins + */ + private function trimInstanceStartingBackslash(&$plugins) + { + foreach ($plugins as &$plugin) { + $plugin['instance'] = ltrim($plugin['instance'], '\\'); + } + } + /** * Sort items * @@ -283,7 +297,7 @@ protected function _loadScopedData() } } foreach ($virtualTypes as $class) { - $this->_inheritPlugins(ltrim($class, '\\')); + $this->_inheritPlugins($class); } foreach ($this->getClassDefinitions() as $class) { $this->_inheritPlugins($class); From 1dc725f74d9ffa1c8837d527157150cec06923a0 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Mon, 2 Feb 2015 13:30:25 +0200 Subject: [PATCH 19/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - added backslash ltrim operation for ObjectManager --- .../Magento/Framework/ObjectManager/ObjectManagerTest.php | 8 +++++++- .../Magento/Framework/ObjectManager/ObjectManager.php | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php index 97325cfdb2ec3..751c92a35b854 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php @@ -14,6 +14,7 @@ require __DIR__ . '/../../_files/Aggregate/AggregateParent.php'; require __DIR__ . '/../../_files/Aggregate/Child.php'; require __DIR__ . '/../../_files/Aggregate/WithOptional.php'; + class ObjectManagerTest extends \PHPUnit_Framework_TestCase { /** @@ -395,6 +396,11 @@ public function testConfiguredArgumentsOverrideInheritedArguments() public function testGetIgnoresFirstSlash() { - $this->assertSame($this->_object->get('Magento\Test\Di\Child'), $this->_object->get('Magento\Test\Di\Child')); + $this->assertSame($this->_object->get('Magento\Test\Di\Child'), $this->_object->get('\Magento\Test\Di\Child')); + } + + public function testCreateIgnoresFirstSlash() + { + $this->assertInstanceOf('Magento\Test\Di\Child', $this->_object->create('\Magento\Test\Di\Child')); } } diff --git a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php index 57e2db9ea1e88..f7a7ca7d8fdd1 100644 --- a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php +++ b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php @@ -53,6 +53,7 @@ public function __construct(FactoryInterface $factory, ConfigInterface $config, */ public function create($type, array $arguments = []) { + $type = ltrim($type, '\\'); return $this->_factory->create($this->_config->getPreference($type), $arguments); } @@ -64,6 +65,7 @@ public function create($type, array $arguments = []) */ public function get($type) { + $type = ltrim($type, '\\'); $type = $this->_config->getPreference($type); if (!isset($this->_sharedInstances[$type])) { $this->_sharedInstances[$type] = $this->_factory->create($type); From 8dc63d378ede9c1d952b53efa80a7f45888e6068 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Mon, 2 Feb 2015 14:40:22 +0200 Subject: [PATCH 20/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - removed ltrim from ObjectManager - changed client code to ltrim string before sending it to OM --- .../ObjectManager/ObjectManagerTest.php | 7 +----- .../Service/Entity/WebapiBuilderFactory.php | 14 +++-------- .../Framework/ObjectManager/ObjectManager.php | 2 -- .../Serialization/DataBuilderFactory.php | 25 +++++++++++++++++++ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php index 751c92a35b854..4a516ab5710a3 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php @@ -396,11 +396,6 @@ public function testConfiguredArgumentsOverrideInheritedArguments() public function testGetIgnoresFirstSlash() { - $this->assertSame($this->_object->get('Magento\Test\Di\Child'), $this->_object->get('\Magento\Test\Di\Child')); - } - - public function testCreateIgnoresFirstSlash() - { - $this->assertInstanceOf('Magento\Test\Di\Child', $this->_object->create('\Magento\Test\Di\Child')); + $this->assertSame($this->_object->get('Magento\Test\Di\Child'), $this->_object->get('Magento\Test\Di\Child')); } } diff --git a/dev/tests/unit/testsuite/Magento/Webapi/Service/Entity/WebapiBuilderFactory.php b/dev/tests/unit/testsuite/Magento/Webapi/Service/Entity/WebapiBuilderFactory.php index 3260472813127..492ba020f384e 100644 --- a/dev/tests/unit/testsuite/Magento/Webapi/Service/Entity/WebapiBuilderFactory.php +++ b/dev/tests/unit/testsuite/Magento/Webapi/Service/Entity/WebapiBuilderFactory.php @@ -16,21 +16,13 @@ public function __construct(\Magento\TestFramework\Helper\ObjectManager $objectM } /** - * Returns a builder for a given class name. + * Creates builder object * - * @param string $className + * @param $builderClassName * @return \Magento\Framework\Api\BuilderInterface Builder Instance */ - public function getDataBuilder($className) + protected function createObject($builderClassName) { - $interfaceSuffix = 'Interface'; - if (substr($className, -strlen($interfaceSuffix)) === $interfaceSuffix) { - /** If class name ends with Interface, replace it with Data suffix */ - $builderClassName = substr($className, 0, -strlen($interfaceSuffix)) . 'Data'; - } else { - $builderClassName = $className; - } - $builderClassName .= 'Builder'; return $this->objectManager->getObject($builderClassName); } } diff --git a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php index f7a7ca7d8fdd1..57e2db9ea1e88 100644 --- a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php +++ b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php @@ -53,7 +53,6 @@ public function __construct(FactoryInterface $factory, ConfigInterface $config, */ public function create($type, array $arguments = []) { - $type = ltrim($type, '\\'); return $this->_factory->create($this->_config->getPreference($type), $arguments); } @@ -65,7 +64,6 @@ public function create($type, array $arguments = []) */ public function get($type) { - $type = ltrim($type, '\\'); $type = $this->_config->getPreference($type); if (!isset($this->_sharedInstances[$type])) { $this->_sharedInstances[$type] = $this->_factory->create($type); diff --git a/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php b/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php index c58a4e7da4421..57478bb8ea8c7 100644 --- a/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php +++ b/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php @@ -33,6 +33,18 @@ public function __construct(ObjectManagerInterface $objectManager) * @return \Magento\Framework\Api\BuilderInterface Builder Instance */ public function getDataBuilder($className) + { + $builderClassName = $this->getBuilderClassName($className); + return $this->createObject($builderClassName); + } + + /** + * Returns builder class name + * + * @param $className + * @return string + */ + protected function getBuilderClassName($className) { $interfaceSuffix = 'Interface'; if (substr($className, -strlen($interfaceSuffix)) === $interfaceSuffix) { @@ -42,6 +54,19 @@ public function getDataBuilder($className) $builderClassName = $className; } $builderClassName .= 'Builder'; + + $builderClassName = ltrim($builderClassName, '\\'); + return $builderClassName; + } + + /** + * Creates builder object + * + * @param $builderClassName + * @return \Magento\Framework\Api\BuilderInterface Builder Instance + */ + protected function createObject($builderClassName) + { return $this->objectManager->create($builderClassName); } } From 617d107e0347bfc330310a4e545671767551f0b7 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Mon, 2 Feb 2015 17:08:30 +0200 Subject: [PATCH 21/57] MAGETWO-30801: Asynchronous products addition to cart --- .../Catalog/Block/Product/ListProduct.php | 10 +-- .../Catalog/view/frontend/requirejs-config.js | 3 +- .../frontend/templates/product/list.phtml | 22 ++++-- .../templates/product/view/addtocart.phtml | 10 ++- .../frontend/web/js/catalog-add-to-cart.js | 69 +++++++++++++++++++ app/code/Magento/Checkout/Controller/Cart.php | 46 ++++++++----- .../Magento/Checkout/Controller/Cart/Add.php | 53 ++++++++++++-- .../Catalog/Block/Product/ListProductTest.php | 14 ++-- lib/web/mage/gallery.less | 8 --- 9 files changed, 183 insertions(+), 52 deletions(-) create mode 100644 app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js diff --git a/app/code/Magento/Catalog/Block/Product/ListProduct.php b/app/code/Magento/Catalog/Block/Product/ListProduct.php index bc007d7e3f479..3aa28a859720d 100644 --- a/app/code/Magento/Catalog/Block/Product/ListProduct.php +++ b/app/code/Magento/Catalog/Block/Product/ListProduct.php @@ -329,11 +329,13 @@ public function getIdentities() public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product) { $url = $this->getAddToCartUrl($product); - $data = [ - 'product' => $product->getEntityId(), - \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->_postDataHelper->getEncodedUrl($url), + return [ + 'action' => $url, + 'data' => [ + 'product' => $product->getEntityId(), + \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->_postDataHelper->getEncodedUrl($url), + ] ]; - return $this->_postDataHelper->getPostData($url, $data); } /** diff --git a/app/code/Magento/Catalog/view/frontend/requirejs-config.js b/app/code/Magento/Catalog/view/frontend/requirejs-config.js index 2ef8d1ad65381..adc8e540f37c5 100644 --- a/app/code/Magento/Catalog/view/frontend/requirejs-config.js +++ b/app/code/Magento/Catalog/view/frontend/requirejs-config.js @@ -19,7 +19,8 @@ var config = { priceBox: 'Magento_Catalog/js/price-box', priceOptionDate: 'Magento_Catalog/js/price-option-date', priceOptions: 'Magento_Catalog/js/price-options', - priceUtils: 'Magento_Catalog/js/price-utils' + priceUtils: 'Magento_Catalog/js/price-utils', + catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart' } } }; \ No newline at end of file diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml index d5a40d57f8c95..0f971f260136c 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml @@ -3,6 +3,7 @@ * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ +use Magento\Framework\App\Action\Action; // @codingStandardsIgnoreFile @@ -70,10 +71,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
>
> isSaleable()): ?> - + getAddToCartPostParams($_product); ?> +
+ + + +
getIsSalable()): ?>
@@ -113,5 +118,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
+ getToolbarHtml(); ?> + getToolbarHtml() ?> diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml index 43c78632c5da9..588a9dba084cd 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml @@ -48,10 +48,16 @@ require([ "jquery", "mage/mage", - "Magento_Catalog/product/view/validation" + "Magento_Catalog/product/view/validation", + "Magento_Catalog/js/catalog-add-to-cart" ], function($){ $('#product_addtocart_form').mage('validation', { - radioCheckboxClosest: '.nested' + radioCheckboxClosest: '.nested', + submitHandler: function(form) { + var widget = $(form).catalogAddToCart({bindSubmit: false}); + widget.catalogAddToCart('submitForm', $(form)); + return false; + } }); }); diff --git a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js new file mode 100644 index 0000000000000..631646b64bac5 --- /dev/null +++ b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js @@ -0,0 +1,69 @@ +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'jquery', + 'jquery/ui' +], function($) { + "use strict"; + + $.widget('mage.catalogAddToCart', { + + options: { + bindSubmit: true, + minicartSelector: '[data-block="minicart"]', + messagesSelector: '.messages', + productStatusSelector: '.stock.available' + }, + + _create: function() { + if (this.options.bindSubmit) { + this._bindSubmit(); + } + }, + + _bindSubmit: function() { + var self = this; + this.element.on('submit', function(e) { + e.preventDefault(); + self.submitForm($(this)); + }); + }, + + submitForm: function(form) { + var self = this; + $.ajax({ + url: form.attr('action'), + data: form.serialize(), + type: 'post', + dataType: 'json', + beforeSend: function() { + $('body').trigger('processStart'); + }, + success: function(res) { + $('body').trigger('processStop'); + if (res.backUrl) { + window.location = res.backUrl; + return; + } + if (res.messages) { + $(self.options.messagesSelector).html(res.messages); + } + if (res.minicart) { + $(self.options.minicartSelector).replaceWith(res.minicart); + } + if (res.product && res.product.statusText) { + $(self.options.productStatusSelector) + .removeClass('available') + .addClass('unavailable') + .find('span') + .html(res.product.statusText); + } + } + }); + } + }); + + return $.mage.catalogAddToCart; +}); \ No newline at end of file diff --git a/app/code/Magento/Checkout/Controller/Cart.php b/app/code/Magento/Checkout/Controller/Cart.php index d3ce20b7be2fd..4a665e357b13a 100644 --- a/app/code/Magento/Checkout/Controller/Cart.php +++ b/app/code/Magento/Checkout/Controller/Cart.php @@ -69,23 +69,8 @@ public function __construct( */ protected function _goBack() { - $returnUrl = $this->getRequest()->getParam('return_url'); - if ($returnUrl && $this->_isInternalUrl($returnUrl)) { - $this->messageManager->getMessages()->clear(); - $this->getResponse()->setRedirect($returnUrl); - } elseif (!$this->_scopeConfig->getValue( - 'checkout/cart/redirect_to_cart', - \Magento\Framework\Store\ScopeInterface::SCOPE_STORE - ) && !$this->getRequest()->getParam( - 'in_cart' - ) && ($backUrl = $this->_redirect->getRefererUrl()) - ) { + if ($backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) { $this->getResponse()->setRedirect($backUrl); - } else { - if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) { - $this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl()); - } - $this->_redirect('checkout/cart'); } return $this; } @@ -111,4 +96,33 @@ protected function _isInternalUrl($url) $secure = strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)) === 0; return $unsecure || $secure; } + + /** + * Get resolved back url + * + * @param null $defaultUrl + * @return mixed|null|string + */ + protected function getBackUrl($defaultUrl = null) + { + $returnUrl = $this->getRequest()->getParam('return_url'); + if ($returnUrl && $this->_isInternalUrl($returnUrl)) { + $this->messageManager->getMessages()->clear(); + return $returnUrl; + } + + $shouldRedirectToCart = $this->_scopeConfig->getValue( + 'checkout/cart/redirect_to_cart', + \Magento\Framework\Store\ScopeInterface::SCOPE_STORE + ); + + if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) { + if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) { + $this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl()); + } + return $this->_url->getUrl('checkout/cart'); + } + + return $defaultUrl; + } } diff --git a/app/code/Magento/Checkout/Controller/Cart/Add.php b/app/code/Magento/Checkout/Controller/Cart/Add.php index d6cafaf38a008..15e00691c0b50 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Add.php +++ b/app/code/Magento/Checkout/Controller/Cart/Add.php @@ -88,7 +88,7 @@ public function execute() * Check product availability */ if (!$product) { - $this->_goBack(); + $this->goBack(); return; } @@ -117,7 +117,7 @@ public function execute() ); $this->messageManager->addSuccess($message); } - $this->_goBack(); + $this->goBack(null, $product); } } catch (\Magento\Framework\Model\Exception $e) { if ($this->_checkoutSession->getUseNotice(true)) { @@ -134,16 +134,55 @@ public function execute() } $url = $this->_checkoutSession->getRedirectUrl(true); - if ($url) { - $this->getResponse()->setRedirect($url); - } else { + if (!$url) { $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); - $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($cartUrl)); + $url = $this->_redirect->getRedirectUrl($cartUrl); } + $this->goBack($url); + } catch (\Exception $e) { $this->messageManager->addException($e, __('We cannot add this item to your shopping cart')); $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e); - $this->_goBack(); + $this->goBack(); + } + } + + /** + * Resolve response + * + * @param string $backUrl + * @param \Magento\Catalog\Model\Product $product + * @return $this|void + */ + protected function goBack($backUrl = null, $product = null) + { + if (!$this->getRequest()->isAjax()) { + return parent::_goBack(); } + + $result = []; + + if ($backUrl || $backUrl = $this->getBackUrl()) { + $result['backUrl'] = $backUrl; + } else { + $this->_view->loadLayout(['default'], true, true, false); + $layout = $this->_view->getLayout(); + + $result['messages'] = $layout->getBlock('global_messages')->toHtml(); + + if ($this->_checkoutSession->getCartWasUpdated()) { + $result['minicart'] = $layout->getBlock('minicart')->toHtml(); + } + + if ($product && true /*!$product->getIsSalable()*/) { + $result['product'] = [ + 'statusText' => __('Out of stock') + ]; + } + } + + $this->getResponse()->representJson( + $this->_objectManager->get('Magento\Core\Helper\Data')->jsonEncode($result) + ); } } diff --git a/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php b/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php index bb44121882a36..9b74a0c67abb9 100644 --- a/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php +++ b/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php @@ -134,12 +134,10 @@ public function testGetAddToCartPostParams() $id = 1; $uenc = strtr(base64_encode($url), '+/=', '-_,'); $data = ['product' => $id, \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $uenc]; - $expectedPostData = json_encode( - [ - 'action' => $url, - 'data' => ['product' => $id, 'uenc' => $uenc], - ] - ); + $expectedPostData = [ + 'action' => $url, + 'data' => ['product' => $id, 'uenc' => $uenc], + ]; $this->typeInstanceMock->expects($this->once()) ->method('hasRequiredOptions') @@ -159,10 +157,6 @@ public function testGetAddToCartPostParams() ->method('getEncodedUrl') ->with($this->equalTo($url)) ->will($this->returnValue($uenc)); - $this->postDataHelperMock->expects($this->once()) - ->method('getPostData') - ->with($this->equalTo($url), $this->equalTo($data)) - ->will($this->returnValue($expectedPostData)); $result = $this->block->getAddToCartPostParams($this->productMock); $this->assertEquals($expectedPostData, $result); } diff --git a/lib/web/mage/gallery.less b/lib/web/mage/gallery.less index 9aee7a0f5ba5e..f3e153aac798e 100644 --- a/lib/web/mage/gallery.less +++ b/lib/web/mage/gallery.less @@ -36,14 +36,6 @@ @import "../css/source/lib/lib.less"; @import "../css/source/theme.less"; -.loading-mask { - .loading(); - z-index: @loader-overlay-z-index; - .loader > p { - display: none; - } -} - .ui-widget-overlay { position: fixed !important; top: 0; From 6dabe7fdd7b252e276c7a3196edb0c7802cf9635 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Mon, 2 Feb 2015 17:17:23 +0200 Subject: [PATCH 22/57] MAGETWO-30801: Asynchronous products addition to cart --- app/code/Magento/Checkout/Controller/Cart/Add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Checkout/Controller/Cart/Add.php b/app/code/Magento/Checkout/Controller/Cart/Add.php index 15e00691c0b50..95a288c76c7f5 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Add.php +++ b/app/code/Magento/Checkout/Controller/Cart/Add.php @@ -174,7 +174,7 @@ protected function goBack($backUrl = null, $product = null) $result['minicart'] = $layout->getBlock('minicart')->toHtml(); } - if ($product && true /*!$product->getIsSalable()*/) { + if ($product && !$product->getIsSalable()) { $result['product'] = [ 'statusText' => __('Out of stock') ]; From acad40063dd50bb03510d26b720e846f9e53778b Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Mon, 2 Feb 2015 17:43:42 +0200 Subject: [PATCH 23/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - added type for configurable array --- .../ObjectManager/Factory/CompiledTest.php | 2 +- .../Di/Compiler/ArgumentsResolverTest.php | 2 +- .../Tools/Di/Compiler/ArgumentsResolver.php | 32 ++++++++++++++++++- .../ObjectManager/Factory/Compiled.php | 6 ++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php index 37dd7fbb309b9..9714034087b5c 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/CompiledTest.php @@ -201,7 +201,7 @@ private function getSimpleNestedConfig() '_v_' => 'value', ], 'value_array' => [ - '_v_' => [ + '_vac_' => [ 'array_value' => 'value', 'array_configured_instance' => [ '_i_' => 'Dependency\Shared\StdClass', diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php index 73e7645c307f2..d83ee54ddc08c 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/ArgumentsResolverTest.php @@ -184,7 +184,7 @@ private function getResolvedConfigurableConfigExpectation() '_v_' => 'value_configured', ], 'value_array_configured' => [ - '_v_' => [ + '_vac_' => [ 'array_value' => 'value', 'array_configured_instance' => [ '_i_' => 'Type\Dependency\Shared\Configured', diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index 72273ad223eff..25aa4f2735886 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -226,12 +226,42 @@ private function getNonObjectArgument($value) $argument = $this->valueArgumentPattern; if (is_array($value)) { - $value = $this->getConfiguredArrayAttribute($value); + if ($this->isConfiguredArray($value)) { + $value = $this->getConfiguredArrayAttribute($value); + $argument = ['_vac_' => $value]; + return $argument; + } } + $argument['_v_'] = $value; return $argument; } + /** + * Whether array is configurable + * + * @param array $value + * @return bool + */ + private function isConfiguredArray($value) + { + foreach ($value as $configuredValue) { + if (!is_array($configuredValue)) { + continue; + } + + if (array_key_exists('instance', $configuredValue) || array_key_exists('argument', $configuredValue)) { + return true; + } + + if ($this->isConfiguredArray($configuredValue)) { + return true; + } + } + + return false; + } + /** * Returns global argument * diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 9fd88520ab1ce..8281b6ebf179a 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -34,9 +34,9 @@ public function create($requestedType, array $arguments = []) $argument = $this->objectManager->create($argument['_ins_']); } elseif (isset($argument['_v_'])) { $argument = $argument['_v_']; - if ($argument === (array)$argument) { - $this->parseArray($argument); - } + } elseif (isset($argument['_vac_'])) { + $argument = $argument['_vac_']; + $this->parseArray($argument); } elseif (isset($argument['_vn_'])) { $argument = null; } elseif (isset($argument['_a_'])) { From 6c6b3214c7b1f1f05839b3fe4bf89988efb4d0c7 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Mon, 2 Feb 2015 18:06:08 +0200 Subject: [PATCH 24/57] MAGETWO-30801: Asynchronous products addition to cart --- app/code/Magento/Checkout/Controller/Cart.php | 4 ++-- app/code/Magento/Checkout/Controller/Cart/Add.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Checkout/Controller/Cart.php b/app/code/Magento/Checkout/Controller/Cart.php index 4a665e357b13a..3497f99691a76 100644 --- a/app/code/Magento/Checkout/Controller/Cart.php +++ b/app/code/Magento/Checkout/Controller/Cart.php @@ -67,9 +67,9 @@ public function __construct( * * @return $this */ - protected function _goBack() + protected function _goBack($backUrl = null) { - if ($backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) { + if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) { $this->getResponse()->setRedirect($backUrl); } return $this; diff --git a/app/code/Magento/Checkout/Controller/Cart/Add.php b/app/code/Magento/Checkout/Controller/Cart/Add.php index 95a288c76c7f5..9c7bd9046d6ff 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Add.php +++ b/app/code/Magento/Checkout/Controller/Cart/Add.php @@ -157,7 +157,7 @@ public function execute() protected function goBack($backUrl = null, $product = null) { if (!$this->getRequest()->isAjax()) { - return parent::_goBack(); + return parent::_goBack($backUrl); } $result = []; From 4dfd4eda717e440a6acd90d718d9070ded53b7b7 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Mon, 2 Feb 2015 19:47:11 +0200 Subject: [PATCH 25/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Magento/Tools/Di/App/CompilerTest.php | 4 +- .../Reader/InstancesNameList/AreaTest.php | 85 ++++++++ .../InstancesNameList/DirectoryTest.php | 202 ++++++++++++++++++ .../InstancesNameList/InterceptionsTest.php | 174 +++++++++++++++ dev/tools/Magento/Tools/Di/App/Compiler.php | 4 +- .../Tools/Di/App/Task/Operation/Area.php | 7 +- .../App/Task/Operation/InterceptionCache.php | 16 +- .../Reader/InstancesNamesList/Directory.php | 33 +-- .../InstancesNamesList/Interceptions.php | 21 +- dev/tools/Magento/Tools/Di/compiler.php | 14 +- 10 files changed, 516 insertions(+), 44 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/App/CompilerTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/App/CompilerTest.php index af3b695cc615d..9bd541f8d1dfa 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/App/CompilerTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/App/CompilerTest.php @@ -103,7 +103,9 @@ private function getPreferences() 'preferences' => [ 'Magento\Tools\Di\Compiler\Config\WriterInterface' => - 'Magento\Tools\Di\Compiler\Config\Writer\Filesystem' + 'Magento\Tools\Di\Compiler\Config\Writer\Filesystem', + 'Magento\Tools\Di\Compiler\Log\Writer\WriterInterface' => + 'Magento\Tools\Di\Compiler\Log\Writer\Console' ] ]; } diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php new file mode 100644 index 0000000000000..26c403eede741 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php @@ -0,0 +1,85 @@ +classesScannerMock = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\ClassesScanner') + ->disableOriginalConstructor() + ->setMethods(['getList']) + ->getMock(); + + $this->classReaderDecoratorMock = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\ClassReaderDecorator') + ->disableOriginalConstructor() + ->setMethods(['getConstructor']) + ->getMock(); + + $this->model = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area( + $this->classesScannerMock, + $this->classReaderDecoratorMock + ); + } + + /** + * + */ + public function testGetList() + { + $path = '/tmp/test'; + + $classes = [ 'NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + + $this->classesScannerMock->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $constructors = [ + ['NameSpace1\ClassName1', ['arg1' => 'NameSpace1\class5', 'arg2' => 'NameSpace1\ClassName4']], + ['NameSpace1\ClassName2', ['arg1' => 'NameSpace1\class5']] + ]; + + $this->classReaderDecoratorMock->expects($this->exactly(count($classes))) + ->method('getConstructor') + ->will($this->returnValueMap( + $constructors + )); + + $result = $this->model->getList($path); + + $expected = [ + $classes[0] => $constructors[0][1], + $classes[1] => $constructors[1][1] + ]; + + $this->assertEquals($result, $expected); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php new file mode 100644 index 0000000000000..83e7a94c8e18c --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php @@ -0,0 +1,202 @@ +logMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Log\Log') + ->disableOriginalConstructor() + ->setMethods(['add']) + ->getMock(); + + $this->classesScanner = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\ClassesScanner') + ->disableOriginalConstructor() + ->setMethods(['getList']) + ->getMock(); + + $this->classReaderMock = $this->getMockBuilder('\Magento\Framework\Code\Reader\ClassReader') + ->disableOriginalConstructor() + ->setMethods(['getParents']) + ->getMock(); + + $this->validatorMock = $this->getMockBuilder('\Magento\Framework\Code\Validator') + ->disableOriginalConstructor() + ->setMethods(['validate']) + ->getMock(); + + $this->model = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Directory( + $this->logMock, + $this->classReaderMock, + $this->classesScanner, + $this->validatorMock, + '/var/generation' + ); + } + + public function testGetList() + { + $path = '/tmp/test'; + + $classes = [ 'NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $parents = [ + ['NameSpace1\ClassName1', ['Parent_Class_Name', 'Interface_1', 'Interface_2']], + ['NameSpace1\ClassName2', ['Parent_Class_Name', 'Interface_1', 'Interface_2']] + ]; + + $this->classReaderMock->expects($this->exactly(count($classes))) + ->method('getParents') + ->will($this->returnValueMap( + $parents + )); + + $this->logMock->expects($this->never()) + ->method('add'); + + $this->validatorMock->expects($this->exactly(count($classes))) + ->method('validate'); + + $this->model->getList($path); + $result = $this->model->getRelations(); + + $expected = [ + $classes[0] => $parents[0][1], + $classes[1] => $parents[1][1] + ]; + + $this->assertEquals($result, $expected); + } + + /** + * + */ + public function testGetListNoValidation() + { + $path = '/var/generation'; + + $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $parents = [ + ['NameSpace1\ClassName1', ['Parent_Class_Name', 'Interface_1', 'Interface_2']], + ['NameSpace1\ClassName2', ['Parent_Class_Name', 'Interface_1', 'Interface_2']] + ]; + + $this->classReaderMock->expects($this->exactly(count($classes))) + ->method('getParents') + ->will($this->returnValueMap( + $parents + )); + + $this->logMock->expects($this->never()) + ->method('add'); + + $this->validatorMock->expects($this->never()) + ->method('validate'); + + $this->model->getList($path); + $result = $this->model->getRelations(); + + $expected = [ + $classes[0] => $parents[0][1], + $classes[1] => $parents[1][1] + ]; + + $this->assertEquals($result, $expected); + } + + /** + * @dataProvider getListExceptionDataProvider + * + * @param $exception + */ + public function testGetListException(\Exception $exception) + { + $path = '/tmp/test'; + + $classes = ['NameSpace1\ClassName1']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $this->logMock->expects($this->once()) + ->method('add') + ->with(Log::COMPILATION_ERROR, $classes[0], $exception->getMessage()); + + $this->validatorMock->expects($this->exactly(count($classes))) + ->method('validate') + ->will( + $this->throwException($exception) + ); + + $this->model->getList($path); + + $result = $this->model->getRelations(); + + $this->assertEquals($result, []); + } + + /** + * DataProvider for test testGetListException + * + * @return array + */ + public function getListExceptionDataProvider() + { + return [ + [new \Magento\Framework\Code\ValidationException('Not Valid!')], + [new \ReflectionException('Not Valid!')] + ]; + } +} diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php new file mode 100644 index 0000000000000..ade6e6ce90abc --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php @@ -0,0 +1,174 @@ +logMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Log\Log') + ->disableOriginalConstructor() + ->setMethods(['add', 'report']) + ->getMock(); + + $this->classesScanner = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\ClassesScanner') + ->disableOriginalConstructor() + ->setMethods(['getList']) + ->getMock(); + + $this->classReaderMock = $this->getMockBuilder('\Magento\Framework\Code\Reader\ClassReader') + ->disableOriginalConstructor() + ->setMethods(['getParents']) + ->getMock(); + + $this->validatorMock = $this->getMockBuilder('\Magento\Framework\Code\Validator') + ->disableOriginalConstructor() + ->setMethods(['validate']) + ->getMock(); + + $this->model = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Interceptions( + $this->classesScanner, + $this->classReaderMock, + $this->validatorMock, + $this->logMock + ); + } + + /** + * + */ + public function testGetList() + { + $path = '/tmp/test'; + + $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $this->logMock->expects($this->never()) + ->method('add'); + + $this->logMock->expects($this->once())->method('report'); + + $this->validatorMock->expects($this->exactly(count($classes))) + ->method('validate'); + + $result = $this->model->getList($path); + + $this->assertEquals($result, $classes); + } + + /** + * + */ + public function testGetListNoValidation() + { + $path = '/var/generation'; + + $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $this->logMock->expects($this->never()) + ->method('add'); + + $this->validatorMock->expects($this->never()) + ->method('validate'); + + $this->logMock->expects($this->once())->method('report'); + + $result = $this->model->getList($path); + + $this->assertEquals($result, $classes); + } + + /** + * @dataProvider getListExceptionDataProvider + * + * @param $exception + */ + public function testGetListException(\Exception $exception) + { + $path = '/tmp/test'; + + $classes = ['NameSpace1\ClassName1']; + + $this->classesScanner->expects($this->once()) + ->method('getList') + ->with($path) + ->willReturn($classes); + + $this->logMock->expects($this->once()) + ->method('add') + ->with(Log::COMPILATION_ERROR, $classes[0], $exception->getMessage()); + + $this->validatorMock->expects($this->exactly(count($classes))) + ->method('validate') + ->will( + $this->throwException($exception) + ); + + $this->logMock->expects($this->once())->method('report'); + + $result = $this->model->getList($path); + + $this->assertEquals($result, []); + } + + /** + * DataProvider for test testGetListException + * + * @return array + */ + public function getListExceptionDataProvider() + { + return [ + [new \Magento\Framework\Code\ValidationException('Not Valid!')], + [new \ReflectionException('Not Valid!')] + ]; + } +} diff --git a/dev/tools/Magento/Tools/Di/App/Compiler.php b/dev/tools/Magento/Tools/Di/App/Compiler.php index 47ca458c42b69..bbb2ab1e0de67 100644 --- a/dev/tools/Magento/Tools/Di/App/Compiler.php +++ b/dev/tools/Magento/Tools/Di/App/Compiler.php @@ -59,7 +59,9 @@ public function launch() 'preferences' => [ 'Magento\Tools\Di\Compiler\Config\WriterInterface' => - 'Magento\Tools\Di\Compiler\Config\Writer\Filesystem' + 'Magento\Tools\Di\Compiler\Config\Writer\Filesystem', + 'Magento\Tools\Di\Compiler\Log\Writer\WriterInterface' => + 'Magento\Tools\Di\Compiler\Log\Writer\Console' ] ] ); diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php index 46a0892cc8064..fd6107202bbf5 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/Area.php @@ -8,7 +8,6 @@ use Magento\Tools\Di\App\Task\OperationInterface; use Magento\Framework\App; use Magento\Tools\Di\Code\Reader\ClassesScanner; -use Magento\Tools\Di\Code\Reader\InstancesNamesListInterface; use Magento\Tools\Di\Compiler\Config; use Magento\Tools\Di\Definition\Collection as DefinitionsCollection; @@ -20,7 +19,7 @@ class Area implements OperationInterface private $areaList; /** - * @var InstancesNamesListInterface + * @var \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area */ private $areaInstancesNamesList; @@ -41,14 +40,14 @@ class Area implements OperationInterface /** * @param App\AreaList $areaList - * @param InstancesNamesListInterface $instancesNamesList + * @param \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area $instancesNamesList * @param Config\Reader $configReader * @param Config\WriterInterface $configWriter * @param array $data */ public function __construct( App\AreaList $areaList, - InstancesNamesList\Area $areaInstancesNamesList, + \Magento\Tools\Di\Code\Reader\InstancesNamesList\Area $areaInstancesNamesList, Config\Reader $configReader, Config\WriterInterface $configWriter, $data = [] diff --git a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php index 8c6adf686b848..1a42794f21ce6 100644 --- a/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php +++ b/dev/tools/Magento/Tools/Di/App/Task/Operation/InterceptionCache.php @@ -6,8 +6,6 @@ namespace Magento\Tools\Di\App\Task\Operation; use Magento\Tools\Di\App\Task\OperationInterface; -use Magento\Tools\Di\Code\Reader\ClassesScanner; -use Magento\Tools\Di\Code\Reader\InstancesNamesListInterface; class InterceptionCache implements OperationInterface { @@ -22,20 +20,20 @@ class InterceptionCache implements OperationInterface private $configInterface; /** - * @var InstancesNamesList\Interceptions + * @var \Magento\Tools\Di\Code\Reader\InstancesNamesList\Interceptions */ private $interceptionsInstancesNamesList; /** - * @param \Magento\Framework\Interception\Config\Config $configInterface - * @param ClassesScanner $classesScanner - * @param InstancesNamesList\Interceptions $interceptionsInstancesNamesList - * @param array $data + * @param \Magento\Framework\Interception\Config\Config $configInterface + * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner + * @param \Magento\Tools\Di\Code\Reader\InstancesNamesList\Interceptions $interceptionsInstancesNamesList + * @param array $data */ public function __construct( \Magento\Framework\Interception\Config\Config $configInterface, - ClassesScanner $classesScanner, - InstancesNamesList\Interceptions $interceptionsInstancesNamesList, + \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner, + \Magento\Tools\Di\Code\Reader\InstancesNamesList\Interceptions $interceptionsInstancesNamesList, array $data = [] ) { $this->data = $data; diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php index 310452b0283f2..bddcc4d26bb77 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php @@ -5,7 +5,6 @@ */ namespace Magento\Tools\Di\Code\Reader\InstancesNamesList; -use Magento\Framework\Code\Reader\ClassReader; use Magento\Tools\Di\Compiler\Log\Log; /** @@ -21,7 +20,7 @@ class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInter private $current; /** - * @var Log + * @var \Magento\Tools\Di\Compiler\Log\Log */ private $log; @@ -36,7 +35,7 @@ class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInter private $validator; /** - * @var \Magento\Tools\Di\Code\Reader\ClassReaderDecorator + * @var \Magento\Framework\Code\Reader\ClassReader */ private $classReader; @@ -46,21 +45,25 @@ class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInter private $classesScanner; /** - * @param Log $log Logging object - * @param string $generationDir directory where generated files is + * @param \Magento\Tools\Di\Compiler\Log\Log $log Logging object + * @param \Magento\Framework\Code\Reader\ClassReader $classReader + * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner + * @param \Magento\Framework\Code\Validator $validator + * @param string $generationDir directory where generated files is */ - public function __construct(Log $log, $generationDir) - { - $this->classReader = new ClassReader(); - $this->classesScanner = new \Magento\Tools\Di\Code\Reader\ClassesScanner(); - + public function __construct( + \Magento\Tools\Di\Compiler\Log\Log $log, + \Magento\Framework\Code\Reader\ClassReader $classReader, + \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner, + \Magento\Framework\Code\Validator $validator, + $generationDir + ) { $this->log = $log; + $this->classReader = $classReader; + $this->classesScanner = $classesScanner; + $this->validator = $validator; $this->generationDir = $generationDir; - $this->validator = new \Magento\Framework\Code\Validator(); - $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); - set_error_handler([$this, 'errorHandler'], E_STRICT); } @@ -83,8 +86,6 @@ public function errorHandler($errorNumber, $msg) * @param string $path path to dir with files * * @return array - * - * @throws \Magento\Framework\Filesystem\FilesystemException */ public function getList($path) { diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php index 51f67a5407b65..e1480ffd868d0 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php @@ -5,7 +5,6 @@ */ namespace Magento\Tools\Di\Code\Reader\InstancesNamesList; -use Magento\Framework\Code\Reader\ClassReader; use Magento\Tools\Di\Compiler\Log\Log; /** @@ -26,7 +25,7 @@ class Interceptions implements \Magento\Tools\Di\Code\Reader\InstancesNamesListI private $classesScanner; /** - * @var Log + * @var \Magento\Tools\Di\Compiler\Log\Log */ private $log; @@ -37,21 +36,21 @@ class Interceptions implements \Magento\Tools\Di\Code\Reader\InstancesNamesListI /** * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner - * @param ClassReader $classReader + * @param \Magento\Framework\Code\Reader\ClassReader $classReader + * @param \Magento\Framework\Code\Validator $validator + * @param Log $log */ public function __construct( \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner, - ClassReader $classReader + \Magento\Framework\Code\Reader\ClassReader $classReader, + \Magento\Framework\Code\Validator $validator, + Log $log ) { $this->classReader = $classReader; $this->classesScanner = $classesScanner; + $this->validator = $validator; + $this->log = $log; - $this->log = new \Magento\Tools\Di\Compiler\Log\Log( - new \Magento\Tools\Di\Compiler\Log\Writer\Quiet(), - new \Magento\Tools\Di\Compiler\Log\Writer\Console() - ); - - $this->validator = new \Magento\Framework\Code\Validator(); $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); } @@ -62,8 +61,6 @@ public function __construct( * @param string $path path to dir with files * * @return array - * - * @throws \Magento\Framework\Filesystem\FilesystemException */ public function getList($path) { diff --git a/dev/tools/Magento/Tools/Di/compiler.php b/dev/tools/Magento/Tools/Di/compiler.php index 8ea98c787042e..608f35f62fa85 100644 --- a/dev/tools/Magento/Tools/Di/compiler.php +++ b/dev/tools/Magento/Tools/Di/compiler.php @@ -140,7 +140,19 @@ // 2. Compilation // 2.1 Code scan - $directoryInstancesNamesList = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Directory($log, $generationDir); + + $validator = new \Magento\Framework\Code\Validator(); + $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); + $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + + $directoryInstancesNamesList = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Directory( + $log, + new \Magento\Framework\Code\Reader\ClassReader(), + new \Magento\Tools\Di\Code\Reader\ClassesScanner(), + $validator, + $generationDir + ); + foreach ($compilationDirs as $path) { if (is_readable($path)) { $directoryInstancesNamesList->getList($path); From fe63955098afe41a00e640cacd819f422a150cb9 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Mon, 2 Feb 2015 19:58:43 +0200 Subject: [PATCH 26/57] MAGETWO-30801: Asynchronous products addition to cart - fix tests --- app/code/Magento/Catalog/Block/Product/ListProduct.php | 3 ++- .../Catalog/view/frontend/templates/product/list.phtml | 5 ++--- app/code/Magento/Checkout/Controller/Cart.php | 1 + .../Magento/Catalog/Block/Product/ListProductTest.php | 1 - .../testsuite/Magento/Checkout/Controller/CartTest.php | 8 -------- 5 files changed, 5 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Catalog/Block/Product/ListProduct.php b/app/code/Magento/Catalog/Block/Product/ListProduct.php index 3aa28a859720d..7526e7a4db35d 100644 --- a/app/code/Magento/Catalog/Block/Product/ListProduct.php +++ b/app/code/Magento/Catalog/Block/Product/ListProduct.php @@ -333,7 +333,8 @@ public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product) 'action' => $url, 'data' => [ 'product' => $product->getEntityId(), - \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->_postDataHelper->getEncodedUrl($url), + \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => + $this->_postDataHelper->getEncodedUrl($url), ] ]; } diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml index 0f971f260136c..d7722247747b1 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml @@ -71,7 +71,7 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
>
> isSaleable()): ?> - getAddToCartPostParams($_product); ?> + getAddToCartPostParams($_product); ?>
@@ -118,7 +118,7 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
- getToolbarHtml(); ?> + getToolbarHtml() ?> - getToolbarHtml() ?> diff --git a/app/code/Magento/Checkout/Controller/Cart.php b/app/code/Magento/Checkout/Controller/Cart.php index 3497f99691a76..938e80039aa03 100644 --- a/app/code/Magento/Checkout/Controller/Cart.php +++ b/app/code/Magento/Checkout/Controller/Cart.php @@ -65,6 +65,7 @@ public function __construct( /** * Set back redirect url to response * + * @param null|string $backUrl * @return $this */ protected function _goBack($backUrl = null) diff --git a/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php b/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php index 9b74a0c67abb9..b762d1f127c65 100644 --- a/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php +++ b/dev/tests/unit/testsuite/Magento/Catalog/Block/Product/ListProductTest.php @@ -133,7 +133,6 @@ public function testGetAddToCartPostParams() $url = 'http://localhost.com/dev/'; $id = 1; $uenc = strtr(base64_encode($url), '+/=', '-_,'); - $data = ['product' => $id, \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $uenc]; $expectedPostData = [ 'action' => $url, 'data' => ['product' => $id, 'uenc' => $uenc], diff --git a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php index 208015c54eb9c..250e0109fc68e 100644 --- a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php +++ b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php @@ -61,14 +61,6 @@ public function testGoBack() $this->returnSelf() ); - $redirect->expects( - $this->once() - )->method( - 'redirect' - )->will( - $this->returnValue('http://some-url/index.php/checkout/cart/') - ); - $storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false); $storeMock->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://some-url/')); From fd3ef4e5402e68008c600df198e1bfcfdbc476fc Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 3 Feb 2015 11:03:05 +0200 Subject: [PATCH 27/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Reader/{InstancesNameList => InstancesNamesList}/AreaTest.php | 0 .../{InstancesNameList => InstancesNamesList}/DirectoryTest.php | 0 .../InterceptionsTest.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/{InstancesNameList => InstancesNamesList}/AreaTest.php (100%) rename dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/{InstancesNameList => InstancesNamesList}/DirectoryTest.php (100%) rename dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/{InstancesNameList => InstancesNamesList}/InterceptionsTest.php (100%) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php similarity index 100% rename from dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/AreaTest.php rename to dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php similarity index 100% rename from dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/DirectoryTest.php rename to dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php similarity index 100% rename from dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNameList/InterceptionsTest.php rename to dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php From 5ab72be1e19b51ac5e51ece310bfa7e274189dad Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 3 Feb 2015 11:13:53 +0200 Subject: [PATCH 28/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Remove definitions from tests - Fix tests --- .../Code/Reader/InstancesNamesList/AreaTest.php | 12 +++--------- .../Reader/InstancesNamesList/DirectoryTest.php | 16 +++++----------- .../InstancesNamesList/InterceptionsTest.php | 13 +++---------- .../Di/Code/Reader/InstancesNamesList/Area.php | 4 ++-- .../Code/Reader/InstancesNamesList/Directory.php | 10 +++++----- .../Reader/InstancesNamesList/Interceptions.php | 14 +++++++++----- 6 files changed, 27 insertions(+), 42 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php index 26c403eede741..0653819edea9d 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/AreaTest.php @@ -27,9 +27,6 @@ class AreaTest extends \PHPUnit_Framework_TestCase */ private $model; - /** - * - */ protected function setUp() { $this->classesScannerMock = $this->getMockBuilder('\Magento\Tools\Di\Code\Reader\ClassesScanner') @@ -48,14 +45,11 @@ protected function setUp() ); } - /** - * - */ public function testGetList() { $path = '/tmp/test'; - $classes = [ 'NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2']; $this->classesScannerMock->expects($this->once()) ->method('getList') @@ -76,8 +70,8 @@ public function testGetList() $result = $this->model->getList($path); $expected = [ - $classes[0] => $constructors[0][1], - $classes[1] => $constructors[1][1] + $classes[0] => $constructors[0][1], + $classes[1] => $constructors[1][1] ]; $this->assertEquals($result, $expected); diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php index 83e7a94c8e18c..691f4ecb563e0 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/DirectoryTest.php @@ -39,9 +39,6 @@ class DirectoryTest extends \PHPUnit_Framework_TestCase */ private $logMock; - /** - * - */ protected function setUp() { $this->logMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Log\Log') @@ -77,7 +74,7 @@ public function testGetList() { $path = '/tmp/test'; - $classes = [ 'NameSpace1\ClassName1', 'NameSpace1\ClassName2']; + $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2']; $this->classesScanner->expects($this->once()) ->method('getList') @@ -105,16 +102,13 @@ public function testGetList() $result = $this->model->getRelations(); $expected = [ - $classes[0] => $parents[0][1], - $classes[1] => $parents[1][1] + $classes[0] => $parents[0][1], + $classes[1] => $parents[1][1] ]; $this->assertEquals($result, $expected); } - /** - * - */ public function testGetListNoValidation() { $path = '/var/generation'; @@ -147,8 +141,8 @@ public function testGetListNoValidation() $result = $this->model->getRelations(); $expected = [ - $classes[0] => $parents[0][1], - $classes[1] => $parents[1][1] + $classes[0] => $parents[0][1], + $classes[1] => $parents[1][1] ]; $this->assertEquals($result, $expected); diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php index ade6e6ce90abc..1c090deb54701 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Code/Reader/InstancesNamesList/InterceptionsTest.php @@ -39,9 +39,6 @@ class InterceptionsTest extends \PHPUnit_Framework_TestCase */ private $logMock; - /** - * - */ protected function setUp() { $this->logMock = $this->getMockBuilder('Magento\Tools\Di\Compiler\Log\Log') @@ -61,20 +58,19 @@ protected function setUp() $this->validatorMock = $this->getMockBuilder('\Magento\Framework\Code\Validator') ->disableOriginalConstructor() - ->setMethods(['validate']) + ->setMethods(['validate', 'add']) ->getMock(); $this->model = new \Magento\Tools\Di\Code\Reader\InstancesNamesList\Interceptions( $this->classesScanner, $this->classReaderMock, $this->validatorMock, + new \Magento\Framework\Code\Validator\ConstructorIntegrity(), + new \Magento\Framework\Code\Validator\ContextAggregation(), $this->logMock ); } - /** - * - */ public function testGetList() { $path = '/tmp/test'; @@ -99,9 +95,6 @@ public function testGetList() $this->assertEquals($result, $classes); } - /** - * - */ public function testGetListNoValidation() { $path = '/var/generation'; diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php index bf43d1cc9edcd..1fae763a1eed2 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Area.php @@ -23,7 +23,7 @@ class Area implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInterface private $classesScanner; /** - * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner + * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner * @param \Magento\Tools\Di\Code\Reader\ClassReaderDecorator $classReaderDecorator */ public function __construct( @@ -40,7 +40,6 @@ public function __construct( * @param string $path path to dir with files * * @return array - * * @throws \Magento\Framework\Filesystem\FilesystemException */ public function getList($path) @@ -49,6 +48,7 @@ public function getList($path) foreach ($this->classesScanner->getList($path) as $className) { $classes[$className] = $this->classReaderDecorator->getConstructor($className); } + return $classes; } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php index bddcc4d26bb77..8cec8b58f34b9 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Directory.php @@ -45,11 +45,11 @@ class Directory implements \Magento\Tools\Di\Code\Reader\InstancesNamesListInter private $classesScanner; /** - * @param \Magento\Tools\Di\Compiler\Log\Log $log Logging object - * @param \Magento\Framework\Code\Reader\ClassReader $classReader + * @param \Magento\Tools\Di\Compiler\Log\Log $log Logging object + * @param \Magento\Framework\Code\Reader\ClassReader $classReader * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner - * @param \Magento\Framework\Code\Validator $validator - * @param string $generationDir directory where generated files is + * @param \Magento\Framework\Code\Validator $validator + * @param string $generationDir directory where generated files is */ public function __construct( \Magento\Tools\Di\Compiler\Log\Log $log, @@ -77,7 +77,7 @@ public function __construct( */ public function errorHandler($errorNumber, $msg) { - $this->log->add(Log::COMPILATION_ERROR, $this->current, '#'. $errorNumber .' '. $msg); + $this->log->add(Log::COMPILATION_ERROR, $this->current, '#' . $errorNumber . ' ' . $msg); } /** diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php index e1480ffd868d0..7c04e4e336270 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/InstancesNamesList/Interceptions.php @@ -36,14 +36,18 @@ class Interceptions implements \Magento\Tools\Di\Code\Reader\InstancesNamesListI /** * @param \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner - * @param \Magento\Framework\Code\Reader\ClassReader $classReader - * @param \Magento\Framework\Code\Validator $validator - * @param Log $log + * @param \Magento\Framework\Code\Reader\ClassReader $classReader + * @param \Magento\Framework\Code\Validator $validator + * @param \Magento\Framework\Code\Validator\ConstructorIntegrity $constructorIntegrityValidator + * @param \Magento\Framework\Code\Validator\ContextAggregation $contextAggregationValidator + * @param Log $log */ public function __construct( \Magento\Tools\Di\Code\Reader\ClassesScanner $classesScanner, \Magento\Framework\Code\Reader\ClassReader $classReader, \Magento\Framework\Code\Validator $validator, + \Magento\Framework\Code\Validator\ConstructorIntegrity $constructorIntegrityValidator, + \Magento\Framework\Code\Validator\ContextAggregation $contextAggregationValidator, Log $log ) { $this->classReader = $classReader; @@ -51,8 +55,8 @@ public function __construct( $this->validator = $validator; $this->log = $log; - $this->validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $this->validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); + $this->validator->add($constructorIntegrityValidator); + $this->validator->add($contextAggregationValidator); } /** From da6fae6c14dc987eeea76788f55f36981f0af0f6 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 12:26:18 +0200 Subject: [PATCH 29/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - added test for data builder factory --- app/bootstrap.php | 2 +- .../Serialization/DataBuilderFactoryTest.php | 57 +++++++++++++++++++ .../Tools/Di/Compiler/ArgumentsResolver.php | 38 ++++++++----- 3 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/Serialization/DataBuilderFactoryTest.php diff --git a/app/bootstrap.php b/app/bootstrap.php index 740ef5fbe9278..183edeceabf33 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -8,7 +8,7 @@ * Environment initialization */ error_reporting(E_ALL); -ini_set('display_errors', 1); +#ini_set('display_errors', 1); umask(0); /* PHP version validation */ diff --git a/dev/tests/unit/testsuite/Magento/Framework/Serialization/DataBuilderFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/Serialization/DataBuilderFactoryTest.php new file mode 100644 index 0000000000000..aea5cc03c4412 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/Serialization/DataBuilderFactoryTest.php @@ -0,0 +1,57 @@ +objectManager = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->setMethods([]) + ->getMock(); + + $this->dataBuilderFactory = new DataBuilderFactory($this->objectManager); + } + + /** + * @param string $className + * @param string $expectedBuilderName + * @dataProvider classNamesDataProvider + */ + public function testGetBuilderClassName($className, $expectedBuilderName) + { + $this->objectManager->expects($this->once()) + ->method('create') + ->with($expectedBuilderName) + ->willReturn(new \StdClass); + + $this->assertInstanceOf('StdClass', $this->dataBuilderFactory->getDataBuilder($className)); + } + + /** + * @return array + */ + public function classNamesDataProvider() + { + return [ + ['\\TypeInterface', 'TypeDataBuilder'], + ['\\Type', 'TypeBuilder'] + ]; + } +} diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index 25aa4f2735886..dbf51cc7bf393 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -19,7 +19,7 @@ class ArgumentsResolver * * @var array */ - private $sharedInstanceArgumentPattern = [ + private $sharedInstancePattern = [ '_i_' => null, ]; @@ -28,7 +28,7 @@ class ArgumentsResolver * * @var array */ - private $notSharedInstanceArgumentPattern = [ + private $notSharedInstancePattern = [ '_ins_' => null, ]; @@ -37,25 +37,34 @@ class ArgumentsResolver * * @var array */ - private $valueArgumentPattern = [ + private $valuePattern = [ '_v_' => null, ]; /** - * Value argument pattern used for configuration + * Value null argument pattern used for configuration * * @var array */ - private $nullValueArgumentPattern = [ + private $nullValuePattern = [ '_vn_' => true, ]; + /** + * Value configured array argument pattern used for configuration + * + * @var array + */ + private $configuredArrayValuePattern = [ + '_vac_' => true, + ]; + /** * Configured argument pattern used for configuration * * @var array */ - private $configuredArgumentPattern = [ + private $configuredPattern = [ '_a_' => null, '_d_' => null ]; @@ -162,10 +171,10 @@ private function getConfiguredInstanceArgument($config) $argument = $this->getInstanceArgument($config['instance']); if (isset($config['shared'])) { if ($config['shared']) { - $pattern = $this->sharedInstanceArgumentPattern; + $pattern = $this->sharedInstancePattern; $pattern['_i_'] = current($argument); } else { - $pattern = $this->notSharedInstanceArgumentPattern; + $pattern = $this->notSharedInstancePattern; $pattern['_ins_'] = current($argument); } $argument = $pattern; @@ -203,10 +212,10 @@ function ($type) { private function getInstanceArgument($instanceType) { if ($this->diContainerConfig->isShared($instanceType)) { - $argument = $this->sharedInstanceArgumentPattern; + $argument = $this->sharedInstancePattern; $argument['_i_'] = $instanceType; } else { - $argument = $this->notSharedInstanceArgumentPattern; + $argument = $this->notSharedInstancePattern; $argument['_ins_'] = $instanceType; } return $argument; @@ -221,14 +230,15 @@ private function getInstanceArgument($instanceType) private function getNonObjectArgument($value) { if (is_null($value)) { - return $this->nullValueArgumentPattern; + return $this->nullValuePattern; } - $argument = $this->valueArgumentPattern; + $argument = $this->valuePattern; if (is_array($value)) { if ($this->isConfiguredArray($value)) { $value = $this->getConfiguredArrayAttribute($value); - $argument = ['_vac_' => $value]; + $argument = $this->configuredArrayValuePattern; + $argument['_vac_'] = $value; return $argument; } } @@ -271,7 +281,7 @@ private function isConfiguredArray($value) */ private function getGlobalArgument($value, $default) { - $argument = $this->configuredArgumentPattern; + $argument = $this->configuredPattern; $argument['_a_'] = $value; $argument['_d_'] = $default; return $argument; From 9a99eac542847968371eda2742f8014a88e4149c Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 12:56:54 +0200 Subject: [PATCH 30/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - fixed style defects --- .../Factory/Fixture/Compiled/SimpleClassTesting.php | 7 ++----- dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php | 4 ++-- .../Magento/Framework/Interception/Config/Config.php | 2 +- .../Framework/Interception/PluginList/PluginList.php | 1 + .../Magento/Framework/ObjectManager/Config/Compiled.php | 4 ++++ .../Magento/Framework/ObjectManager/Factory/Compiled.php | 2 ++ .../Magento/Framework/Serialization/DataBuilderFactory.php | 4 ++-- 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php index a328092268fd4..c8e3211ea8efe 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -32,7 +32,7 @@ class SimpleClassTesting * @var string */ private $globalValue; - + /** * @var */ @@ -50,10 +50,7 @@ public function __construct( \StdClass $nonSharedDependency, \StdClass $sharedDependency, $value = 'value', - array $valueArray = [ - 'default_value1', - 'default_value2' - ], + array $valueArray = ['default_value1', 'default_value2'], $globalValue = '', $nullValue = null ) { diff --git a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php index dbf51cc7bf393..e50ea54db4225 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php +++ b/dev/tools/Magento/Tools/Di/Compiler/ArgumentsResolver.php @@ -163,10 +163,10 @@ private function getConfiguredArrayAttribute($array) /** * Returns configured instance argument * - * @param $config + * @param array $config * @return array|mixed */ - private function getConfiguredInstanceArgument($config) + private function getConfiguredInstanceArgument(array $config) { $argument = $this->getInstanceArgument($config['instance']); if (isset($config['shared'])) { diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index edda3288ca18f..7c9ede12daf5c 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -126,7 +126,7 @@ public function initialize($classDefinitions = []) foreach ($config as $typeName => $typeConfig) { $this->hasPlugins($typeName); } - foreach ($classDefinitions as $key => $class) { + foreach ($classDefinitions as $class) { $this->hasPlugins($class); } $this->_cache->save(serialize($this->_intercepted), $this->_cacheId); diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index 7e330fa632b6f..a1a7a29ba3313 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -189,6 +189,7 @@ protected function _inheritPlugins($type) * Trims starting backslash from plugin instance name * * @param array $plugins + * @return void */ private function trimInstanceStartingBackslash(&$plugins) { diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index 181a6b146c697..e94df3c02d49e 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -48,6 +48,8 @@ public function __construct($data) * @param RelationsInterface $relations * * @return void + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function setRelations(RelationsInterface $relations) { @@ -59,6 +61,8 @@ public function setRelations(RelationsInterface $relations) * @param ConfigCacheInterface $cache * * @return void + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function setCache(ConfigCacheInterface $cache) { diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 8281b6ebf179a..d21ffedfce2df 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -65,6 +65,8 @@ public function create($requestedType, array $arguments = []) * @param array $array * * @return void + * + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function parseArray(&$array) { diff --git a/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php b/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php index 57478bb8ea8c7..a64670910d253 100644 --- a/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php +++ b/lib/internal/Magento/Framework/Serialization/DataBuilderFactory.php @@ -41,7 +41,7 @@ public function getDataBuilder($className) /** * Returns builder class name * - * @param $className + * @param string $className * @return string */ protected function getBuilderClassName($className) @@ -62,7 +62,7 @@ protected function getBuilderClassName($className) /** * Creates builder object * - * @param $builderClassName + * @param string $builderClassName * @return \Magento\Framework\Api\BuilderInterface Builder Instance */ protected function createObject($builderClassName) From ca601518e0b92d867b1aba5c8f841e2cc536b0af Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 13:08:50 +0200 Subject: [PATCH 31/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - code style defect fixed --- .../Factory/Fixture/Compiled/SimpleClassTesting.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php index c8e3211ea8efe..f6c6c1e1dc075 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -50,7 +50,10 @@ public function __construct( \StdClass $nonSharedDependency, \StdClass $sharedDependency, $value = 'value', - array $valueArray = ['default_value1', 'default_value2'], + array $valueArray = [ + 'default_value1', + 'default_value2' + ], $globalValue = '', $nullValue = null ) { From 9a830b07967dea68b5c76341cb97bcac4ef24e76 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 13:52:25 +0200 Subject: [PATCH 32/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - fixed block factory to trimm starting backslash --- .../Factory/Fixture/Compiled/SimpleClassTesting.php | 4 ++-- lib/internal/Magento/Framework/View/Element/BlockFactory.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php index f6c6c1e1dc075..feff51b14d2ff 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php +++ b/dev/tests/unit/testsuite/Magento/Framework/ObjectManager/Factory/Fixture/Compiled/SimpleClassTesting.php @@ -51,8 +51,8 @@ public function __construct( \StdClass $sharedDependency, $value = 'value', array $valueArray = [ - 'default_value1', - 'default_value2' + 'default_value1', + 'default_value2' ], $globalValue = '', $nullValue = null diff --git a/lib/internal/Magento/Framework/View/Element/BlockFactory.php b/lib/internal/Magento/Framework/View/Element/BlockFactory.php index 2d5a7c10969ef..8832e6e40bb89 100644 --- a/lib/internal/Magento/Framework/View/Element/BlockFactory.php +++ b/lib/internal/Magento/Framework/View/Element/BlockFactory.php @@ -39,6 +39,7 @@ public function __construct(ObjectManagerInterface $objectManager) */ public function createBlock($blockName, array $arguments = []) { + $blockName = ltrim($blockName, '\\'); $block = $this->objectManager->create($blockName, $arguments); if (!$block instanceof BlockInterface) { throw new \LogicException($blockName . ' does not implemented BlockInterface'); From c813a16f854bfe25b1f1d316979c10cb49b7d885 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 14:55:01 +0200 Subject: [PATCH 33/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - ltrim placed to objectmanager::create - todo added --- lib/internal/Magento/Framework/ObjectManager/ObjectManager.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php index 57e2db9ea1e88..53d3fdf05daa1 100644 --- a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php +++ b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php @@ -53,6 +53,8 @@ public function __construct(FactoryInterface $factory, ConfigInterface $config, */ public function create($type, array $arguments = []) { + /** @TODO get rid of ltrim() usage and place it to client code */ + $type = ltrim($type, '\\'); return $this->_factory->create($this->_config->getPreference($type), $arguments); } From e00d7a46954fd04c3b2fe6efb354925f7ac11950 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 3 Feb 2015 16:04:37 +0200 Subject: [PATCH 34/57] MAGETWO-31112: Multi-tenant mode OM optimization solution integration - Merge fix --- .../testsuite/Magento/Test/Legacy/_files/obsolete_methods.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php index 0dae9b1fb052f..1fb48d58b21c0 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php @@ -2041,7 +2041,6 @@ ['getResourceCollection', 'Magento\CatalogSearch\Model\Resource\EngineInterface'], ['getResultCollection', 'Magento\CatalogSearch\Model\Resource\EngineInterface'], ['getAdvancedResultCollection', 'Magento\CatalogSearch\Model\Resource\EngineInterface'], - ['getEntityTypeId', 'Magento\Customer\Model\Customer'], ['setIsConfigurable', 'Magento\Catalog\Api\Data\ProductAttributeDataBuilder'], ['_getSendfriendModel', 'Magento\Sendfriend\Block\Send'], ['_initSendToFriendModel', 'Magento\Sendfriend\Controller\Product'], From f47a9e2ddb6174118d0fd1c142758cbda8a2a499 Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Tue, 3 Feb 2015 16:19:43 +0200 Subject: [PATCH 35/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - ltrim moved back to compiled factory - todo added --- .../Magento/Framework/ObjectManager/Factory/Compiled.php | 2 ++ lib/internal/Magento/Framework/ObjectManager/ObjectManager.php | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index d21ffedfce2df..72b6377a36e56 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -19,6 +19,8 @@ class Compiled extends AbstractFactory */ public function create($requestedType, array $arguments = []) { + /** @TODO get rid of ltrim() usage and place it to client code */ + $requestedType = ltrim($requestedType, '\\'); $type = $this->config->getInstanceType($requestedType); $args = $this->config->getArguments($requestedType); if ($args === null) { diff --git a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php index 53d3fdf05daa1..57e2db9ea1e88 100644 --- a/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php +++ b/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php @@ -53,8 +53,6 @@ public function __construct(FactoryInterface $factory, ConfigInterface $config, */ public function create($type, array $arguments = []) { - /** @TODO get rid of ltrim() usage and place it to client code */ - $type = ltrim($type, '\\'); return $this->_factory->create($this->_config->getPreference($type), $arguments); } From 5672ce6896a3cb631b68013781d838727864a70c Mon Sep 17 00:00:00 2001 From: Dmytro Kvashnin Date: Wed, 4 Feb 2015 10:27:47 +0200 Subject: [PATCH 36/57] MAGETWO-33035: Optimize DI configuration to plain read in optimized way - fixed plugin definitions generation to match general pattern for instance name --- dev/tools/Magento/Tools/Di/compiler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tools/Magento/Tools/Di/compiler.php b/dev/tools/Magento/Tools/Di/compiler.php index 608f35f62fa85..2087d4627e031 100644 --- a/dev/tools/Magento/Tools/Di/compiler.php +++ b/dev/tools/Magento/Tools/Di/compiler.php @@ -205,7 +205,7 @@ $pluginDefinitionList = new \Magento\Framework\Interception\Definition\Runtime(); foreach ($pluginList as $type => $entityList) { foreach ($entityList as $entity) { - $pluginDefinitions[$entity] = $pluginDefinitionList->getMethodList($entity); + $pluginDefinitions[ltrim($entity, '\\')] = $pluginDefinitionList->getMethodList($entity); } } From cb4cd2f924221beb587c1e628e2e3836174d6db4 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Wed, 4 Feb 2015 13:10:48 +0200 Subject: [PATCH 37/57] MAGETWO-30801: Asynchronous products addition to cart - fix tests --- .../Magento/Catalog/Test/TestCase/Product/CrosssellTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/CrosssellTest.php b/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/CrosssellTest.php index 2f71f814a2ac3..f1ecc04b18a76 100644 --- a/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/CrosssellTest.php +++ b/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/CrosssellTest.php @@ -90,6 +90,8 @@ public function testCreateCrosssell() $productPage->getViewBlock()->addToCart($configurable); $checkoutCartPage = Factory::getPageFactory()->getCheckoutCartIndex(); + $checkoutCartPage->getMessagesBlock()->waitSuccessMessage(); + $cartBlock = $checkoutCartPage->getCartBlock(); $this->assertTrue($cartBlock->isProductInShoppingCart($configurable)); $this->assertTrue($cartBlock->isProductInShoppingCart($simple1)); @@ -102,6 +104,8 @@ public function testCreateCrosssell() $productPage->getViewBlock()->addToCart($simple2); $checkoutCartPage = Factory::getPageFactory()->getCheckoutCartIndex(); + $checkoutCartPage->getMessagesBlock()->waitSuccessMessage(); + $cartBlock = $checkoutCartPage->getCartBlock(); $this->assertTrue($cartBlock->isProductInShoppingCart($configurable)); $this->assertTrue($cartBlock->isProductInShoppingCart($simple1)); From 056ebce041630d65541e471dbcd01ce6588df8b5 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Wed, 4 Feb 2015 15:46:45 +0200 Subject: [PATCH 38/57] MAGETWO-30801: Asynchronous products addition to cart - fix response --- app/code/Magento/Catalog/Controller/Product/View.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/code/Magento/Catalog/Controller/Product/View.php b/app/code/Magento/Catalog/Controller/Product/View.php index 0a06c8d0b9e0a..4de32d56cee47 100644 --- a/app/code/Magento/Catalog/Controller/Product/View.php +++ b/app/code/Magento/Catalog/Controller/Product/View.php @@ -94,6 +94,14 @@ public function execute() $this->messageManager->addNotice($notice); } $resultRedirect = $this->resultRedirectFactory->create(); + if ($this->getRequest()->isAjax()) { + $this->getResponse()->representJson( + $this->_objectManager->get('Magento\Core\Helper\Data')->jsonEncode([ + 'backUrl' => $this->_redirect->getRedirectUrl() + ]) + ); + return; + } $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } From 55b5b2d35242caac0f61985c666d90e60fca88ca Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Wed, 4 Feb 2015 17:25:00 +0200 Subject: [PATCH 39/57] MAGETWO-30801: Asynchronous products addition to cart - fix response --- app/code/Magento/Catalog/Controller/Product/View.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Controller/Product/View.php b/app/code/Magento/Catalog/Controller/Product/View.php index 4de32d56cee47..32f1d4da34305 100644 --- a/app/code/Magento/Catalog/Controller/Product/View.php +++ b/app/code/Magento/Catalog/Controller/Product/View.php @@ -93,7 +93,6 @@ public function execute() $notice = $product->getTypeInstance()->getSpecifyOptionMessage(); $this->messageManager->addNotice($notice); } - $resultRedirect = $this->resultRedirectFactory->create(); if ($this->getRequest()->isAjax()) { $this->getResponse()->representJson( $this->_objectManager->get('Magento\Core\Helper\Data')->jsonEncode([ @@ -102,6 +101,7 @@ public function execute() ); return; } + $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } From 7c1abf6dc7bf014df739e804df856d006e050e2d Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Wed, 4 Feb 2015 18:48:04 +0200 Subject: [PATCH 40/57] MAGETWO-30801: Asynchronous products addition to cart - fix flow (if redirect to cart enabled - do submit form synchronously) --- .../Catalog/Block/Product/AbstractProduct.php | 13 ++++++ .../frontend/templates/product/list.phtml | 18 ++++---- .../templates/product/view/addtocart.phtml | 43 ++++++++++++------- .../frontend/web/js/catalog-add-to-cart.js | 15 ++++++- app/code/Magento/Checkout/etc/config.xml | 2 +- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php index ea891535c263d..904af4739258e 100644 --- a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php +++ b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php @@ -585,4 +585,17 @@ public function getProductPriceHtml( } return $price; } + + /** + * Whether redirect to cart enabled + * + * @return bool + */ + public function getIsRedirectToCartEnabled() + { + return $this->_scopeConfig->getValue( + 'checkout/cart/redirect_to_cart', + \Magento\Framework\Store\ScopeInterface::SCOPE_STORE + ); + } } diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml index d7722247747b1..15dd7c8cc54ed 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml @@ -119,12 +119,14 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I
getToolbarHtml() ?> - + getIsRedirectToCartEnabled()) : ?> + + diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml index 588a9dba084cd..66ff8e0b1b5de 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml @@ -43,21 +43,32 @@
- diff --git a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js index 631646b64bac5..28f36bd7b3f2d 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js @@ -11,6 +11,8 @@ define([ $.widget('mage.catalogAddToCart', { options: { + processStart: null, + processStop: null, bindSubmit: true, minicartSelector: '[data-block="minicart"]', messagesSelector: '.messages', @@ -31,6 +33,10 @@ define([ }); }, + isLoaderEnabled: function() { + return this.options.processStart && this.options.processStop; + }, + submitForm: function(form) { var self = this; $.ajax({ @@ -39,10 +45,15 @@ define([ type: 'post', dataType: 'json', beforeSend: function() { - $('body').trigger('processStart'); + if (self.isLoaderEnabled()) { + $('body').trigger(self.options.processStart); + } }, success: function(res) { - $('body').trigger('processStop'); + if (self.isLoaderEnabled()) { + $('body').trigger(self.options.processStop); + } + if (res.backUrl) { window.location = res.backUrl; return; diff --git a/app/code/Magento/Checkout/etc/config.xml b/app/code/Magento/Checkout/etc/config.xml index d319d8ea62705..5d709676cb3b6 100644 --- a/app/code/Magento/Checkout/etc/config.xml +++ b/app/code/Magento/Checkout/etc/config.xml @@ -14,7 +14,7 @@ 30 - 1 + 0 1 From 661539bda3209bcad165f1b803ee4de9fe373c88 Mon Sep 17 00:00:00 2001 From: Kostiantyn Poida Date: Thu, 5 Feb 2015 14:23:00 +0200 Subject: [PATCH 41/57] MAGETWO-30801: Asynchronous products addition to cart - fix tests --- .../Magento/Catalog/Block/Product/AbstractProduct.php | 2 +- .../Catalog/view/frontend/templates/product/list.phtml | 2 +- .../view/frontend/templates/product/view/addtocart.phtml | 2 +- .../Catalog/Test/TestCase/Product/CrosssellTest.php | 9 ++++++--- .../Catalog/Test/TestCase/Product/RelatedProductTest.php | 3 ++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php index 904af4739258e..f2498cb12393d 100644 --- a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php +++ b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php @@ -591,7 +591,7 @@ public function getProductPriceHtml( * * @return bool */ - public function getIsRedirectToCartEnabled() + public function isRedirectToCartEnabled() { return $this->_scopeConfig->getValue( 'checkout/cart/redirect_to_cart', diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml index 15dd7c8cc54ed..0768aa2ceaeac 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml @@ -119,7 +119,7 @@ $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\I getToolbarHtml() ?> - getIsRedirectToCartEnabled()) : ?> + isRedirectToCartEnabled()) : ?> +
diff --git a/app/code/Magento/Catalog/view/frontend/web/js/list.js b/app/code/Magento/Catalog/view/frontend/web/js/list.js index 904a8f4b661dd..d9c4d0cdc9c88 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/list.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/list.js @@ -42,14 +42,6 @@ define([ e.preventDefault(); window.print(); }); - - $.each(this.options.selectors, function(i, selector) { - $(selector).on('click', function(e) { - e.preventDefault(); - window.location.href = $(this).data('url'); - }); - }); - } }); From 3f5539fbf0753a5edb018f3c1666da33e450cb93 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 10 Feb 2015 17:26:29 +0200 Subject: [PATCH 44/57] MAGETWO-30801: Asynchronous products addition to cart - fix Js messages --- .../Catalog/view/frontend/web/js/catalog-add-to-cart.js | 2 +- app/code/Magento/Theme/view/frontend/layout/default.xml | 1 + .../Theme/view/frontend/templates/html/messages.phtml | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Theme/view/frontend/templates/html/messages.phtml diff --git a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js index 28f36bd7b3f2d..1bb91fd2455e0 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js @@ -15,7 +15,7 @@ define([ processStop: null, bindSubmit: true, minicartSelector: '[data-block="minicart"]', - messagesSelector: '.messages', + messagesSelector: '[data-placeholder="messages"]', productStatusSelector: '.stock.available' }, diff --git a/app/code/Magento/Theme/view/frontend/layout/default.xml b/app/code/Magento/Theme/view/frontend/layout/default.xml index 35b28ffd0f583..6d06cca2917f5 100644 --- a/app/code/Magento/Theme/view/frontend/layout/default.xml +++ b/app/code/Magento/Theme/view/frontend/layout/default.xml @@ -55,6 +55,7 @@ + diff --git a/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml b/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml new file mode 100644 index 0000000000000..c53aa2dcc612f --- /dev/null +++ b/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml @@ -0,0 +1,9 @@ + +
\ No newline at end of file From 18a831ec9f31e893b69ca732c149a6dbc35da4c9 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 10 Feb 2015 18:18:00 +0200 Subject: [PATCH 45/57] MAGETWO-30801: Asynchronous products addition to cart - fix Js messages --- .../Magento/Theme/view/frontend/templates/html/messages.phtml | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml b/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml index c53aa2dcc612f..0896e85d906af 100644 --- a/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml +++ b/app/code/Magento/Theme/view/frontend/templates/html/messages.phtml @@ -3,7 +3,5 @@ * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile ?>
\ No newline at end of file From d2165462b7a561d845eb604873b9ded149becc0d Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Tue, 10 Feb 2015 18:52:50 +0200 Subject: [PATCH 46/57] MAGETWO-30801: Asynchronous products addition to cart - fix Js messages --- app/code/Magento/Theme/view/frontend/layout/default.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Theme/view/frontend/layout/default.xml b/app/code/Magento/Theme/view/frontend/layout/default.xml index 6d06cca2917f5..efce6ce1922d0 100644 --- a/app/code/Magento/Theme/view/frontend/layout/default.xml +++ b/app/code/Magento/Theme/view/frontend/layout/default.xml @@ -55,7 +55,7 @@ - + From 2fe889a9c5e1aa195f63870c7e88d8916234f06a Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Wed, 11 Feb 2015 14:51:57 +0200 Subject: [PATCH 47/57] MAGETWO-30801: Asynchronous products addition to cart - fdata-validate... attributes processed by jQuery.metadata - we don't need it in this case --- .../Catalog/view/frontend/web/product/view/validation.js | 4 ++-- .../view/frontend/templates/product/view/type/grouped.phtml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/view/frontend/web/product/view/validation.js b/app/code/Magento/Catalog/view/frontend/web/product/view/validation.js index 2c33dae8b99ef..8910b1b3db30a 100644 --- a/app/code/Magento/Catalog/view/frontend/web/product/view/validation.js +++ b/app/code/Magento/Catalog/view/frontend/web/product/view/validation.js @@ -19,8 +19,8 @@ options: { radioCheckboxClosest: 'ul, ol', errorPlacement: function (error, element) { - if (element.attr('data-validate-message-box')) { - var messageBox = $(element.attr('data-validate-message-box')); + if (element.attr('data-errors-message-box')) { + var messageBox = $(element.attr('data-errors-message-box')); messageBox.html(error); return; } diff --git a/app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml b/app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml index c2a27fa4ea0b1..8824109a28edd 100644 --- a/app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml +++ b/app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml @@ -52,7 +52,7 @@ title="" class="input-text qty" data-validate="{'validate-grouped-qty':'#super-product-table'}" - data-validate-message-box="#validate-message-box"/> + data-errors-message-box="#validation-message-box"/>
From d624d82957002983e2b156ec1aa1286887a81ba1 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Wed, 11 Feb 2015 16:51:09 +0200 Subject: [PATCH 48/57] MAGETWO-30801: Asynchronous products addition to cart --- .../Catalog/view/frontend/templates/product/compare/list.phtml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml index 339fe240147e6..aa0c404763765 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml @@ -67,7 +67,6 @@ From 977bf672a4cca303fb2f70325d15787f9cc6c515 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Thu, 12 Feb 2015 13:18:07 +0200 Subject: [PATCH 49/57] MAGETWO-30801: Asynchronous products addition to cart - fix benchmark.jmx according to changes in checkout flow --- dev/tests/performance/benchmark.jmx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dev/tests/performance/benchmark.jmx b/dev/tests/performance/benchmark.jmx index bde18fefbb95a..e7f66716c687d 100644 --- a/dev/tests/performance/benchmark.jmx +++ b/dev/tests/performance/benchmark.jmx @@ -566,13 +566,14 @@ false - count(//*[@class='cart item'])=1 + //*[@class='counter-number']/text()=1 false false true false true true + Make sure that minicart was refreshed @@ -713,13 +714,14 @@ false - count(//*[@class='cart item'])=2 + //*[@class='counter-number']/text()=2 false false true false true true + Make sure that minicart was refreshed From 053200c920263e05cd02a18147661170cfc9f7dd Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Thu, 12 Feb 2015 19:36:18 +0200 Subject: [PATCH 50/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/Checkout/Controller/Cart.php | 9 +++++++-- app/code/Magento/Checkout/Controller/Cart/Add.php | 12 ++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Checkout/Controller/Cart.php b/app/code/Magento/Checkout/Controller/Cart.php index 06acc88f940fa..4672356490fe4 100644 --- a/app/code/Magento/Checkout/Controller/Cart.php +++ b/app/code/Magento/Checkout/Controller/Cart.php @@ -75,13 +75,17 @@ public function __construct( * Set back redirect url to response * * @param null|string $backUrl - * @return $this + * + * @return \Magento\Framework\Controller\Result\Redirect */ protected function _goBack($backUrl = null) { + $resultRedirect = $this->resultRedirectFactory->create(); + if ($backUrl || $backUrl = $this->getBackUrl($this->_redirect->getRefererUrl())) { - $this->getResponse()->setRedirect($backUrl); + $resultRedirect->setUrl($backUrl); } + return $resultRedirect; } @@ -111,6 +115,7 @@ protected function _isInternalUrl($url) * Get resolved back url * * @param null $defaultUrl + * * @return mixed|null|string */ protected function getBackUrl($defaultUrl = null) diff --git a/app/code/Magento/Checkout/Controller/Cart/Add.php b/app/code/Magento/Checkout/Controller/Cart/Add.php index 1eec4bc447b66..332cd19f7da40 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Add.php +++ b/app/code/Magento/Checkout/Controller/Cart/Add.php @@ -143,11 +143,15 @@ public function execute() } $url = $this->_checkoutSession->getRedirectUrl(true); - if (!$url) { + + if ($url) { + return $this->resultRedirectFactory->create()->setUrl($url); + } else { $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); - return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($cartUrl)); + $url = $this->_redirect->getRedirectUrl($cartUrl); } - $this->goBack($url); + + return $this->goBack($url); } catch (\Exception $e) { $this->messageManager->addException($e, __('We cannot add this item to your shopping cart')); @@ -161,7 +165,7 @@ public function execute() * * @param string $backUrl * @param \Magento\Catalog\Model\Product $product - * @return $this|void + * @return $this|\Magento\Framework\Controller\Result\Redirect */ protected function goBack($backUrl = null, $product = null) { From 8a97365a734af19b9d894f537228c5d9ba190014 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 13 Feb 2015 12:38:05 +0200 Subject: [PATCH 51/57] MAGETWO-30801: Asynchronous products addition to cart - unit test fix --- .../Magento/Checkout/Controller/CartTest.php | 288 ++++++++++++++---- 1 file changed, 229 insertions(+), 59 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php index 47e0a0ef701b3..b6c3d15f04c5a 100644 --- a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php +++ b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php @@ -7,86 +7,256 @@ class CartTest extends \PHPUnit_Framework_TestCase { + + /** + * @var \Magento\Framework\Store\StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject + */ + private $storeManagerMock; + + /** + * @var \Magento\Framework\App\Request\Http | \PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; + + /** + * @var \Magento\Framework\App\Response\Http | \PHPUnit_Framework_MockObject_MockObject + */ + private $responseMock; + + /** + * @var \Magento\Framework\App\Response\RedirectInterface | \PHPUnit_Framework_MockObject_MockObject + */ + private $responseRedirectMock; + + /** + * @var \Magento\Framework\Controller\Result\Redirect | \PHPUnit_Framework_MockObject_MockObject + */ + private $resultRedirectMock; + + /** + * @var \Magento\Checkout\Model\Session | \PHPUnit_Framework_MockObject_MockObject + */ + private $checkoutModelSessionMock; + + /** + * @var \Magento\Framework\Controller\Result\RedirectFactory | \PHPUnit_Framework_MockObject_MockObject + */ + private $resultRedirectFactoryMock; + + /** + * @var \Magento\Store\Model\Store | \PHPUnit_Framework_MockObject_MockObject + */ + private $storeMock; + + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject + */ + private $scopeConfigMock; + + /** + * @var \Magento\Framework\UrlInterface | \PHPUnit_Framework_MockObject_MockObject + */ + private $contextUrlMock; + + /** + * @var \Magento\Checkout\Controller\Cart + */ + private $controller; + + protected function setUp() + { + $this->responseMock = $this->getMockBuilder('Magento\Framework\App\Response\Http') + ->disableOriginalConstructor() + ->getMock(); + + $this->requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') + ->disableOriginalConstructor() + ->getMock(); + + $this->responseRedirectMock = $this->getMockBuilder('Magento\Framework\App\Response\RedirectInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->resultRedirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect') + ->disableOriginalConstructor() + ->getMock(); + + $this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $this->checkoutModelSessionMock = $this->getMockBuilder('Magento\Checkout\Model\Session') + ->disableOriginalConstructor() + ->setMethods(['setContinueShoppingUrl']) + ->getMock(); + + $this->storeManagerMock = $this->getMockBuilder('Magento\Framework\Store\StoreManagerInterface') + ->getMock(); + + $this->storeMock = $this->getMockBuilder('Magento\Store\Model\Store') + ->disableOriginalConstructor() + ->getMock(); + + $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->contextUrlMock = $this->getMockBuilder('Magento\Framework\UrlInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject( + 'Magento\Checkout\Controller\Cart', + [ + 'response' => $this->responseMock, + 'request' => $this->requestMock, + 'checkoutSession' => $this->checkoutModelSessionMock, + 'scopeConfig' => $this->scopeConfigMock, + 'redirect' => $this->responseRedirectMock, + 'storeManager' => $this->storeManagerMock, + 'resultRedirectFactory' => $this->resultRedirectFactoryMock, + 'url' => $this->contextUrlMock + ] + ); + } + public function testControllerImplementsProductViewInterface() { - $this->assertInstanceOf( - 'Magento\Catalog\Controller\Product\View\ViewInterface', - $this->getMock('Magento\Checkout\Controller\Cart', [], [], '', false) + $this->assertInstanceOf('Magento\Catalog\Controller\Product\View\ViewInterface', $this->controller); + } + + public function testGoBackWithBackUrlInArgs() + { + $backUrl = 'test'; + + $this->resultRedirectMock->expects($this->once()) + ->method('setUrl') + ->with($backUrl) + ->willReturnSelf(); + + $this->resultRedirectFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->resultRedirectMock); + + $reflectionObject = new \ReflectionObject($this->controller); + $reflectionMethod = $reflectionObject->getMethod('_goBack'); + $reflectionMethod->setAccessible(true); + + $this->assertSame( + $this->resultRedirectMock, + $reflectionMethod->invokeArgs( + $this->controller, [$backUrl] + ) ); } - public function testGoBack() + public function testGoBackWithNoBackUrlAndShouldNotRedirectToCart() { - $helper = new \Magento\TestFramework\Helper\ObjectManager($this); - $storeManagerMock = $this->getMock('Magento\Framework\Store\StoreManagerInterface'); + $refererUrl = 'http://some-url/index.php/product.html'; - $responseMock = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false); - $responseMock->headersSentThrowsException = false; + $this->responseRedirectMock->expects($this->once()) + ->method('getRefererUrl') + ->willreturn($refererUrl); - $requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false); - $requestMock->expects($this->any())->method('getActionName')->will($this->returnValue('add')); - $requestMock->expects($this->at(0)) + $this->scopeConfigMock->expects($this->once()) + ->method('getValue') + ->with('checkout/cart/redirect_to_cart') + ->willreturn(0); + + $this->requestMock->expects($this->at(0)) ->method('getParam') ->with('return_url') - ->will($this->returnValue('http://malicious.com/')); - $requestMock->expects($this->any())->method('getParam')->will($this->returnValue(null)); - $redirect = $this->getMock('Magento\Framework\App\Response\RedirectInterface'); - $redirect->expects($this->any()) - ->method('getRefererUrl') - ->will($this->returnValue('http://some-url/index.php/product.html')); - - $checkoutSessionMock = $this->getMock( - 'Magento\Checkout\Model\Session', - ['setContinueShoppingUrl'], - [], - '', - false - ); - $checkoutSessionMock->expects($this->once()) - ->method('setContinueShoppingUrl') - ->with('http://some-url/index.php/product.html') - ->will($this->returnSelf()); + ->willreturn('http://malicious.com/'); + + $this->storeMock->expects($this->exactly(2)) + ->method('getBaseUrl') + ->willreturn('http://some-url/'); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->willreturn($this->storeMock); + + $this->resultRedirectFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($this->resultRedirectMock); + + $this->resultRedirectMock->expects($this->once()) + ->method('setUrl') + ->with($refererUrl) + ->willReturnSelf(); - $storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false); - $storeMock->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://some-url/')); + $this->resultRedirectFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->resultRedirectMock); + + $reflectionObject = new \ReflectionObject($this->controller); + $reflectionMethod = $reflectionObject->getMethod('_goBack'); + $reflectionMethod->setAccessible(true); + + $this->assertSame($this->resultRedirectMock, $reflectionMethod->invoke($this->controller)); + } - $configMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface'); - $configMock->expects($this->once()) + public function testGoBackWithNoBackUrlAndShouldRedirectToCart() + { + $refererUrl = 'http://some-url/index.php/product.html'; + + $this->responseRedirectMock->expects($this->exactly(2)) + ->method('getRefererUrl') + ->willreturn($refererUrl); + + $this->scopeConfigMock->expects($this->once()) ->method('getValue') ->with('checkout/cart/redirect_to_cart') - ->will($this->returnValue('1')); - $storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock)); + ->willreturn('1'); - $resultRedirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect') - ->disableOriginalConstructor() - ->getMock(); - $resultRedirectFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory') - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $resultRedirectFactoryMock->expects($this->any()) + $this->requestMock->expects($this->any()) + ->method('getActionName') + ->willreturn('add'); + + $this->requestMock->expects($this->at(0)) + ->method('getParam') + ->with('return_url') + ->willreturn('http://malicious.com/'); + + $this->requestMock->expects($this->at(1)) + ->method('getParam') + ->willreturn(false); + + $this->storeMock->expects($this->exactly(2)) + ->method('getBaseUrl') + ->willreturn('http://some-url/'); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->willreturn($this->storeMock); + + $this->checkoutModelSessionMock->expects($this->once()) + ->method('setContinueShoppingUrl') + ->with($refererUrl) + ->will($this->returnSelf()); + + $this->resultRedirectFactoryMock->expects($this->any()) ->method('create') - ->willReturn($resultRedirectMock); - $resultRedirectMock->expects($this->once()) - ->method('setPath') + ->willReturn($this->resultRedirectMock); + + $this->contextUrlMock->expects($this->once()) + ->method('getUrl') + ->with('checkout/cart')->willReturn('checkout/cart'); + + $this->resultRedirectMock->expects($this->once()) + ->method('setUrl') ->with('checkout/cart') ->willReturnSelf(); - $arguments = [ - 'response' => $responseMock, - 'request' => $requestMock, - 'checkoutSession' => $checkoutSessionMock, - 'scopeConfig' => $configMock, - 'redirect' => $redirect, - 'storeManager' => $storeManagerMock, - 'resultRedirectFactory' => $resultRedirectFactoryMock - ]; - - $controller = $helper->getObject('Magento\Checkout\Controller\Cart', $arguments); + $this->resultRedirectFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->resultRedirectMock); - $reflectionObject = new \ReflectionObject($controller); + $reflectionObject = new \ReflectionObject($this->controller); $reflectionMethod = $reflectionObject->getMethod('_goBack'); $reflectionMethod->setAccessible(true); - $this->assertSame($resultRedirectMock, $reflectionMethod->invoke($controller)); + + $this->assertSame($this->resultRedirectMock, $reflectionMethod->invoke($this->controller)); } } From 2de7e025789304cc63d6b5237cdcfac171cc1b50 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 13 Feb 2015 15:36:17 +0200 Subject: [PATCH 52/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/Checkout/Controller/Cart/Add.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/Magento/Checkout/Controller/Cart/Add.php b/app/code/Magento/Checkout/Controller/Cart/Add.php index 332cd19f7da40..086679d286625 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Add.php +++ b/app/code/Magento/Checkout/Controller/Cart/Add.php @@ -144,9 +144,7 @@ public function execute() $url = $this->_checkoutSession->getRedirectUrl(true); - if ($url) { - return $this->resultRedirectFactory->create()->setUrl($url); - } else { + if (!$url) { $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); $url = $this->_redirect->getRedirectUrl($cartUrl); } From 6842e971dcac0c63a622592435e58ae7c67de714 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 13 Feb 2015 16:19:11 +0200 Subject: [PATCH 53/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php index ebc22bb271cc8..a785dfe055e87 100644 --- a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php @@ -402,7 +402,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } } - return __('Please specify the quantity of product(s).'); + return (string)__('Please specify the quantity of product(s).'); } /** From bd595e3200600ec9c9239a7228f8af890e35fcd8 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Fri, 13 Feb 2015 19:27:01 +0200 Subject: [PATCH 54/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/Checkout/Controller/Cart.php | 2 +- .../unit/testsuite/Magento/Checkout/Controller/CartTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Checkout/Controller/Cart.php b/app/code/Magento/Checkout/Controller/Cart.php index 3b700707150d8..029bc7bfd56da 100644 --- a/app/code/Magento/Checkout/Controller/Cart.php +++ b/app/code/Magento/Checkout/Controller/Cart.php @@ -128,7 +128,7 @@ protected function getBackUrl($defaultUrl = null) $shouldRedirectToCart = $this->_scopeConfig->getValue( 'checkout/cart/redirect_to_cart', - \Magento\Framework\Store\ScopeInterface::SCOPE_STORE + \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) { diff --git a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php index b6c3d15f04c5a..f64c699e95650 100644 --- a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php +++ b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php @@ -9,7 +9,7 @@ class CartTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Store\StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Store\Model\StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject */ private $storeManagerMock; @@ -91,7 +91,7 @@ protected function setUp() ->setMethods(['setContinueShoppingUrl']) ->getMock(); - $this->storeManagerMock = $this->getMockBuilder('Magento\Framework\Store\StoreManagerInterface') + $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') ->getMock(); $this->storeMock = $this->getMockBuilder('Magento\Store\Model\Store') From 960a3a92cf67e62275c02c5cf9806170cf93d14d Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Mon, 16 Feb 2015 11:46:14 +0200 Subject: [PATCH 55/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/Catalog/Block/Product/AbstractProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php index f2498cb12393d..4747f181dccb2 100644 --- a/app/code/Magento/Catalog/Block/Product/AbstractProduct.php +++ b/app/code/Magento/Catalog/Block/Product/AbstractProduct.php @@ -595,7 +595,7 @@ public function isRedirectToCartEnabled() { return $this->_scopeConfig->getValue( 'checkout/cart/redirect_to_cart', - \Magento\Framework\Store\ScopeInterface::SCOPE_STORE + \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } } From cf131909dc96e1c8e966dda69e5188e807bab13c Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Mon, 16 Feb 2015 13:17:30 +0200 Subject: [PATCH 56/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php index d6860c0d89bbf..628bcd44aba97 100644 --- a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php @@ -402,7 +402,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } } - return (string)__('Please specify the quantity of product(s).'); + return __('Please specify the quantity of product(s).')->__toString(); } /** From e456fc6cb6ccdc33ccb4136f474a5d91bf3314ab Mon Sep 17 00:00:00 2001 From: Pavlo Cherniavskyi Date: Mon, 16 Feb 2015 14:43:23 +0200 Subject: [PATCH 57/57] MAGETWO-30801: Asynchronous products addition to cart - merge fix --- .../Model/Product/Type/Configurable.php | 4 +- .../Downloadable/Model/Product/Type.php | 2 +- .../Model/Product/Type/Grouped.php | 4 +- .../Magento/Checkout/Controller/CartTest.php | 262 ------------------ 4 files changed, 5 insertions(+), 267 deletions(-) delete mode 100644 dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php diff --git a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php index c618cd27f8b6d..9c79b10dc0ab2 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php @@ -774,7 +774,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } if (!isset($_result[0])) { - return __('Cannot add the item to shopping cart'); + return __('Cannot add the item to shopping cart')->render(); } /** @@ -809,7 +809,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } } - return (string)$this->getSpecifyOptionMessage(); + return $this->getSpecifyOptionMessage()->render(); } /** diff --git a/app/code/Magento/Downloadable/Model/Product/Type.php b/app/code/Magento/Downloadable/Model/Product/Type.php index 26390b3bf5d79..a7a9a7b3e62ef 100644 --- a/app/code/Magento/Downloadable/Model/Product/Type.php +++ b/app/code/Magento/Downloadable/Model/Product/Type.php @@ -485,7 +485,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ return $result; } if ($this->getLinkSelectionRequired($product) && $this->_isStrictProcessMode($processMode)) { - return __('Please specify product link(s).'); + return __('Please specify product link(s).')->render(); } return $result; } diff --git a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php index 628bcd44aba97..5037941f97ddb 100644 --- a/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php @@ -364,7 +364,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } if (!isset($_result[0])) { - return __('We cannot process the item.'); + return __('We cannot process the item.')->render(); } if ($isStrictProcessMode) { @@ -402,7 +402,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ } } - return __('Please specify the quantity of product(s).')->__toString(); + return __('Please specify the quantity of product(s).')->render(); } /** diff --git a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php b/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php deleted file mode 100644 index f64c699e95650..0000000000000 --- a/dev/tests/unit/testsuite/Magento/Checkout/Controller/CartTest.php +++ /dev/null @@ -1,262 +0,0 @@ -responseMock = $this->getMockBuilder('Magento\Framework\App\Response\Http') - ->disableOriginalConstructor() - ->getMock(); - - $this->requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') - ->disableOriginalConstructor() - ->getMock(); - - $this->responseRedirectMock = $this->getMockBuilder('Magento\Framework\App\Response\RedirectInterface') - ->disableOriginalConstructor() - ->getMock(); - - $this->resultRedirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect') - ->disableOriginalConstructor() - ->getMock(); - - $this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory') - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - - $this->checkoutModelSessionMock = $this->getMockBuilder('Magento\Checkout\Model\Session') - ->disableOriginalConstructor() - ->setMethods(['setContinueShoppingUrl']) - ->getMock(); - - $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') - ->getMock(); - - $this->storeMock = $this->getMockBuilder('Magento\Store\Model\Store') - ->disableOriginalConstructor() - ->getMock(); - - $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') - ->disableOriginalConstructor() - ->getMock(); - - $this->contextUrlMock = $this->getMockBuilder('Magento\Framework\UrlInterface') - ->disableOriginalConstructor() - ->getMock(); - - $this->controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject( - 'Magento\Checkout\Controller\Cart', - [ - 'response' => $this->responseMock, - 'request' => $this->requestMock, - 'checkoutSession' => $this->checkoutModelSessionMock, - 'scopeConfig' => $this->scopeConfigMock, - 'redirect' => $this->responseRedirectMock, - 'storeManager' => $this->storeManagerMock, - 'resultRedirectFactory' => $this->resultRedirectFactoryMock, - 'url' => $this->contextUrlMock - ] - ); - } - - public function testControllerImplementsProductViewInterface() - { - $this->assertInstanceOf('Magento\Catalog\Controller\Product\View\ViewInterface', $this->controller); - } - - public function testGoBackWithBackUrlInArgs() - { - $backUrl = 'test'; - - $this->resultRedirectMock->expects($this->once()) - ->method('setUrl') - ->with($backUrl) - ->willReturnSelf(); - - $this->resultRedirectFactoryMock->expects($this->once()) - ->method('create') - ->willReturn($this->resultRedirectMock); - - $reflectionObject = new \ReflectionObject($this->controller); - $reflectionMethod = $reflectionObject->getMethod('_goBack'); - $reflectionMethod->setAccessible(true); - - $this->assertSame( - $this->resultRedirectMock, - $reflectionMethod->invokeArgs( - $this->controller, [$backUrl] - ) - ); - } - - public function testGoBackWithNoBackUrlAndShouldNotRedirectToCart() - { - $refererUrl = 'http://some-url/index.php/product.html'; - - $this->responseRedirectMock->expects($this->once()) - ->method('getRefererUrl') - ->willreturn($refererUrl); - - $this->scopeConfigMock->expects($this->once()) - ->method('getValue') - ->with('checkout/cart/redirect_to_cart') - ->willreturn(0); - - $this->requestMock->expects($this->at(0)) - ->method('getParam') - ->with('return_url') - ->willreturn('http://malicious.com/'); - - $this->storeMock->expects($this->exactly(2)) - ->method('getBaseUrl') - ->willreturn('http://some-url/'); - - $this->storeManagerMock->expects($this->any()) - ->method('getStore') - ->willreturn($this->storeMock); - - $this->resultRedirectFactoryMock->expects($this->any()) - ->method('create') - ->willReturn($this->resultRedirectMock); - - $this->resultRedirectMock->expects($this->once()) - ->method('setUrl') - ->with($refererUrl) - ->willReturnSelf(); - - $this->resultRedirectFactoryMock->expects($this->once()) - ->method('create') - ->willReturn($this->resultRedirectMock); - - $reflectionObject = new \ReflectionObject($this->controller); - $reflectionMethod = $reflectionObject->getMethod('_goBack'); - $reflectionMethod->setAccessible(true); - - $this->assertSame($this->resultRedirectMock, $reflectionMethod->invoke($this->controller)); - } - - public function testGoBackWithNoBackUrlAndShouldRedirectToCart() - { - $refererUrl = 'http://some-url/index.php/product.html'; - - $this->responseRedirectMock->expects($this->exactly(2)) - ->method('getRefererUrl') - ->willreturn($refererUrl); - - $this->scopeConfigMock->expects($this->once()) - ->method('getValue') - ->with('checkout/cart/redirect_to_cart') - ->willreturn('1'); - - $this->requestMock->expects($this->any()) - ->method('getActionName') - ->willreturn('add'); - - $this->requestMock->expects($this->at(0)) - ->method('getParam') - ->with('return_url') - ->willreturn('http://malicious.com/'); - - $this->requestMock->expects($this->at(1)) - ->method('getParam') - ->willreturn(false); - - $this->storeMock->expects($this->exactly(2)) - ->method('getBaseUrl') - ->willreturn('http://some-url/'); - - $this->storeManagerMock->expects($this->any()) - ->method('getStore') - ->willreturn($this->storeMock); - - $this->checkoutModelSessionMock->expects($this->once()) - ->method('setContinueShoppingUrl') - ->with($refererUrl) - ->will($this->returnSelf()); - - $this->resultRedirectFactoryMock->expects($this->any()) - ->method('create') - ->willReturn($this->resultRedirectMock); - - $this->contextUrlMock->expects($this->once()) - ->method('getUrl') - ->with('checkout/cart')->willReturn('checkout/cart'); - - $this->resultRedirectMock->expects($this->once()) - ->method('setUrl') - ->with('checkout/cart') - ->willReturnSelf(); - - $this->resultRedirectFactoryMock->expects($this->once()) - ->method('create') - ->willReturn($this->resultRedirectMock); - - $reflectionObject = new \ReflectionObject($this->controller); - $reflectionMethod = $reflectionObject->getMethod('_goBack'); - $reflectionMethod->setAccessible(true); - - $this->assertSame($this->resultRedirectMock, $reflectionMethod->invoke($this->controller)); - } -}