GroupRepositoryTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\ResourceModel;
  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\Customer\Model\GroupRegistry|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $groupRegistry;
  17. /**
  18. * @var \Magento\Customer\Model\GroupFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $groupFactory;
  21. /**
  22. * @var \Magento\Customer\Model\Group|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $groupModel;
  25. /**
  26. * @var \Magento\Customer\Api\Data\GroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $groupDataFactory;
  29. /**
  30. * @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $group;
  33. /**
  34. * @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $factoryCreatedGroup;
  37. /**
  38. * @var \Magento\Customer\Model\ResourceModel\Group|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $groupResourceModel;
  41. /**
  42. * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $dataObjectProcessor;
  45. /**
  46. * @var \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $searchResultsFactory;
  49. /**
  50. * @var \Magento\Customer\Api\Data\GroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $searchResults;
  53. /**
  54. * @var \Magento\Tax\Api\TaxClassRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $taxClassRepository;
  57. /**
  58. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $extensionAttributesJoinProcessor;
  61. /**
  62. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $collectionProcessorMock;
  65. /**
  66. * @var \Magento\Customer\Model\ResourceModel\GroupRepository
  67. */
  68. protected $model;
  69. protected function setUp()
  70. {
  71. $this->setupGroupObjects();
  72. $this->dataObjectProcessor = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
  73. $this->searchResultsFactory = $this->createPartialMock(
  74. \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory::class,
  75. ['create']
  76. );
  77. $this->searchResults = $this->getMockForAbstractClass(
  78. \Magento\Customer\Api\Data\GroupSearchResultsInterface::class,
  79. [],
  80. '',
  81. false
  82. );
  83. $this->taxClassRepository = $this->getMockForAbstractClass(
  84. \Magento\Tax\Api\TaxClassRepositoryInterface::class,
  85. [],
  86. '',
  87. false
  88. );
  89. $this->extensionAttributesJoinProcessor = $this->getMockForAbstractClass(
  90. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class,
  91. [],
  92. '',
  93. false
  94. );
  95. $this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
  96. ->getMock();
  97. $this->model = new \Magento\Customer\Model\ResourceModel\GroupRepository(
  98. $this->groupRegistry,
  99. $this->groupFactory,
  100. $this->groupDataFactory,
  101. $this->groupResourceModel,
  102. $this->dataObjectProcessor,
  103. $this->searchResultsFactory,
  104. $this->taxClassRepository,
  105. $this->extensionAttributesJoinProcessor,
  106. $this->collectionProcessorMock
  107. );
  108. }
  109. private function setupGroupObjects()
  110. {
  111. $this->groupRegistry = $this->createMock(\Magento\Customer\Model\GroupRegistry::class);
  112. $this->groupFactory = $this->createPartialMock(\Magento\Customer\Model\GroupFactory::class, ['create']);
  113. $this->groupModel = $this->getMockBuilder(\Magento\Customer\Model\Group::class)
  114. ->setMethods(
  115. [
  116. 'getTaxClassId',
  117. 'getTaxClassName',
  118. 'getId',
  119. 'getCode',
  120. 'setDataUsingMethod',
  121. 'setCode',
  122. 'setTaxClassId',
  123. 'usesAsDefault',
  124. 'delete',
  125. 'getCollection',
  126. 'getData',
  127. ]
  128. )
  129. ->setMockClassName('groupModel')
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $this->groupDataFactory = $this->createPartialMock(
  133. \Magento\Customer\Api\Data\GroupInterfaceFactory::class,
  134. ['create']
  135. );
  136. $this->group = $this->getMockForAbstractClass(
  137. \Magento\Customer\Api\Data\GroupInterface::class,
  138. [],
  139. 'group',
  140. false
  141. );
  142. $this->factoryCreatedGroup = $this->getMockForAbstractClass(
  143. \Magento\Customer\Api\Data\GroupInterface::class,
  144. [],
  145. 'group',
  146. false
  147. );
  148. $this->groupResourceModel = $this->createMock(\Magento\Customer\Model\ResourceModel\Group::class);
  149. }
  150. public function testSave()
  151. {
  152. $groupId = 0;
  153. $taxClass = $this->getMockForAbstractClass(\Magento\Tax\Api\Data\TaxClassInterface::class, [], '', false);
  154. $extensionAttributes = $this->getMockForAbstractClass(
  155. \Magento\Customer\Api\Data\GroupExtensionInterface::class
  156. );
  157. $this->group->expects($this->atLeastOnce())
  158. ->method('getCode')
  159. ->willReturn('Code');
  160. $this->group->expects($this->atLeastOnce())
  161. ->method('getId')
  162. ->willReturn($groupId);
  163. $this->group->expects($this->atLeastOnce())
  164. ->method('getTaxClassId')
  165. ->willReturn(17);
  166. $this->group->expects($this->atLeastOnce())
  167. ->method('getExtensionAttributes')
  168. ->willReturn($extensionAttributes);
  169. $this->groupModel->expects($this->atLeastOnce())
  170. ->method('getId')
  171. ->willReturn($groupId);
  172. $this->groupModel->expects($this->atLeastOnce())
  173. ->method('getCode')
  174. ->willReturn('Code');
  175. $this->groupModel->expects($this->atLeastOnce())
  176. ->method('getTaxClassId')
  177. ->willReturn(234);
  178. $this->groupModel->expects($this->atLeastOnce())
  179. ->method('getTaxClassName')
  180. ->willReturn('Tax class name');
  181. $this->factoryCreatedGroup->expects($this->once())
  182. ->method('setId')
  183. ->with($groupId)
  184. ->willReturnSelf();
  185. $this->factoryCreatedGroup->expects($this->once())
  186. ->method('setCode')
  187. ->with('Code')
  188. ->willReturnSelf();
  189. $this->factoryCreatedGroup->expects($this->once())
  190. ->method('setTaxClassId')
  191. ->with(234)
  192. ->willReturnSelf();
  193. $this->factoryCreatedGroup->expects($this->once())
  194. ->method('setTaxClassName')
  195. ->with('Tax class name')
  196. ->willReturnSelf();
  197. $this->factoryCreatedGroup->expects($this->once())
  198. ->method('setExtensionAttributes')
  199. ->with($extensionAttributes)
  200. ->willReturnSelf();
  201. $this->factoryCreatedGroup->expects($this->atLeastOnce())
  202. ->method('getCode')
  203. ->willReturn('Code');
  204. $this->factoryCreatedGroup->expects($this->atLeastOnce())
  205. ->method('getTaxClassId')
  206. ->willReturn(17);
  207. $this->taxClassRepository->expects($this->once())
  208. ->method('get')
  209. ->with(17)
  210. ->willReturn($taxClass);
  211. $taxClass->expects($this->once())
  212. ->method('getClassType')
  213. ->willReturn('CUSTOMER');
  214. $this->groupRegistry->expects($this->once())
  215. ->method('retrieve')
  216. ->with($groupId)
  217. ->willReturn($this->groupModel);
  218. $this->dataObjectProcessor->expects($this->once())
  219. ->method('buildOutputDataArray')
  220. ->with($this->group, \Magento\Customer\Api\Data\GroupInterface::class)
  221. ->willReturn(['attributeCode' => 'attributeData']);
  222. $this->groupModel->expects($this->once())
  223. ->method('setDataUsingMethod')
  224. ->with('attributeCode', 'attributeData');
  225. $this->groupResourceModel->expects($this->once())
  226. ->method('save')
  227. ->with($this->groupModel);
  228. $this->groupRegistry->expects($this->once())
  229. ->method('remove')
  230. ->with($groupId);
  231. $this->groupDataFactory->expects($this->once())
  232. ->method('create')
  233. ->willReturn($this->factoryCreatedGroup);
  234. $updatedGroup = $this->model->save($this->group);
  235. $this->assertSame($this->group->getCode(), $updatedGroup->getCode());
  236. $this->assertSame($this->group->getTaxClassId(), $updatedGroup->getTaxClassId());
  237. }
  238. /**
  239. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  240. */
  241. public function testSaveWithException()
  242. {
  243. $taxClass = $this->getMockForAbstractClass(\Magento\Tax\Api\Data\TaxClassInterface::class, [], '', false);
  244. $this->groupFactory->expects($this->once())
  245. ->method('create')
  246. ->willReturn($this->groupModel);
  247. $this->group->expects($this->atLeastOnce())
  248. ->method('getCode')
  249. ->willReturn('Code');
  250. $this->group->expects($this->atLeastOnce())
  251. ->method('getId')
  252. ->willReturn(false);
  253. $this->group->expects($this->atLeastOnce())
  254. ->method('getTaxClassId')
  255. ->willReturn(234);
  256. $this->group->expects($this->atLeastOnce())
  257. ->method('getTaxClassId')
  258. ->willReturn(17);
  259. $this->groupModel->expects($this->once())
  260. ->method('setCode')
  261. ->with('Code');
  262. $this->groupModel->expects($this->once())
  263. ->method('setTaxClassId')
  264. ->with(234);
  265. $this->taxClassRepository->expects($this->once())
  266. ->method('get')
  267. ->with(234)
  268. ->willReturn($taxClass);
  269. $taxClass->expects($this->once())
  270. ->method('getClassType')
  271. ->willReturn('CUSTOMER');
  272. $this->groupResourceModel->expects($this->once())
  273. ->method('save')
  274. ->with($this->groupModel)
  275. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(
  276. new \Magento\Framework\Phrase('Customer Group already exists.')
  277. ));
  278. $this->model->save($this->group);
  279. }
  280. public function testGetById()
  281. {
  282. $groupId = 86;
  283. $this->groupRegistry->expects($this->once())
  284. ->method('retrieve')
  285. ->with($groupId)
  286. ->willReturn($this->groupModel);
  287. $this->groupDataFactory->expects($this->once())
  288. ->method('create')
  289. ->willReturn($this->group);
  290. $this->group->expects($this->once())
  291. ->method('setId')
  292. ->with($groupId)
  293. ->willReturnSelf();
  294. $this->group->expects($this->once())
  295. ->method('setCode')
  296. ->with('Code')
  297. ->willReturnSelf();
  298. $this->group->expects($this->once())
  299. ->method('setTaxClassId')
  300. ->with(234)
  301. ->willReturnSelf();
  302. $this->group->expects($this->once())
  303. ->method('setTaxClassName')
  304. ->with('Tax class name')
  305. ->willReturnSelf();
  306. $this->groupModel->expects($this->atLeastOnce())
  307. ->method('getId')
  308. ->willReturn($groupId);
  309. $this->groupModel->expects($this->atLeastOnce())
  310. ->method('getCode')
  311. ->willReturn('Code');
  312. $this->groupModel->expects($this->atLeastOnce())
  313. ->method('getTaxClassId')
  314. ->willReturn(234);
  315. $this->groupModel->expects($this->atLeastOnce())
  316. ->method('getTaxClassName')
  317. ->willReturn('Tax class name');
  318. $this->assertSame($this->group, $this->model->getById($groupId));
  319. }
  320. /**
  321. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  322. */
  323. public function testGetList()
  324. {
  325. $groupId = 86;
  326. $groupExtension = $this->createMock(\Magento\Customer\Api\Data\GroupExtensionInterface::class);
  327. $collection = $this->createMock(\Magento\Customer\Model\ResourceModel\Group\Collection::class);
  328. $searchCriteria = $this->getMockForAbstractClass(
  329. \Magento\Framework\Api\SearchCriteriaInterface::class,
  330. [],
  331. '',
  332. false
  333. );
  334. $searchResults = $this->getMockForAbstractClass(
  335. \Magento\Customer\Api\Data\AddressSearchResultsInterface::class,
  336. [],
  337. '',
  338. false
  339. );
  340. $this->searchResultsFactory->expects($this->once())
  341. ->method('create')
  342. ->willReturn($searchResults);
  343. $searchResults->expects($this->once())
  344. ->method('setSearchCriteria')
  345. ->with($searchCriteria);
  346. $this->groupFactory->expects($this->once())
  347. ->method('create')
  348. ->willReturn($this->groupModel);
  349. $this->groupModel->expects($this->once())
  350. ->method('getCollection')
  351. ->willReturn($collection);
  352. $this->extensionAttributesJoinProcessor->expects($this->once())
  353. ->method('process')
  354. ->with($collection, \Magento\Customer\Api\Data\GroupInterface::class);
  355. $collection->expects($this->once())
  356. ->method('addTaxClass');
  357. $this->collectionProcessorMock->expects($this->once())
  358. ->method('process')
  359. ->with($searchCriteria, $collection);
  360. $collection->expects($this->once())
  361. ->method('getIterator')
  362. ->willReturn(new \ArrayIterator([$this->groupModel]));
  363. $this->groupDataFactory->expects($this->once())
  364. ->method('create')
  365. ->willReturn($this->group);
  366. $this->group->expects($this->once())
  367. ->method('setId')
  368. ->with($groupId)
  369. ->willReturnSelf();
  370. $this->group->expects($this->once())
  371. ->method('setCode')
  372. ->with('Code')
  373. ->willReturnSelf();
  374. $this->group->expects($this->once())
  375. ->method('setTaxClassId')
  376. ->with(234)
  377. ->willReturnSelf();
  378. $this->group->expects($this->once())
  379. ->method('setTaxClassName')
  380. ->with('Tax class name')
  381. ->willReturnSelf();
  382. $this->groupModel->expects($this->atLeastOnce())
  383. ->method('getId')
  384. ->willReturn($groupId);
  385. $this->groupModel->expects($this->atLeastOnce())
  386. ->method('getCode')
  387. ->willReturn('Code');
  388. $this->groupModel->expects($this->atLeastOnce())
  389. ->method('getTaxClassId')
  390. ->willReturn(234);
  391. $this->groupModel->expects($this->atLeastOnce())
  392. ->method('getTaxClassName')
  393. ->willReturn('Tax class name');
  394. $this->groupModel->expects($this->once())
  395. ->method('getData')
  396. ->willReturn([]);
  397. $this->extensionAttributesJoinProcessor->expects($this->once())
  398. ->method('extractExtensionAttributes')
  399. ->with(\Magento\Customer\Api\Data\GroupInterface::class, [])
  400. ->willReturn(['extension_attributes' => $groupExtension]);
  401. $this->group->expects($this->once())
  402. ->method('setExtensionAttributes')
  403. ->with($groupExtension);
  404. $collection
  405. ->expects($this->once())
  406. ->method('getSize')
  407. ->willReturn(9);
  408. $searchResults->expects($this->once())
  409. ->method('setTotalCount')
  410. ->with(9);
  411. $searchResults->expects($this->once())
  412. ->method('setItems')
  413. ->with([$this->group])
  414. ->willReturnSelf();
  415. $this->assertSame($searchResults, $this->model->getList($searchCriteria));
  416. }
  417. public function testDeleteById()
  418. {
  419. $groupId = 6;
  420. $this->groupRegistry->expects($this->once())
  421. ->method('retrieve')
  422. ->with($groupId)
  423. ->willReturn($this->groupModel);
  424. $this->groupModel->expects($this->once())
  425. ->method('usesAsDefault')
  426. ->willReturn(false);
  427. $this->groupModel->expects($this->once())
  428. ->method('delete');
  429. $this->groupRegistry
  430. ->expects($this->once())
  431. ->method('remove')
  432. ->with($groupId);
  433. $this->assertTrue($this->model->deleteById($groupId));
  434. }
  435. public function testDelete()
  436. {
  437. $groupId = 6;
  438. $this->group->expects($this->once())
  439. ->method('getId')
  440. ->willReturn($groupId);
  441. $this->groupRegistry->expects($this->once())
  442. ->method('retrieve')
  443. ->with($groupId)
  444. ->willReturn($this->groupModel);
  445. $this->groupModel->expects($this->once())
  446. ->method('usesAsDefault')
  447. ->willReturn(false);
  448. $this->groupModel->expects($this->once())
  449. ->method('delete');
  450. $this->groupRegistry
  451. ->expects($this->once())
  452. ->method('remove')
  453. ->with($groupId);
  454. $this->assertTrue($this->model->delete($this->group));
  455. }
  456. }