AttributeTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\Model\Adapter\Container;
  7. use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * Unit test for Magento\Elasticsearch\Model\Adapter\Container\Attribute
  11. */
  12. class AttributeTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Elasticsearch\Model\Adapter\Container\Attribute
  16. */
  17. private $attribute;
  18. /**
  19. * @var Collection|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $collectionMock;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp()
  26. {
  27. $this->collectionMock = $this->getMockBuilder(Collection::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $objectManager = new ObjectManagerHelper($this);
  31. $this->attribute = $objectManager->getObject(
  32. \Magento\Elasticsearch\Model\Adapter\Container\Attribute::class,
  33. [
  34. 'attributeCollection' => $this->collectionMock,
  35. ]
  36. );
  37. }
  38. /**
  39. * @return void
  40. */
  41. public function testGetAttributeCodeById()
  42. {
  43. $attributeId = 555;
  44. $attributeCode = 'test_attr_code1';
  45. $expected = 'test_attr_code1';
  46. $this->mockAttributeById($attributeId, $attributeCode);
  47. $result = $this->attribute->getAttributeCodeById($attributeId);
  48. $this->assertEquals($expected, $result);
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function testGetOptionsAttributeCodeById()
  54. {
  55. $attributeId = 'options';
  56. $expected = 'options';
  57. $result = $this->attribute->getAttributeCodeById($attributeId);
  58. $this->assertEquals($expected, $result);
  59. }
  60. /**
  61. * @return void
  62. */
  63. public function testGetAttributeIdByCode()
  64. {
  65. $attributeId = 100;
  66. $attributeCode = 'test_attribute_code';
  67. $this->mockAttributeByCode($attributeId, $attributeCode);
  68. $result = $this->attribute->getAttributeIdByCode($attributeCode);
  69. $this->assertEquals($attributeId, $result);
  70. }
  71. /**
  72. * Test getAttributeIdByCode() method.
  73. */
  74. public function testGetOptionsAttributeIdByCode()
  75. {
  76. $attributeCode = 'options';
  77. $expected = 'options';
  78. $result = $this->attribute->getAttributeIdByCode($attributeCode);
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * @return void
  83. */
  84. public function testGetMultipleAttributeIdsByCode()
  85. {
  86. $firstAttributeId = 100;
  87. $firstAttributeCode = 'test_attribute_code_100';
  88. $this->mockAttributeByCode($firstAttributeId, $firstAttributeCode, 0);
  89. $this->assertEquals($firstAttributeId, $this->attribute->getAttributeIdByCode($firstAttributeCode));
  90. $secondAttributeId = 200;
  91. $secondAttributeCode = 'test_attribute_code_200';
  92. $this->mockAttributeByCode($secondAttributeId, $secondAttributeCode, 0);
  93. $this->assertEquals($secondAttributeId, $this->attribute->getAttributeIdByCode($secondAttributeCode));
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function testGetAttributeByIdTwice()
  99. {
  100. $attributeId = 555;
  101. $attributeCode = 'test_attr_code2';
  102. $expected = 'test_attr_code2';
  103. $this->mockAttributeById($attributeId, $attributeCode, 0);
  104. $this->assertEquals($expected, $this->attribute->getAttributeCodeById($attributeId));
  105. $this->assertEquals($expected, $this->attribute->getAttributeCodeById($attributeId));
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testGetAttributeByIdCachedInGetAttributeByCode()
  111. {
  112. $attributeId = 100;
  113. $attributeCode = 'test_attribute_code';
  114. $this->mockAttributeByCode($attributeId, $attributeCode);
  115. $this->assertEquals($attributeId, $this->attribute->getAttributeIdByCode($attributeCode));
  116. $this->assertEquals($attributeCode, $this->attribute->getAttributeCodeById($attributeId));
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function testGetAttribute()
  122. {
  123. $attributeCode = 'attr_code_120';
  124. $attribute = $this->createAttributeMock(120, $attributeCode);
  125. $attributes = [
  126. $attribute
  127. ];
  128. $this->mockAttributes($attributes);
  129. $this->assertEquals($attribute, $this->attribute->getAttribute($attributeCode));
  130. }
  131. /**
  132. * @return void
  133. */
  134. public function testGetUnknownAttribute()
  135. {
  136. $attributeCode = 'attr_code_120';
  137. $attributes = [
  138. $this->createAttributeMock(120, 'attribute_code')
  139. ];
  140. $this->mockAttributes($attributes);
  141. $this->assertEquals(null, $this->attribute->getAttribute($attributeCode));
  142. }
  143. /**
  144. * @return void
  145. */
  146. public function testGetAttributes()
  147. {
  148. $attributes = [
  149. 'attr_1_mock' => $this->createAttributeMock(1, 'attr_1_mock'),
  150. 'attr_20_mock' => $this->createAttributeMock(20, 'attr_20_mock'),
  151. 'attr_25_mock' => $this->createAttributeMock(25, 'attr_25_mock'),
  152. 'attr_40_mock' => $this->createAttributeMock(40, 'attr_40_mock'),
  153. 'attr_73_mock' => $this->createAttributeMock(73, 'attr_73_mock'),
  154. 'attr_52_mock' => $this->createAttributeMock(52, 'attr_52_mock'),
  155. 'attr_97_mock' => $this->createAttributeMock(97, 'attr_97_mock'),
  156. ];
  157. $this->mockAttributes($attributes);
  158. $this->assertEquals($attributes, $this->attribute->getAttributes());
  159. }
  160. /**
  161. * @param array $attributes
  162. * @return void
  163. */
  164. private function mockAttributes(array $attributes)
  165. {
  166. $this->collectionMock->expects($this->once())
  167. ->method('getIterator')
  168. ->willReturn(new \ArrayIterator($attributes));
  169. }
  170. /**
  171. * @param int $attributeId
  172. * @param string $attributeCode
  173. * @param int $sequence
  174. * @return \PHPUnit_Framework_MockObject_MockObject
  175. */
  176. private function mockAttributeById($attributeId, $attributeCode, $sequence = 0)
  177. {
  178. $attribute = $this->createAttributeMock($attributeId, $attributeCode);
  179. $this->collectionMock->expects($this->at($sequence))
  180. ->method('getItemById')
  181. ->with($attributeId)
  182. ->willReturn($attribute);
  183. return $attribute;
  184. }
  185. /**
  186. * @param int $attributeId
  187. * @param string $attributeCode
  188. * @param int $sequence
  189. * @return \PHPUnit_Framework_MockObject_MockObject
  190. */
  191. private function mockAttributeByCode($attributeId, $attributeCode, $sequence = 0)
  192. {
  193. $attribute = $this->createAttributeMock($attributeId, $attributeCode);
  194. $this->collectionMock->expects($this->at($sequence))
  195. ->method('getItemByColumnValue')
  196. ->with('attribute_code', $attributeCode)
  197. ->willReturn($attribute);
  198. return $attribute;
  199. }
  200. /**
  201. * @param int $attributeId
  202. * @param string $attributeCode
  203. * @return \PHPUnit_Framework_MockObject_MockObject
  204. */
  205. private function createAttributeMock($attributeId, $attributeCode)
  206. {
  207. $attribute = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
  208. ->setMethods(['getAttributeCode', 'getId'])
  209. ->disableOriginalConstructor()
  210. ->getMock();
  211. $attribute->method('getAttributeCode')
  212. ->willReturn($attributeCode);
  213. $attribute->method('getId')
  214. ->willReturn($attributeId);
  215. return $attribute;
  216. }
  217. }