diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php b/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php index b57f4665aff21..bd1f9891fdd9d 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php @@ -3,21 +3,26 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\ObjectManager\Factory; use Magento\Framework\Exception\RuntimeException; +use Magento\Framework\ObjectManager\ConfigInterface; +use Magento\Framework\ObjectManager\Definition\Runtime; +use Magento\Framework\ObjectManager\DefinitionInterface; +use Magento\Framework\ObjectManager\FactoryInterface; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Phrase; -use Psr\Log\LoggerInterface; use Magento\Framework\App\ObjectManager; +use Psr\Log\LoggerInterface; /** * Class AbstractFactory */ -abstract class AbstractFactory implements \Magento\Framework\ObjectManager\FactoryInterface +abstract class AbstractFactory implements FactoryInterface { /** - * Object manager + * Object manager instance * * @var ObjectManagerInterface */ @@ -26,19 +31,19 @@ abstract class AbstractFactory implements \Magento\Framework\ObjectManager\Facto /** * Object manager config * - * @var \Magento\Framework\ObjectManager\ConfigInterface + * @var ConfigInterface */ protected $config; /** * Definition list * - * @var \Magento\Framework\ObjectManager\DefinitionInterface + * @var DefinitionInterface */ protected $definitions; /** - * Global arguments + * Global arguments list * * @var array */ @@ -52,15 +57,15 @@ abstract class AbstractFactory implements \Magento\Framework\ObjectManager\Facto protected $creationStack = []; /** - * @param \Magento\Framework\ObjectManager\ConfigInterface $config - * @param ObjectManagerInterface $objectManager - * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions - * @param array $globalArguments + * @param ConfigInterface $config + * @param ObjectManagerInterface $objectManager + * @param DefinitionInterface $definitions + * @param array $globalArguments */ public function __construct( - \Magento\Framework\ObjectManager\ConfigInterface $config, + ConfigInterface $config, ObjectManagerInterface $objectManager = null, - \Magento\Framework\ObjectManager\DefinitionInterface $definitions = null, + DefinitionInterface $definitions = null, $globalArguments = [] ) { $this->config = $config; @@ -96,12 +101,12 @@ public function setArguments($arguments) /** * Get definitions * - * @return \Magento\Framework\ObjectManager\DefinitionInterface + * @return DefinitionInterface */ public function getDefinitions() { if ($this->definitions === null) { - $this->definitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); + $this->definitions = new Runtime(); } return $this->definitions; } @@ -110,7 +115,7 @@ public function getDefinitions() * Create object * * @param string $type - * @param array $args + * @param array $args * * @return object * @throws RuntimeException @@ -118,7 +123,7 @@ public function getDefinitions() protected function createObject($type, $args) { try { - return new $type(...array_values($args)); + return new $type(...$args); } catch (\TypeError $exception) { /** * @var LoggerInterface $logger @@ -140,9 +145,9 @@ protected function createObject($type, $args) /** * Resolve an argument * - * @param array $argument + * @param array $argument * @param string $paramType - * @param mixed $paramDefault + * @param mixed $paramDefault * @param string $paramName * @param string $requestedType * @@ -224,8 +229,8 @@ protected function parseArray(&$array) * Resolve constructor arguments * * @param string $requestedType - * @param array $parameters - * @param array $arguments + * @param array $parameters + * @param array $arguments * * @return array * @@ -245,16 +250,16 @@ protected function resolveArgumentsInRuntime($requestedType, array $parameters, /** * Get resolved argument from parameter * - * @param string $requestedType - * @param array $parameter - * @param array $arguments + * @param string $requestedType + * @param array $parameter + * @param array $arguments * @return array */ private function getResolvedArgument(string $requestedType, array $parameter, array $arguments): array { list($paramName, $paramType, $paramRequired, $paramDefault, $isVariadic) = $parameter; $argument = null; - if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) { + if (!empty($arguments) && (array_key_exists($paramName, $arguments))) { $argument = $arguments[$paramName]; } elseif ($paramRequired) { if ($paramType) { diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index b219d93b0f0fa..3d25c0aae1e68 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -15,7 +15,7 @@ class Compiled extends AbstractFactory protected $config; /** - * Global arguments + * Global arguments list * * @var array */ @@ -94,7 +94,6 @@ public function create($requestedType, array $arguments = []) } } } - $args = array_values($args); } else { // Case 3: arguments retrieved in runtime $parameters = $this->getDefinitions()->getParameters($type) ?: []; diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php index 26e870541b999..7aefbffa4713c 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php @@ -5,7 +5,9 @@ */ namespace Magento\Framework\ObjectManager\Factory\Dynamic; -class Developer extends \Magento\Framework\ObjectManager\Factory\AbstractFactory +use Magento\Framework\ObjectManager\Factory\AbstractFactory; + +class Developer extends AbstractFactory { /** * Resolve constructor arguments diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Production.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Production.php index 72f44f489dfbf..b5d00ce113db3 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Production.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Production.php @@ -5,7 +5,9 @@ */ namespace Magento\Framework\ObjectManager\Factory\Dynamic; -class Production extends \Magento\Framework\ObjectManager\Factory\AbstractFactory +use Magento\Framework\ObjectManager\Factory\AbstractFactory; + +class Production extends AbstractFactory { /** * Resolve constructor arguments @@ -28,7 +30,7 @@ protected function _resolveArguments($requestedType, array $parameters, array $a foreach ($parameters as $parameter) { list($paramName, $paramType, $paramRequired, $paramDefault) = $parameter; $argument = null; - if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) { + if (!empty($arguments) && (array_key_exists($paramName, $arguments))) { $argument = $arguments[$paramName]; } elseif ($paramRequired) { $argument = ['instance' => $paramType]; diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php index 4f6b161b05619..715c22a9c1c3c 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/CompiledTest.php @@ -28,16 +28,16 @@ class CompiledTest extends TestCase { /** @var ObjectManagerInterface|MockObject */ - protected $objectManagerMock; + private $objectManagerMock; /** @var ConfigInterface|MockObject */ - protected $config; + private $config; /** @var DefinitionInterface|MockObject */ private $definitionsMock; /** @var Compiled */ - protected $factory; + private $factory; /** @var array */ private $sharedInstances; @@ -76,7 +76,7 @@ protected function setUp(): void /** * Test create simple */ - public function testCreateSimple() + public function testCreateSimple(): void { $expectedConfig = $this->getSimpleConfig(); @@ -85,7 +85,7 @@ public function testCreateSimple() $sharedType = DependencySharedTesting::class; $nonSharedType = DependencyTesting::class; - $this->config->expects($this->any()) + $this->config ->method('getArguments') ->willReturnMap( [ @@ -94,7 +94,7 @@ public function testCreateSimple() [$nonSharedType, null] ] ); - $this->config->expects($this->any()) + $this->config ->method('getInstanceType') ->willReturnMap( [ @@ -125,6 +125,50 @@ public function testCreateSimple() $this->assertNull($result->getNullValue()); } + /** + * Test create invalid simple + */ + public function testCreateInvalidSimple(): void + { + $expectedConfig = $this->getInvalidSimpleConfig(); + + $requestedType = 'requestedType'; + $type = SimpleClassTesting::class; + $sharedType = DependencySharedTesting::class; + $nonSharedType = DependencyTesting::class; + + $this->config + ->method('getArguments') + ->willReturnMap( + [ + [$requestedType, $expectedConfig], + [$sharedType, null], + [$nonSharedType, null] + ] + ); + $this->config + ->method('getInstanceType') + ->willReturnMap( + [ + [$requestedType, $type], + [$sharedType, $sharedType], + [$nonSharedType, $nonSharedType] + ] + ); + + $this->factory->setArguments( + [ + 'globalValue' => 'GLOBAL_ARGUMENT', + ] + ); + + $this->expectException(\Error::class); + $this->expectExceptionMessage('Unknown named parameter $value_array'); + + /** @var SimpleClassTesting $result */ + $this->factory->create($requestedType, []); + } + /** * Create class with exception * @@ -149,7 +193,7 @@ public function testCreateSimpleWithException(): void /** * Test create simple configured arguments */ - public function testCreateSimpleConfiguredArguments() + public function testCreateSimpleConfiguredArguments(): void { $expectedConfig = $this->getSimpleNestedConfig(); @@ -159,7 +203,7 @@ public function testCreateSimpleConfiguredArguments() DependencySharedTesting::class; $nonSharedType = DependencyTesting::class; - $this->config->expects($this->any()) + $this->config ->method('getArguments') ->willReturnMap( [ @@ -168,7 +212,7 @@ public function testCreateSimpleConfiguredArguments() [$nonSharedType, null] ] ); - $this->config->expects($this->any()) + $this->config ->method('getInstanceType') ->willReturnMap( [ @@ -216,11 +260,11 @@ public function testCreateSimpleConfiguredArguments() /** * Test create get arguments in runtime */ - public function testCreateGetArgumentsInRuntime() + public function testCreateGetArgumentsInRuntime(): void { // Stub OM to create test assets - $this->config->expects($this->any())->method('isShared')->willReturn(true); - $this->objectManagerMock->expects($this->any())->method('get')->willReturnMap( + $this->config->method('isShared')->willReturn(true); + $this->objectManagerMock->method('get')->willReturnMap( [ [DependencyTesting::class, new DependencyTesting()], [DependencySharedTesting::class, new DependencySharedTesting()] @@ -229,8 +273,8 @@ public function testCreateGetArgumentsInRuntime() // Simulate case where compiled DI config not found $type = SimpleClassTesting::class; - $this->config->expects($this->any())->method('getArguments')->willReturn(null); - $this->config->expects($this->any())->method('getInstanceType')->willReturnArgument(0); + $this->config->method('getArguments')->willReturn(null); + $this->config->method('getInstanceType')->willReturnArgument(0); $this->definitionsMock->expects($this->once()) ->method('getParameters') ->with($type) @@ -258,7 +302,7 @@ public function testCreateGetArgumentsInRuntime() * * @return array */ - private function getSimpleConfig() + private function getSimpleConfig(): array { return [ 'nonSharedDependency' => [ @@ -270,9 +314,6 @@ private function getSimpleConfig() 'value' => [ '_v_' => 'value', ], - 'value_array' => [ - '_v_' => ['default_value1', 'default_value2'], - ], 'globalValue' => [ '_a_' => 'globalValue', '_d_' => null @@ -289,7 +330,24 @@ private function getSimpleConfig() * * @return array */ - private function getSimpleNestedConfig() + private function getInvalidSimpleConfig(): array + { + $config = $this->getSimpleConfig(); + //Add not existing parameter + $config['value_array'] = [ + '_v_' => ['default_value1', 'default_value2'], + ]; + + return $config; + } + + /** + * Returns config for \Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\Compiled\SimpleClassTesting + * with non-default nested array value for the $value_array parameter + * + * @return array + */ + private function getSimpleNestedConfig(): array { return [ 'nonSharedDependency' => [ @@ -301,7 +359,7 @@ private function getSimpleNestedConfig() 'value' => [ '_v_' => 'value', ], - 'value_array' => [ + 'valueArray' => [ '_vac_' => [ 'array_value' => 'value', 'array_configured_instance' => [ @@ -344,7 +402,7 @@ private function getSimpleNestedConfig() * * @return array */ - private function getRuntimeParameters() + private function getRuntimeParameters(): array { return [ 0 => [