ObjectManagerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager;
  7. class ObjectManagerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**#@+
  10. * Test class with type error
  11. */
  12. const TEST_CLASS_WITH_TYPE_ERROR = \Magento\Framework\ObjectManager\TestAsset\ConstructorWithTypeError::class;
  13. /**#@+
  14. * Test classes for basic instantiation
  15. */
  16. const TEST_CLASS = \Magento\Framework\ObjectManager\TestAsset\Basic::class;
  17. const TEST_CLASS_INJECTION = \Magento\Framework\ObjectManager\TestAsset\BasicInjection::class;
  18. /**#@-*/
  19. /**#@+
  20. * Test classes and interface to test preferences
  21. */
  22. const TEST_INTERFACE = \Magento\Framework\ObjectManager\TestAsset\TestAssetInterface::class;
  23. const TEST_INTERFACE_IMPLEMENTATION = \Magento\Framework\ObjectManager\TestAsset\InterfaceImplementation::class;
  24. const TEST_CLASS_WITH_INTERFACE = \Magento\Framework\ObjectManager\TestAsset\InterfaceInjection::class;
  25. /**#@-*/
  26. /**
  27. * @var \Magento\Framework\ObjectManagerInterface
  28. */
  29. protected static $_objectManager;
  30. /**
  31. * List of classes with different number of arguments
  32. *
  33. * @var array
  34. */
  35. protected $_numerableClasses = [
  36. 0 => \Magento\Framework\ObjectManager\TestAsset\ConstructorNoArguments::class,
  37. 1 => \Magento\Framework\ObjectManager\TestAsset\ConstructorOneArgument::class,
  38. 2 => \Magento\Framework\ObjectManager\TestAsset\ConstructorTwoArguments::class,
  39. 3 => \Magento\Framework\ObjectManager\TestAsset\ConstructorThreeArguments::class,
  40. 4 => \Magento\Framework\ObjectManager\TestAsset\ConstructorFourArguments::class,
  41. 5 => \Magento\Framework\ObjectManager\TestAsset\ConstructorFiveArguments::class,
  42. 6 => \Magento\Framework\ObjectManager\TestAsset\ConstructorSixArguments::class,
  43. 7 => \Magento\Framework\ObjectManager\TestAsset\ConstructorSevenArguments::class,
  44. 8 => \Magento\Framework\ObjectManager\TestAsset\ConstructorEightArguments::class,
  45. 9 => \Magento\Framework\ObjectManager\TestAsset\ConstructorNineArguments::class,
  46. 10 => \Magento\Framework\ObjectManager\TestAsset\ConstructorTenArguments::class,
  47. ];
  48. /**
  49. * Names of properties
  50. *
  51. * @var array
  52. */
  53. protected $_numerableProperties = [
  54. 1 => '_one',
  55. 2 => '_two',
  56. 3 => '_three',
  57. 4 => '_four',
  58. 5 => '_five',
  59. 6 => '_six',
  60. 7 => '_seven',
  61. 8 => '_eight',
  62. 9 => '_nine',
  63. 10 => '_ten',
  64. ];
  65. public static function setUpBeforeClass()
  66. {
  67. $config = new \Magento\Framework\ObjectManager\Config\Config();
  68. $factory = new Factory\Dynamic\Developer($config);
  69. self::$_objectManager = new \Magento\Framework\ObjectManager\ObjectManager($factory, $config);
  70. self::$_objectManager->configure(
  71. ['preferences' => [self::TEST_INTERFACE => self::TEST_INTERFACE_IMPLEMENTATION]]
  72. );
  73. $factory->setObjectManager(self::$_objectManager);
  74. }
  75. public static function tearDownAfterClass()
  76. {
  77. self::$_objectManager = null;
  78. }
  79. /**
  80. * Data provider for testNewInstance
  81. *
  82. * @return array
  83. */
  84. public function newInstanceDataProvider()
  85. {
  86. $data = [
  87. 'basic model' => [
  88. '$actualClassName' => self::TEST_CLASS_INJECTION,
  89. '$properties' => ['_object' => self::TEST_CLASS],
  90. ],
  91. 'model with interface' => [
  92. '$actualClassName' => self::TEST_CLASS_WITH_INTERFACE,
  93. '$properties' => ['_object' => self::TEST_INTERFACE_IMPLEMENTATION],
  94. ],
  95. ];
  96. foreach ($this->_numerableClasses as $number => $className) {
  97. $properties = [];
  98. for ($i = 1; $i <= $number; $i++) {
  99. $propertyName = $this->_numerableProperties[$i];
  100. $properties[$propertyName] = self::TEST_CLASS;
  101. }
  102. $data[$number . ' arguments'] = ['$actualClassName' => $className, '$properties' => $properties];
  103. }
  104. return $data;
  105. }
  106. /**
  107. * @param string $actualClassName
  108. * @param array $properties
  109. * @param string|null $expectedClassName
  110. *
  111. * @dataProvider newInstanceDataProvider
  112. */
  113. public function testNewInstance($actualClassName, array $properties = [], $expectedClassName = null)
  114. {
  115. if (!$expectedClassName) {
  116. $expectedClassName = $actualClassName;
  117. }
  118. $testObject = self::$_objectManager->create($actualClassName);
  119. $this->assertInstanceOf($expectedClassName, $testObject);
  120. if ($properties) {
  121. foreach ($properties as $propertyName => $propertyClass) {
  122. $this->assertAttributeInstanceOf($propertyClass, $propertyName, $testObject);
  123. }
  124. }
  125. }
  126. /**
  127. * Test creating an object and passing incorrect type of arguments to the constructor.
  128. *
  129. * @expectedException \Magento\Framework\Exception\RuntimeException
  130. * @expectedExceptionMessage Error occurred when creating object
  131. */
  132. public function testNewInstanceWithTypeError()
  133. {
  134. self::$_objectManager->create(self::TEST_CLASS_WITH_TYPE_ERROR, [
  135. 'testArgument' => new \stdClass()
  136. ]);
  137. }
  138. }