ConfigTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Test\Unit\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. use \Magento\Framework\ObjectManager\Config\Config;
  9. class ConfigTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  12. private $objectManagerHelper;
  13. protected function setUp()
  14. {
  15. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  16. }
  17. public function testGetArgumentsEmpty()
  18. {
  19. $config = new Config();
  20. $this->assertSame([], $config->getArguments('An invalid type'));
  21. }
  22. public function testExtendMergeConfiguration()
  23. {
  24. $this->_assertFooTypeArguments(new Config());
  25. }
  26. /**
  27. * A primitive fixture for testing merging arguments
  28. *
  29. * @param Config $config
  30. */
  31. private function _assertFooTypeArguments(Config $config)
  32. {
  33. $expected = ['argName' => 'argValue'];
  34. $fixture = ['FooType' => ['arguments' => $expected]];
  35. $config->extend($fixture);
  36. $this->assertEquals($expected, $config->getArguments('FooType'));
  37. }
  38. public function testExtendWithCacheMock()
  39. {
  40. $definitions = $this->createMock(\Magento\Framework\ObjectManager\DefinitionInterface::class);
  41. $definitions->expects($this->once())->method('getClasses')->will($this->returnValue(['FooType']));
  42. $cache = $this->createMock(\Magento\Framework\ObjectManager\ConfigCacheInterface::class);
  43. $cache->expects($this->once())->method('get')->will($this->returnValue(false));
  44. $config = new Config(null, $definitions);
  45. $serializerMock = $this->createMock(SerializerInterface::class);
  46. $serializerMock->expects($this->exactly(2))
  47. ->method('serialize');
  48. $this->objectManagerHelper->setBackwardCompatibleProperty(
  49. $config,
  50. 'serializer',
  51. $serializerMock
  52. );
  53. $config->setCache($cache);
  54. $this->_assertFooTypeArguments($config);
  55. }
  56. public function testGetPreferenceTrimsFirstSlash()
  57. {
  58. $config = new Config();
  59. $this->assertEquals('Some\Class\Name', $config->getPreference('\Some\Class\Name'));
  60. }
  61. public function testExtendIgnoresFirstSlashesOnPreferences()
  62. {
  63. $config = new Config();
  64. $config->extend(['preferences' => ['\Some\Interface' => '\Some\Class']]);
  65. $this->assertEquals('Some\Class', $config->getPreference('Some\Interface'));
  66. $this->assertEquals('Some\Class', $config->getPreference('\Some\Interface'));
  67. }
  68. public function testExtendIgnoresFirstShashesOnVirtualTypes()
  69. {
  70. $config = new Config();
  71. $config->extend(['\SomeVirtualType' => ['type' => '\Some\Class']]);
  72. $this->assertEquals('Some\Class', $config->getInstanceType('SomeVirtualType'));
  73. }
  74. public function testExtendIgnoresFirstShashes()
  75. {
  76. $config = new Config();
  77. $config->extend(['\Some\Class' => ['arguments' => ['someArgument']]]);
  78. $this->assertEquals(['someArgument'], $config->getArguments('Some\Class'));
  79. }
  80. public function testExtendIgnoresFirstShashesForSharing()
  81. {
  82. $config = new Config();
  83. $config->extend(['\Some\Class' => ['shared' => true]]);
  84. $this->assertTrue($config->isShared('Some\Class'));
  85. }
  86. }