AttributeRepositoryTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model;
  7. use Magento\Eav\Api\Data\AttributeSearchResultsInterfaceFactory;
  8. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
  9. use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
  10. use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
  11. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  12. use Magento\Framework\Api\SearchCriteriaInterface;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class AttributeRepositoryTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $eavConfig;
  22. /**
  23. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $eavResource;
  26. /**
  27. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $attributeCollectionFactory;
  30. /**
  31. * @var AttributeSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $searchResultsFactory;
  34. /**
  35. * @var \Magento\Eav\Model\Entity\AttributeFactory|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $attributeFactory;
  38. /**
  39. * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $joinProcessor;
  42. /**
  43. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $collectionProcessor;
  46. /**
  47. * @var \Magento\Eav\Model\AttributeRepository
  48. */
  49. private $model;
  50. protected function setUp()
  51. {
  52. $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->eavResource = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->attributeCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['create'])
  61. ->getMock();
  62. $this->searchResultsFactory = $this->getMockBuilder(AttributeSearchResultsInterfaceFactory::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['create'])
  65. ->getMock();
  66. $this->attributeFactory = $this->getMockBuilder(\Magento\Eav\Model\Entity\AttributeFactory::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->joinProcessor = $this->getMockBuilder(JoinProcessorInterface::class)
  70. ->getMockForAbstractClass();
  71. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  72. ->getMockForAbstractClass();
  73. $this->model = new \Magento\Eav\Model\AttributeRepository(
  74. $this->eavConfig,
  75. $this->eavResource,
  76. $this->attributeCollectionFactory,
  77. $this->searchResultsFactory,
  78. $this->attributeFactory,
  79. $this->joinProcessor,
  80. $this->collectionProcessor
  81. );
  82. }
  83. /**
  84. * @expectedException \Magento\Framework\Exception\InputException
  85. * @expectedExceptionMessage "entity_type_code" is required. Enter and try again.
  86. */
  87. public function testGetListInputException()
  88. {
  89. $searchCriteriaMock = $this->getMockBuilder(SearchCriteriaInterface::class)
  90. ->getMockForAbstractClass();
  91. $this->model->getList(null, $searchCriteriaMock);
  92. }
  93. /**
  94. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  95. */
  96. public function testGetList()
  97. {
  98. $entityTypeCode = 'entity_type_code';
  99. $eavEntityTypeTable = 'eav_entity_type_table';
  100. $eavEntityAttributeTable = 'eav_entity_attribute_table';
  101. $additionalTable = 'additional_table';
  102. $attributeCode = 'attribute_code';
  103. $attributeId = 1;
  104. $collectionSize = 1;
  105. $searchCriteriaMock = $this->getMockBuilder(SearchCriteriaInterface::class)
  106. ->setMethods(['getPageSize'])
  107. ->getMockForAbstractClass();
  108. $searchCriteriaMock->expects($this->any())
  109. ->method('getPageSize')
  110. ->willReturn($collectionSize);
  111. $attributeMock = $this->createAttributeMock($attributeCode, $attributeId);
  112. $attributeCollectionMock = $this->getMockBuilder(Collection::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $attributeCollectionMock->expects($this->once())
  116. ->method('addFieldToFilter')
  117. ->with('entity_type_code', ['eq' => $entityTypeCode])
  118. ->willReturnSelf();
  119. $attributeCollectionMock->expects($this->exactly(3))
  120. ->method('getTable')
  121. ->willReturnMap([
  122. ['eav_entity_type', $eavEntityTypeTable],
  123. ['eav_entity_attribute', $eavEntityAttributeTable],
  124. [$additionalTable, $additionalTable],
  125. ]);
  126. $attributeCollectionMock->expects($this->exactly(2))
  127. ->method('join')
  128. ->willReturnMap([
  129. [
  130. ['entity_type' => $eavEntityTypeTable],
  131. 'main_table.entity_type_id = entity_type.entity_type_id',
  132. []
  133. ],
  134. [
  135. ['additional_table' => $additionalTable],
  136. 'main_table.attribute_id = additional_table.attribute_id',
  137. []
  138. ]
  139. ]);
  140. $attributeCollectionMock->expects($this->once())
  141. ->method('joinLeft')
  142. ->with(
  143. ['eav_entity_attribute' => $eavEntityAttributeTable],
  144. 'main_table.attribute_id = eav_entity_attribute.attribute_id',
  145. []
  146. )
  147. ->willReturnSelf();
  148. $attributeCollectionMock->expects($this->once())
  149. ->method('addAttributeGrouping')
  150. ->willReturnSelf();
  151. $attributeCollectionMock->expects($this->once())
  152. ->method('getIterator')
  153. ->willReturn(new \ArrayIterator([$attributeMock]));
  154. $attributeCollectionMock->expects($this->once())
  155. ->method('getSize')
  156. ->willReturn($collectionSize);
  157. $this->attributeCollectionFactory->expects($this->once())
  158. ->method('create')
  159. ->willReturn($attributeCollectionMock);
  160. $this->joinProcessor->expects($this->once())
  161. ->method('process')
  162. ->with($attributeCollectionMock)
  163. ->willReturnSelf();
  164. $entityTypeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  165. ->disableOriginalConstructor()
  166. ->setMethods(['getAdditionalAttributeTable'])
  167. ->getMock();
  168. $entityTypeMock->expects($this->once())
  169. ->method('getAdditionalAttributeTable')
  170. ->willReturn($additionalTable);
  171. $this->eavConfig->expects($this->once())
  172. ->method('getEntityType')
  173. ->with($entityTypeCode)
  174. ->willReturn($entityTypeMock);
  175. $this->eavConfig->expects($this->once())
  176. ->method('getAttribute')
  177. ->with($entityTypeCode, $attributeCode)
  178. ->willReturn($attributeMock);
  179. $this->collectionProcessor->expects($this->once())
  180. ->method('process')
  181. ->with($searchCriteriaMock, $attributeCollectionMock)
  182. ->willReturnSelf();
  183. $searchResultsMock = $this->createSearchResultsMock($searchCriteriaMock, $attributeMock, $collectionSize);
  184. $this->searchResultsFactory->expects($this->once())
  185. ->method('create')
  186. ->willReturn($searchResultsMock);
  187. $this->assertEquals($searchResultsMock, $this->model->getList($entityTypeCode, $searchCriteriaMock));
  188. }
  189. /**
  190. * @param \PHPUnit_Framework_MockObject_MockObject $searchCriteriaMock
  191. * @param \PHPUnit_Framework_MockObject_MockObject $attributeMock
  192. * @param int $collectionSize
  193. * @return \PHPUnit_Framework_MockObject_MockObject
  194. */
  195. protected function createSearchResultsMock($searchCriteriaMock, $attributeMock, $collectionSize)
  196. {
  197. /** @var \PHPUnit_Framework_MockObject_MockObject $searchResultsMock */
  198. $searchResultsMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeSearchResultsInterface::class)
  199. ->getMockForAbstractClass();
  200. $searchResultsMock->expects($this->once())
  201. ->method('setSearchCriteria')
  202. ->with($searchCriteriaMock)
  203. ->willReturnSelf();
  204. $searchResultsMock->expects($this->once())
  205. ->method('setItems')
  206. ->with([$attributeMock])
  207. ->willReturnSelf();
  208. $searchResultsMock->expects($this->once())
  209. ->method('setTotalCount')
  210. ->with($collectionSize)
  211. ->willReturnSelf();
  212. return $searchResultsMock;
  213. }
  214. /**
  215. * @param string $attributeCode
  216. * @param int $attributeId
  217. * @return \PHPUnit_Framework_MockObject_MockObject
  218. */
  219. protected function createAttributeMock($attributeCode, $attributeId)
  220. {
  221. /** @var \PHPUnit_Framework_MockObject_MockObject $attributeMock */
  222. $attributeMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeInterface::class)
  223. ->setMethods([
  224. 'getAttributeCode',
  225. 'getAttributeId',
  226. ])
  227. ->getMockForAbstractClass();
  228. $attributeMock->expects($this->once())
  229. ->method('getAttributeCode')
  230. ->willReturn($attributeCode);
  231. $attributeMock->expects($this->once())
  232. ->method('getAttributeId')
  233. ->willReturn($attributeId);
  234. return $attributeMock;
  235. }
  236. }