AttributeSetRepositoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\Model\AttributeSetRepository;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.LongVariable)
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class AttributeSetRepositoryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var AttributeSetRepository
  17. */
  18. private $model;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $resourceMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $setFactoryMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $collectionFactoryMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $eavConfigMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $resultFactoryMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $extensionAttributesJoinProcessorMock;
  43. /**
  44. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $collectionProcessor;
  47. /**
  48. * @return void
  49. */
  50. protected function setUp()
  51. {
  52. $this->resourceMock = $this->createMock(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class);
  53. $this->setFactoryMock = $this->createPartialMock(
  54. \Magento\Eav\Model\Entity\Attribute\SetFactory::class,
  55. ['create']
  56. );
  57. $this->collectionFactoryMock = $this->createPartialMock(
  58. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class,
  59. ['create']
  60. );
  61. $this->eavConfigMock = $this->createPartialMock(\Magento\Eav\Model\Config::class, ['getEntityType']);
  62. $this->resultFactoryMock = $this->createPartialMock(
  63. \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory::class,
  64. ['create']
  65. );
  66. $this->extensionAttributesJoinProcessorMock = $this->createPartialMock(
  67. \Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
  68. ['process']
  69. );
  70. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  71. ->getMockForAbstractClass();
  72. $this->model = new \Magento\Eav\Model\AttributeSetRepository(
  73. $this->resourceMock,
  74. $this->setFactoryMock,
  75. $this->collectionFactoryMock,
  76. $this->eavConfigMock,
  77. $this->resultFactoryMock,
  78. $this->extensionAttributesJoinProcessorMock,
  79. $this->collectionProcessor
  80. );
  81. }
  82. /**
  83. * @return void
  84. */
  85. public function testGet()
  86. {
  87. $attributeSetId = 1;
  88. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  89. $this->setFactoryMock->expects($this->once())->method('create')->will($this->returnValue($attributeSetMock));
  90. $this->resourceMock->expects($this->once())->method('load')->with($attributeSetMock, $attributeSetId, null);
  91. $attributeSetMock->expects($this->any())->method('getId')->will($this->returnValue($attributeSetId));
  92. $this->assertEquals($attributeSetMock, $this->model->get($attributeSetId));
  93. }
  94. /**
  95. * @return void
  96. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  97. * @expectedExceptionMessage No such entity with id = 9999
  98. */
  99. public function testGetThrowsExceptionIfRequestedAttributeSetDoesNotExist()
  100. {
  101. $attributeSetId = 9999;
  102. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  103. $this->setFactoryMock->expects($this->once())->method('create')->will($this->returnValue($attributeSetMock));
  104. $this->resourceMock->expects($this->once())->method('load')->with($attributeSetMock, $attributeSetId, null);
  105. $this->model->get($attributeSetId);
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testSave()
  111. {
  112. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  113. $this->resourceMock->expects($this->once())->method('save')->with($attributeSetMock);
  114. $this->assertEquals($attributeSetMock, $this->model->save($attributeSetMock));
  115. }
  116. /**
  117. * @return void
  118. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  119. */
  120. public function testSaveThrowsExceptionIfGivenEntityCannotBeSaved()
  121. {
  122. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  123. $this->resourceMock->expects($this->once())->method('save')->with($attributeSetMock)->willThrowException(
  124. new \Exception('Some internal exception message.')
  125. );
  126. $this->model->save($attributeSetMock);
  127. $this->expectExceptionMessage(
  128. "The attribute set couldn't be saved due to an error. Verify your information and try again. "
  129. . "If the error persists, please try again later."
  130. );
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testDelete()
  136. {
  137. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  138. $this->resourceMock->expects($this->once())->method('delete')->with($attributeSetMock);
  139. $this->assertTrue($this->model->delete($attributeSetMock));
  140. }
  141. /**
  142. * @return void
  143. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  144. */
  145. public function testDeleteThrowsExceptionIfGivenEntityCannotBeDeleted()
  146. {
  147. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  148. $this->resourceMock->expects($this->once())->method('delete')->with($attributeSetMock)->willThrowException(
  149. new \Magento\Framework\Exception\CouldNotDeleteException(__('Some internal exception message.'))
  150. );
  151. $this->model->delete($attributeSetMock);
  152. $this->expectExceptionMessage(
  153. "The attribute set couldn't be deleted due to an error. "
  154. . "Try again — if the error persists, please try again later."
  155. );
  156. }
  157. /**
  158. * @return void
  159. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  160. * @expectedExceptionMessage The default attribute set can't be deleted.
  161. */
  162. public function testDeleteThrowsExceptionIfGivenAttributeSetIsDefault()
  163. {
  164. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  165. $this->resourceMock->expects($this->once())->method('delete')->with($attributeSetMock)->willThrowException(
  166. new \Magento\Framework\Exception\StateException(__('Some internal exception message.'))
  167. );
  168. $this->model->delete($attributeSetMock);
  169. }
  170. /**
  171. * @return void
  172. */
  173. public function testDeleteById()
  174. {
  175. $attributeSetId = 1;
  176. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  177. $attributeSetMock->expects($this->any())->method('getId')->will($this->returnValue($attributeSetId));
  178. $this->setFactoryMock->expects($this->once())->method('create')->will($this->returnValue($attributeSetMock));
  179. $this->resourceMock->expects($this->once())->method('load')->with($attributeSetMock, $attributeSetId, null);
  180. $this->resourceMock->expects($this->once())->method('delete')->with($attributeSetMock);
  181. $this->assertTrue($this->model->deleteById($attributeSetId));
  182. }
  183. /**
  184. * @return void
  185. */
  186. public function testGetList()
  187. {
  188. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  189. $collectionMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::class)
  190. ->disableOriginalConstructor()
  191. ->setMethods([
  192. 'getItems',
  193. 'getSize',
  194. ])
  195. ->getMock();
  196. $collectionMock->expects($this->once())
  197. ->method('getItems')
  198. ->willReturn([$attributeSetMock]);
  199. $collectionMock->expects($this->once())
  200. ->method('getSize')
  201. ->willReturn(1);
  202. $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
  203. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
  204. $resultMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeSetSearchResultsInterface::class)
  205. ->getMockForAbstractClass();
  206. $resultMock->expects($this->once())
  207. ->method('setSearchCriteria')
  208. ->with($searchCriteriaMock)
  209. ->willReturnSelf();
  210. $resultMock->expects($this->once())
  211. ->method('setItems')
  212. ->with([$attributeSetMock])
  213. ->willReturnSelf();
  214. $resultMock->expects($this->once())
  215. ->method('setTotalCount')
  216. ->with(1)
  217. ->willReturnSelf();
  218. $this->resultFactoryMock->expects($this->once())
  219. ->method('create')
  220. ->willReturn($resultMock);
  221. $this->collectionProcessor->expects($this->once())
  222. ->method('process')
  223. ->with($searchCriteriaMock, $collectionMock)
  224. ->willReturnSelf();
  225. $this->model->getList($searchCriteriaMock);
  226. }
  227. /**
  228. * @return void
  229. */
  230. public function testGetListIfEntityTypeCodeIsNull()
  231. {
  232. $attributeSetMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Set::class);
  233. $collectionMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::class)
  234. ->disableOriginalConstructor()
  235. ->setMethods([
  236. 'getItems',
  237. 'getSize',
  238. ])
  239. ->getMock();
  240. $collectionMock->expects($this->once())
  241. ->method('getItems')
  242. ->willReturn([$attributeSetMock]);
  243. $collectionMock->expects($this->once())
  244. ->method('getSize')
  245. ->willReturn(1);
  246. $this->collectionFactoryMock->expects($this->once())
  247. ->method('create')
  248. ->willReturn($collectionMock);
  249. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
  250. $resultMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeSetSearchResultsInterface::class)
  251. ->getMockForAbstractClass();
  252. $resultMock->expects($this->once())
  253. ->method('setSearchCriteria')
  254. ->with($searchCriteriaMock)
  255. ->willReturnSelf();
  256. $resultMock->expects($this->once())
  257. ->method('setItems')
  258. ->with([$attributeSetMock])
  259. ->willReturnSelf();
  260. $resultMock->expects($this->once())
  261. ->method('setTotalCount')
  262. ->with(1)
  263. ->willReturnSelf();
  264. $this->resultFactoryMock->expects($this->once())
  265. ->method('create')
  266. ->willReturn($resultMock);
  267. $this->collectionProcessor->expects($this->once())
  268. ->method('process')
  269. ->with($searchCriteriaMock, $collectionMock)
  270. ->willReturnSelf();
  271. $this->model->getList($searchCriteriaMock);
  272. }
  273. }