GroupRepositoryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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\Attribute;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class GroupRepositoryTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Eav\Model\Attribute\GroupRepository
  15. */
  16. protected $model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $groupResourceMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $groupFactoryMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $setRepositoryMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $searchResultsFactoryMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $groupListFactoryMock;
  37. /**
  38. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $collectionProcessor;
  41. /**
  42. * SetUp method
  43. *
  44. * @return void
  45. */
  46. protected function setUp()
  47. {
  48. $this->groupResourceMock = $this->createPartialMock(
  49. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group::class,
  50. ['delete', '__wakeup', 'load', 'save']
  51. );
  52. $this->groupFactoryMock = $this->createPartialMock(
  53. \Magento\Eav\Model\Entity\Attribute\GroupFactory::class,
  54. ['create']
  55. );
  56. $this->setRepositoryMock = $this->createMock(\Magento\Eav\Api\AttributeSetRepositoryInterface::class);
  57. $this->searchResultsFactoryMock = $this->createPartialMock(
  58. \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory::class,
  59. ['create']
  60. );
  61. $this->groupListFactoryMock = $this->createPartialMock(
  62. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory::class,
  63. ['create']
  64. );
  65. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  66. ->getMockForAbstractClass();
  67. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  68. $this->model = $objectManager->getObject(
  69. \Magento\Eav\Model\Attribute\GroupRepository::class,
  70. [
  71. 'groupResource' => $this->groupResourceMock,
  72. 'groupListFactory' => $this->groupListFactoryMock,
  73. 'groupFactory' => $this->groupFactoryMock,
  74. 'setRepository' => $this->setRepositoryMock,
  75. 'searchResultsFactory' => $this->searchResultsFactoryMock,
  76. 'collectionProcessor' => $this->collectionProcessor
  77. ]
  78. );
  79. }
  80. /**
  81. * Test saving if object is new
  82. *
  83. * @throws \Magento\Framework\Exception\NoSuchEntityException
  84. * @throws \Magento\Framework\Exception\StateException
  85. * @return void
  86. */
  87. public function testSaveIfObjectNew()
  88. {
  89. $attributeSetId = 42;
  90. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  91. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  92. $groupMock->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  93. $attributeSetMock->expects($this->any())->method('getAttributeSetId')->willReturn(10);
  94. $this->setRepositoryMock->expects($this->once())
  95. ->method('get')
  96. ->with($attributeSetId)
  97. ->willReturn($attributeSetMock);
  98. $this->groupResourceMock->expects($this->once())->method('save')->with($groupMock);
  99. $this->assertEquals($groupMock, $this->model->save($groupMock));
  100. }
  101. /**
  102. * Test saving if object is not new
  103. *
  104. * @throws \Magento\Framework\Exception\NoSuchEntityException
  105. * @throws \Magento\Framework\Exception\StateException
  106. * @return void
  107. */
  108. public function testSaveIfObjectNotNew()
  109. {
  110. $attributeSetId = 42;
  111. $groupId = 20;
  112. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  113. $existingGroupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  114. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  115. $groupMock->expects($this->exactly(2))->method('getAttributeSetId')->willReturn($attributeSetId);
  116. $groupMock->expects($this->exactly(2))->method('getAttributeGroupId')->willReturn($groupId);
  117. $attributeSetMock->expects($this->any())->method('getAttributeSetId')->willReturn(10);
  118. $this->setRepositoryMock->expects($this->once())
  119. ->method('get')
  120. ->with($attributeSetId)
  121. ->willReturn($attributeSetMock);
  122. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($existingGroupMock);
  123. $this->groupResourceMock->expects($this->once())->method('load')->with($existingGroupMock, $groupId);
  124. $existingGroupMock->expects($this->any())->method('getId')->willReturn($groupId);
  125. $existingGroupMock->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  126. $this->groupResourceMock->expects($this->once())->method('save')->with($groupMock);
  127. $this->assertEquals($groupMock, $this->model->save($groupMock));
  128. }
  129. /**
  130. * Test saving throws exception if attribute set does not exist
  131. *
  132. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  133. * @expectedExceptionMessage No such entity with attributeSetId = -1
  134. * @throws \Magento\Framework\Exception\NoSuchEntityException
  135. * @throws \Magento\Framework\Exception\StateException
  136. * @return void
  137. */
  138. public function testSaveThrowExceptionIfAttributeSetDoesNotExist()
  139. {
  140. $attributeSetId = -1;
  141. $groupMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Attribute\Group::class, ['getAttributeSetId']);
  142. $groupMock->expects($this->exactly(2))->method('getAttributeSetId')->willReturn($attributeSetId);
  143. $this->setRepositoryMock->expects($this->once())
  144. ->method('get')
  145. ->with($attributeSetId)
  146. ->will(
  147. $this->throwException(
  148. new \Magento\Framework\Exception\NoSuchEntityException(__('AttributeSet does not exist.'))
  149. )
  150. );
  151. $this->model->save($groupMock);
  152. }
  153. /**
  154. * Test saving throws exception if cannot save group
  155. *
  156. * @expectedException \Magento\Framework\Exception\StateException
  157. * @expectedExceptionMessage The attributeGroup can't be saved.
  158. * @throws \Magento\Framework\Exception\NoSuchEntityException
  159. * @throws \Magento\Framework\Exception\StateException
  160. * @return void
  161. */
  162. public function testSaveThrowExceptionIfCannotSaveGroup()
  163. {
  164. $attributeSetId = 42;
  165. $groupId = 20;
  166. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  167. $existingGroupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  168. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  169. $groupMock->expects($this->any())->method('getAttributeSetId')->willReturn($attributeSetId);
  170. $groupMock->expects($this->any())->method('getAttributeGroupId')->willReturn($groupId);
  171. $attributeSetMock->expects($this->any())->method('getAttributeSetId')->willReturn(10);
  172. $this->setRepositoryMock->expects($this->once())->method('get')->with($attributeSetId)
  173. ->willReturn($attributeSetMock);
  174. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($existingGroupMock);
  175. $this->groupResourceMock->expects($this->once())->method('load')->with($existingGroupMock, $groupId);
  176. $existingGroupMock->expects($this->any())->method('getId')->willReturn($groupId);
  177. $existingGroupMock->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
  178. $this->groupResourceMock->expects($this->once())
  179. ->method('save')
  180. ->will($this->throwException(new \Exception()));
  181. $this->model->save($groupMock);
  182. }
  183. /**
  184. * Test saving throws exception if group does not belong to provided set
  185. *
  186. * @expectedException \Magento\Framework\Exception\StateException
  187. * @expectedExceptionMessage The attribute group doesn't belong to the provided attribute set.
  188. * @throws \Magento\Framework\Exception\NoSuchEntityException
  189. * @throws \Magento\Framework\Exception\StateException
  190. * @return void
  191. */
  192. public function testSaveThrowExceptionIfGroupDoesNotBelongToProvidedSet()
  193. {
  194. $attributeSetId = 42;
  195. $groupId = 20;
  196. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  197. $existingGroupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  198. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  199. $groupMock->expects($this->any())->method('getAttributeSetId')->willReturn($attributeSetId);
  200. $groupMock->expects($this->any())->method('getAttributeGroupId')->willReturn($groupId);
  201. $attributeSetMock->expects($this->any())->method('getAttributeSetId')->willReturn(10);
  202. $this->setRepositoryMock->expects($this->once())->method('get')->with($attributeSetId)
  203. ->willReturn($attributeSetMock);
  204. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($existingGroupMock);
  205. $this->groupResourceMock->expects($this->once())->method('load')->with($existingGroupMock, $groupId);
  206. $existingGroupMock->expects($this->any())->method('getId')->willReturn($groupId);
  207. $this->model->save($groupMock);
  208. }
  209. /**
  210. * Test saving throws exception if provided group does not exist
  211. *
  212. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  213. * @expectedExceptionMessage No such entity with attributeGroupId =
  214. * @throws \Magento\Framework\Exception\NoSuchEntityException
  215. * @throws \Magento\Framework\Exception\StateException
  216. * @return void
  217. */
  218. public function testSaveThrowExceptionIfProvidedGroupDoesNotExist()
  219. {
  220. $attributeSetId = 42;
  221. $groupId = 20;
  222. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  223. $existingGroupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  224. $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
  225. $groupMock->expects($this->any())->method('getAttributeSetId')->willReturn($attributeSetId);
  226. $groupMock->expects($this->any())->method('getAttributeGroupId')->willReturn($groupId);
  227. $attributeSetMock->expects($this->any())->method('getAttributeSetId')->willReturn(10);
  228. $this->setRepositoryMock->expects($this->once())->method('get')->with($attributeSetId)
  229. ->willReturn($attributeSetMock);
  230. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($existingGroupMock);
  231. $this->groupResourceMock->expects($this->once())->method('load')->with($existingGroupMock, $groupId);
  232. $existingGroupMock->expects($this->any())->method('getId')->willReturn(false);
  233. $this->model->save($groupMock);
  234. }
  235. /**
  236. * Test get list
  237. *
  238. * @throws \Magento\Framework\Exception\InputException
  239. * @throws \Magento\Framework\Exception\NoSuchEntityException
  240. * @return void
  241. */
  242. public function testGetList()
  243. {
  244. $filterInterfaceMock = $this->getMockBuilder(\Magento\Framework\Api\Search\FilterGroup::class)
  245. ->disableOriginalConstructor()
  246. ->setMethods([
  247. 'getField',
  248. 'getValue',
  249. ])
  250. ->getMock();
  251. $filterGroupMock = $this->getMockBuilder(\Magento\Framework\Api\Search\FilterGroup::class)
  252. ->disableOriginalConstructor()
  253. ->getMock();
  254. $filterGroupMock->expects($this->any())
  255. ->method('getFilters')
  256. ->willReturn([$filterInterfaceMock]);
  257. $searchCriteriaMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
  258. ->disableOriginalConstructor()
  259. ->getMock();
  260. $searchCriteriaMock->expects($this->any())
  261. ->method('getFilterGroups')
  262. ->willReturn([$filterGroupMock]);
  263. $groupMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Group::class)
  264. ->disableOriginalConstructor()
  265. ->getMock();
  266. $groupCollectionMock = $this->createPartialMock(
  267. \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
  268. ['getItems', 'getSize']
  269. );
  270. $groupCollectionMock->expects($this->once())->method('getItems')->willReturn([$groupMock]);
  271. $this->groupListFactoryMock->expects($this->once())->method('create')->willReturn($groupCollectionMock);
  272. $groupCollectionMock->expects($this->once())->method('getSize')->willReturn(1);
  273. $searchResultsMock = $this->createMock(\Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface::class);
  274. $searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);
  275. $searchResultsMock->expects($this->once())->method('setItems')->with([$groupMock]);
  276. $searchResultsMock->expects($this->once())->method('setTotalCount')->with(1);
  277. $this->searchResultsFactoryMock->expects($this->once())->method('create')->willReturn($searchResultsMock);
  278. $this->collectionProcessor->expects($this->once())
  279. ->method('process')
  280. ->with($searchCriteriaMock, $groupCollectionMock)
  281. ->willReturnSelf();
  282. $this->assertEquals($searchResultsMock, $this->model->getList($searchCriteriaMock));
  283. }
  284. /**
  285. * Test get
  286. *
  287. * @throws \Magento\Framework\Exception\NoSuchEntityException
  288. * @return void
  289. */
  290. public function testGet()
  291. {
  292. $groupId = 42;
  293. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  294. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($groupMock);
  295. $this->groupResourceMock->expects($this->once())->method('load')->with($groupMock, $groupId);
  296. $groupMock->expects($this->once())->method('getId')->willReturn($groupId);
  297. $this->assertEquals($groupMock, $this->model->get($groupId));
  298. }
  299. /**
  300. * Test get throws exception if provided group does not exist
  301. *
  302. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  303. * @expectedExceptionMessage The group with the "42" ID doesn't exist. Verify the ID and try again.
  304. * @throws \Magento\Framework\Exception\NoSuchEntityException
  305. * @return void
  306. */
  307. public function testGetThrowExceptionIfProvidedGroupDoesNotExist()
  308. {
  309. $groupId = 42;
  310. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  311. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($groupMock);
  312. $this->groupResourceMock->expects($this->once())->method('load')->with($groupMock, $groupId);
  313. $groupMock->expects($this->once())->method('getId')->willReturn(false);
  314. $this->assertEquals($groupMock, $this->model->get($groupId));
  315. }
  316. /**
  317. * Test delete
  318. *
  319. * @throws \Magento\Framework\Exception\StateException
  320. * @return void
  321. */
  322. public function testDelete()
  323. {
  324. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  325. $this->groupResourceMock->expects($this->once())->method('delete')->with($groupMock);
  326. $this->assertTrue($this->model->delete($groupMock));
  327. }
  328. /**
  329. * Test deletion throws exception if provided group does not exist
  330. *
  331. * @expectedException \Magento\Framework\Exception\StateException
  332. * @expectedExceptionMessage The attribute group with id "42" can't be deleted.
  333. * @throws \Magento\Framework\Exception\StateException
  334. * @return void
  335. */
  336. public function testDeleteThrowExceptionIfProvidedGroupDoesNotExist()
  337. {
  338. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  339. $this->groupResourceMock->expects($this->once())
  340. ->method('delete')
  341. ->with($groupMock)
  342. ->will($this->throwException(new \Exception()));
  343. $groupMock->expects($this->once())->method('getId')->willReturn(42);
  344. $this->model->delete($groupMock);
  345. }
  346. /**
  347. * Test delete by id
  348. *
  349. * @return void
  350. */
  351. public function testDeleteById()
  352. {
  353. $groupId = 42;
  354. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  355. $this->groupFactoryMock->expects($this->once())->method('create')->willReturn($groupMock);
  356. $this->groupResourceMock->expects($this->once())->method('load')->with($groupMock, $groupId);
  357. $groupMock->expects($this->once())->method('getId')->willReturn($groupId);
  358. $groupMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Group::class);
  359. $this->groupResourceMock->expects($this->once())->method('delete')->with($groupMock);
  360. $this->assertTrue($this->model->deleteById($groupId));
  361. }
  362. }