CompiledTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\ObjectManager\Config\Compiled;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManager;
  9. class CompiledTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var ObjectManager
  13. */
  14. private $objectManager;
  15. /**
  16. * @var \Magento\Framework\ObjectManager\Config\Compiled
  17. */
  18. private $compiled;
  19. protected function setUp()
  20. {
  21. $this->objectManager = new ObjectManager($this);
  22. $initialData = [
  23. 'arguments' => [
  24. 'type1' => 'initial serialized configuration for type1',
  25. 'class_with_no_arguments_serialized' => null,
  26. 'class_with_arguments_string' => 'string arguments',
  27. 'class_with_arguments_array' => ['unserialized', 'arguments'],
  28. 'class_with_no_arguments_empty_array' => [],
  29. ],
  30. 'instanceTypes' => [
  31. 'instanceType1' => 'instanceTypeValue1',
  32. 'instanceType2' => 'instanceTypeValue2'
  33. ],
  34. 'preferences' => [
  35. 'preference1' => 'preferenceValue1',
  36. 'preference2' => 'preferenceValue2'
  37. ]
  38. ];
  39. $this->compiled = $this->objectManager->getObject(
  40. Compiled::class,
  41. [
  42. 'data' => $initialData,
  43. ]
  44. );
  45. }
  46. /**
  47. * Test is it possible extend/overwrite arguments for the DI.
  48. *
  49. */
  50. public function testExtendArguments()
  51. {
  52. $configuration = [
  53. 'arguments' => [
  54. 'type1' => 'configuration for type1',
  55. 'type2' => [
  56. 'argument2_1' => 'newArgumentValue2_1',
  57. ]
  58. ],
  59. 'instanceTypes' => [
  60. 'instanceType2' => 'newInstanceTypeValue2',
  61. 'instanceType3' => 'newInstanceTypeValue3',
  62. ],
  63. 'preferences' => [
  64. 'preference1' => 'newPreferenceValue1',
  65. ],
  66. ];
  67. $expectedArguments = [
  68. 'type1' => 'configuration for type1',
  69. 'type2' => [
  70. 'argument2_1' => 'newArgumentValue2_1',
  71. ]
  72. ];
  73. $this->compiled->extend($configuration);
  74. foreach ($expectedArguments as $type => $arguments) {
  75. $this->assertEquals($arguments, $this->compiled->getArguments($type));
  76. }
  77. }
  78. /**
  79. * Test getting virtual types from the DI.
  80. */
  81. public function testVirtualTypes()
  82. {
  83. $configuration = [
  84. 'instanceTypes' => [
  85. 'instanceType2' => 'newInstanceTypeValue2',
  86. 'instanceType3' => 'newInstanceTypeValue3'
  87. ],
  88. ];
  89. $expectedTypes = [
  90. 'instanceType1' => 'instanceTypeValue1',
  91. 'instanceType2' => 'newInstanceTypeValue2',
  92. 'instanceType3' => 'newInstanceTypeValue3'
  93. ];
  94. $this->compiled->extend($configuration);
  95. $this->assertEquals($expectedTypes, $this->compiled->getVirtualTypes());
  96. }
  97. /**
  98. * Test getting preferences from the DI.
  99. */
  100. public function testPreferences()
  101. {
  102. $configuration = [
  103. 'preferences' => [
  104. 'preference1' => 'newPreferenceValue1'
  105. ]
  106. ];
  107. $expectedPreferences = [
  108. 'preference1' => 'newPreferenceValue1',
  109. 'preference2' => 'preferenceValue2'
  110. ];
  111. $this->compiled->extend($configuration);
  112. $this->assertEquals($expectedPreferences, $this->compiled->getPreferences());
  113. }
  114. /**
  115. * Arguments defined in array, have not previously been unserialized
  116. */
  117. public function testGetArgumentsSerialized()
  118. {
  119. $unserializedArguments = 'string arguments';
  120. $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
  121. $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
  122. }
  123. /**
  124. * Arguments defined in array, have not previously been unserialized
  125. */
  126. public function testGetArgumentsSerializedEmpty()
  127. {
  128. $this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_serialized'));
  129. }
  130. /**
  131. * Arguments defined in array, have previously been unserialized
  132. */
  133. public function testGetArgumentsUnserialized()
  134. {
  135. $unserializedArguments = ['unserialized', 'arguments'];
  136. $this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_array'));
  137. }
  138. /**
  139. * Arguments are defined but empty
  140. */
  141. public function testGetArgumentsUnserializedEmpty()
  142. {
  143. $this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_empty_array'));
  144. }
  145. /**
  146. * Arguments not defined in array
  147. */
  148. public function testGetArgumentsNotDefined()
  149. {
  150. $this->assertSame(null, $this->compiled->getArguments('class_not_stored_in_config'));
  151. }
  152. /**
  153. * Test that $arguments, $virtualTypes and $preferences initializing in construct must be array.
  154. *
  155. * @param $data
  156. * @param array $expectedResult
  157. *
  158. * @dataProvider constructorFieldsValidation
  159. */
  160. public function testConstructorFieldsValidation($data, $expectedResult)
  161. {
  162. /** @var Compiled $compiled */
  163. $compiled = $this->objectManager->getObject(
  164. Compiled::class,
  165. [
  166. 'data' => $data,
  167. ]
  168. );
  169. $reflection = new \ReflectionClass(Compiled::class);
  170. $arguments = $reflection->getProperty('arguments');
  171. $arguments->setAccessible(true);
  172. $this->assertEquals($expectedResult['arguments'], $arguments->getValue($compiled));
  173. $this->assertEquals($expectedResult['preferences'], $compiled->getPreferences());
  174. $this->assertEquals($expectedResult['instanceTypes'], $compiled->getVirtualTypes());
  175. }
  176. /**
  177. * Data provider for testConstructorFieldsValidation.
  178. *
  179. * @return array
  180. */
  181. public function constructorFieldsValidation()
  182. {
  183. return [
  184. [
  185. 'no array',
  186. [
  187. 'arguments' => [],
  188. 'instanceTypes' => [],
  189. 'preferences' => [],
  190. ],
  191. ],
  192. [
  193. [
  194. 'arguments' => 1,
  195. 'instanceTypes' => [1, 2, 3],
  196. 'preferences' => 'test',
  197. ],
  198. [
  199. 'arguments' => [],
  200. 'instanceTypes' => [1, 2, 3],
  201. 'preferences' => [],
  202. ],
  203. ],
  204. ];
  205. }
  206. /**
  207. * Test that $arguments, $virtualTypes and $preferences initializing in extend must be array.
  208. *
  209. * @param $data
  210. * @param array $expectedResult
  211. *
  212. * @dataProvider extendFieldsValidation
  213. */
  214. public function testExtendFieldsValidation($data, $expectedResult)
  215. {
  216. /** @var Compiled $compiled */
  217. $compiled = $this->objectManager->getObject(
  218. Compiled::class,
  219. [
  220. 'data' => $data,
  221. ]
  222. );
  223. $compiled->extend($data);
  224. $reflection = new \ReflectionClass(Compiled::class);
  225. $arguments = $reflection->getProperty('arguments');
  226. $arguments->setAccessible(true);
  227. $this->assertEquals($expectedResult['arguments'], $arguments->getValue($compiled));
  228. $this->assertEquals($expectedResult['preferences'], $compiled->getPreferences());
  229. $this->assertEquals($expectedResult['instanceTypes'], $compiled->getVirtualTypes());
  230. }
  231. /**
  232. * Data provider for testExtendFieldsValidation.
  233. *
  234. * @return array
  235. */
  236. public function extendFieldsValidation()
  237. {
  238. return [
  239. [
  240. [],
  241. [
  242. 'arguments' => [],
  243. 'instanceTypes' => [],
  244. 'preferences' => [],
  245. ],
  246. ],
  247. [
  248. [
  249. 'arguments' => 1,
  250. 'instanceTypes' => [1, 2, 3],
  251. 'preferences' => 'test',
  252. ],
  253. [
  254. 'arguments' => [],
  255. 'instanceTypes' => [1, 2, 3],
  256. 'preferences' => [],
  257. ],
  258. ],
  259. ];
  260. }
  261. }