123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\ObjectManager\Test\Unit\Config;
- use Magento\Framework\ObjectManager\Config\Compiled;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManager;
- class CompiledTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var ObjectManager
- */
- private $objectManager;
- /**
- * @var \Magento\Framework\ObjectManager\Config\Compiled
- */
- private $compiled;
- protected function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $initialData = [
- 'arguments' => [
- 'type1' => 'initial serialized configuration for type1',
- 'class_with_no_arguments_serialized' => null,
- 'class_with_arguments_string' => 'string arguments',
- 'class_with_arguments_array' => ['unserialized', 'arguments'],
- 'class_with_no_arguments_empty_array' => [],
- ],
- 'instanceTypes' => [
- 'instanceType1' => 'instanceTypeValue1',
- 'instanceType2' => 'instanceTypeValue2'
- ],
- 'preferences' => [
- 'preference1' => 'preferenceValue1',
- 'preference2' => 'preferenceValue2'
- ]
- ];
- $this->compiled = $this->objectManager->getObject(
- Compiled::class,
- [
- 'data' => $initialData,
- ]
- );
- }
- /**
- * Test is it possible extend/overwrite arguments for the DI.
- *
- */
- public function testExtendArguments()
- {
- $configuration = [
- 'arguments' => [
- 'type1' => 'configuration for type1',
- 'type2' => [
- 'argument2_1' => 'newArgumentValue2_1',
- ]
- ],
- 'instanceTypes' => [
- 'instanceType2' => 'newInstanceTypeValue2',
- 'instanceType3' => 'newInstanceTypeValue3',
- ],
- 'preferences' => [
- 'preference1' => 'newPreferenceValue1',
- ],
- ];
- $expectedArguments = [
- 'type1' => 'configuration for type1',
- 'type2' => [
- 'argument2_1' => 'newArgumentValue2_1',
- ]
- ];
- $this->compiled->extend($configuration);
- foreach ($expectedArguments as $type => $arguments) {
- $this->assertEquals($arguments, $this->compiled->getArguments($type));
- }
- }
- /**
- * Test getting virtual types from the DI.
- */
- public function testVirtualTypes()
- {
- $configuration = [
- 'instanceTypes' => [
- 'instanceType2' => 'newInstanceTypeValue2',
- 'instanceType3' => 'newInstanceTypeValue3'
- ],
- ];
- $expectedTypes = [
- 'instanceType1' => 'instanceTypeValue1',
- 'instanceType2' => 'newInstanceTypeValue2',
- 'instanceType3' => 'newInstanceTypeValue3'
- ];
- $this->compiled->extend($configuration);
- $this->assertEquals($expectedTypes, $this->compiled->getVirtualTypes());
- }
- /**
- * Test getting preferences from the DI.
- */
- public function testPreferences()
- {
- $configuration = [
- 'preferences' => [
- 'preference1' => 'newPreferenceValue1'
- ]
- ];
- $expectedPreferences = [
- 'preference1' => 'newPreferenceValue1',
- 'preference2' => 'preferenceValue2'
- ];
- $this->compiled->extend($configuration);
- $this->assertEquals($expectedPreferences, $this->compiled->getPreferences());
- }
- /**
- * Arguments defined in array, have not previously been unserialized
- */
- public function testGetArgumentsSerialized()
- {
- $unserializedArguments = 'string arguments';
- $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
- $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
- }
- /**
- * Arguments defined in array, have not previously been unserialized
- */
- public function testGetArgumentsSerializedEmpty()
- {
- $this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_serialized'));
- }
- /**
- * Arguments defined in array, have previously been unserialized
- */
- public function testGetArgumentsUnserialized()
- {
- $unserializedArguments = ['unserialized', 'arguments'];
- $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_array'));
- }
- /**
- * Arguments are defined but empty
- */
- public function testGetArgumentsUnserializedEmpty()
- {
- $this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_empty_array'));
- }
- /**
- * Arguments not defined in array
- */
- public function testGetArgumentsNotDefined()
- {
- $this->assertSame(null, $this->compiled->getArguments('class_not_stored_in_config'));
- }
- /**
- * Test that $arguments, $virtualTypes and $preferences initializing in construct must be array.
- *
- * @param $data
- * @param array $expectedResult
- *
- * @dataProvider constructorFieldsValidation
- */
- public function testConstructorFieldsValidation($data, $expectedResult)
- {
- /** @var Compiled $compiled */
- $compiled = $this->objectManager->getObject(
- Compiled::class,
- [
- 'data' => $data,
- ]
- );
- $reflection = new \ReflectionClass(Compiled::class);
- $arguments = $reflection->getProperty('arguments');
- $arguments->setAccessible(true);
- $this->assertEquals($expectedResult['arguments'], $arguments->getValue($compiled));
- $this->assertEquals($expectedResult['preferences'], $compiled->getPreferences());
- $this->assertEquals($expectedResult['instanceTypes'], $compiled->getVirtualTypes());
- }
- /**
- * Data provider for testConstructorFieldsValidation.
- *
- * @return array
- */
- public function constructorFieldsValidation()
- {
- return [
- [
- 'no array',
- [
- 'arguments' => [],
- 'instanceTypes' => [],
- 'preferences' => [],
- ],
- ],
- [
- [
- 'arguments' => 1,
- 'instanceTypes' => [1, 2, 3],
- 'preferences' => 'test',
- ],
- [
- 'arguments' => [],
- 'instanceTypes' => [1, 2, 3],
- 'preferences' => [],
- ],
- ],
- ];
- }
- /**
- * Test that $arguments, $virtualTypes and $preferences initializing in extend must be array.
- *
- * @param $data
- * @param array $expectedResult
- *
- * @dataProvider extendFieldsValidation
- */
- public function testExtendFieldsValidation($data, $expectedResult)
- {
- /** @var Compiled $compiled */
- $compiled = $this->objectManager->getObject(
- Compiled::class,
- [
- 'data' => $data,
- ]
- );
- $compiled->extend($data);
- $reflection = new \ReflectionClass(Compiled::class);
- $arguments = $reflection->getProperty('arguments');
- $arguments->setAccessible(true);
- $this->assertEquals($expectedResult['arguments'], $arguments->getValue($compiled));
- $this->assertEquals($expectedResult['preferences'], $compiled->getPreferences());
- $this->assertEquals($expectedResult['instanceTypes'], $compiled->getVirtualTypes());
- }
- /**
- * Data provider for testExtendFieldsValidation.
- *
- * @return array
- */
- public function extendFieldsValidation()
- {
- return [
- [
- [],
- [
- 'arguments' => [],
- 'instanceTypes' => [],
- 'preferences' => [],
- ],
- ],
- [
- [
- 'arguments' => 1,
- 'instanceTypes' => [1, 2, 3],
- 'preferences' => 'test',
- ],
- [
- 'arguments' => [],
- 'instanceTypes' => [1, 2, 3],
- 'preferences' => [],
- ],
- ],
- ];
- }
- }
|