TMapTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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;
  7. use Magento\Framework\ObjectManager\TMap;
  8. use Magento\Framework\ObjectManagerInterface;
  9. require_once __DIR__ . '/_files/TMap/TClass.php';
  10. require_once __DIR__ . '/_files/TMap/TInterface.php';
  11. class TMapTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $om;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\ObjectManager\ConfigInterface
  19. */
  20. private $omConfig;
  21. protected function setUp()
  22. {
  23. $this->om = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  24. ->getMockForAbstractClass();
  25. $this->omConfig = $this->getMockBuilder(\Magento\Framework\ObjectManager\ConfigInterface::class)
  26. ->getMockForAbstractClass();
  27. }
  28. public function testConstructor()
  29. {
  30. $tMap = $this->getSimpleInitialized(3);
  31. static::assertEquals(3, $tMap->count());
  32. }
  33. public function testRead()
  34. {
  35. $tMap = $this->getSimpleInitialized(3);
  36. $this->om->expects(static::exactly(3))
  37. ->method('create')
  38. ->willReturnMap(
  39. [
  40. ['TClass', [], new \TClass()],
  41. ['TInterface', [], new \TClass()],
  42. ['TClassVirtual', [], new \TClass()]
  43. ]
  44. );
  45. foreach ($tMap as $instance) {
  46. static::assertInstanceOf('\TInterface', $instance);
  47. }
  48. }
  49. /**
  50. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  51. */
  52. public function testRemove()
  53. {
  54. $tMap = $this->getSimpleInitialized(3);
  55. static::assertEquals(3, $tMap->count());
  56. foreach ($tMap as $key => $instance) {
  57. unset($tMap[$key]);
  58. }
  59. static::assertEquals(0, $tMap->count());
  60. }
  61. /**
  62. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  63. */
  64. public function testEdit()
  65. {
  66. $expectedKeysOrder = [
  67. 'item',
  68. 4,
  69. 'item2',
  70. 5
  71. ];
  72. $tMap = $this->getSimpleInitialized(6);
  73. unset($tMap[0], $tMap[3]);
  74. $tMap[] = 'TClassVirtual';
  75. $tMap['item2'] = 'TClass';
  76. $tMap[] = 'TInterface';
  77. $this->om->expects(static::exactly(4))
  78. ->method('create')
  79. ->willReturnMap(
  80. [
  81. ['TClass', [], new \TClass()],
  82. ['TInterface', [], new \TClass()],
  83. ['TClassVirtual', [], new \TClass()]
  84. ]
  85. );
  86. $i = 0;
  87. foreach ($tMap as $key => $item) {
  88. static::assertEquals($expectedKeysOrder[$i], $key);
  89. $i++;
  90. }
  91. static::assertEquals(4, $tMap->count());
  92. }
  93. /**
  94. * Returns simple initialized tMap
  95. *
  96. * @param int $exactlyCalls
  97. * @return TMap
  98. */
  99. private function getSimpleInitialized($exactlyCalls = 3)
  100. {
  101. /**
  102. [
  103. 0 => ['TClass', 'TClass', 'TClass'],
  104. 'item' => ['TClassVirtual', 'TClassVirtual', 'TClass'],
  105. 3 => ['TInterface', 'TClassVirtual', 'TClass']
  106. ];
  107. */
  108. $testClasses = [
  109. 0 => 'TClass',
  110. 'item' => 'TClassVirtual',
  111. 3 => 'TInterface'
  112. ];
  113. $this->omConfig->expects(static::exactly($exactlyCalls))
  114. ->method('getPreference')
  115. ->willReturnMap(
  116. [
  117. ['TClass', 'TClass'],
  118. ['TClassVirtual', 'TClassVirtual'],
  119. ['TInterface', 'TClassVirtual']
  120. ]
  121. );
  122. $this->omConfig->expects(static::exactly($exactlyCalls))
  123. ->method('getInstanceType')
  124. ->willReturnMap(
  125. [
  126. ['TClass', 'TClass'],
  127. ['TClassVirtual', 'TClass']
  128. ]
  129. );
  130. return new TMap(
  131. 'TInterface',
  132. $this->om,
  133. $this->omConfig,
  134. $testClasses,
  135. function (ObjectManagerInterface $om, $objectName) {
  136. return $om->create($objectName);
  137. }
  138. );
  139. }
  140. }