GroupRepositoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel;
  7. use Magento\Customer\Api\Data\GroupInterface;
  8. use Magento\Framework\Api\SortOrder;
  9. /**
  10. * Integration test for \Magento\Customer\Model\ResourceModel\GroupRepository
  11. */
  12. class GroupRepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** The group id of the "NOT LOGGED IN" group */
  15. const NOT_LOGGED_IN_GROUP_ID = 0;
  16. /** @var \Magento\Customer\Api\GroupRepositoryInterface */
  17. private $groupRepository;
  18. /** @var \Magento\Framework\ObjectManagerInterface */
  19. private $objectManager;
  20. /** @var \Magento\Customer\Api\Data\GroupInterfaceFactory */
  21. private $groupFactory;
  22. /** @var \Magento\Framework\Api\SearchCriteriaBuilder */
  23. private $searchCriteriaBuilder;
  24. /** @var \Magento\Framework\Api\SortOrderBuilder */
  25. private $sortOrderBuilder;
  26. protected function setUp()
  27. {
  28. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  29. $this->groupRepository = $this->objectManager->create(\Magento\Customer\Api\GroupRepositoryInterface::class);
  30. $this->groupFactory = $this->objectManager->create(\Magento\Customer\Api\Data\GroupInterfaceFactory::class);
  31. $this->searchCriteriaBuilder = $this->objectManager->create(
  32. \Magento\Framework\Api\SearchCriteriaBuilder::class
  33. );
  34. $this->sortOrderBuilder = $this->objectManager->create(\Magento\Framework\Api\SortOrderBuilder::class);
  35. }
  36. /**
  37. * @param array $testGroup
  38. * @dataProvider getGroupsDataProvider
  39. */
  40. public function testGetGroup($testGroup)
  41. {
  42. $group = $this->groupRepository->getById($testGroup[GroupInterface::ID]);
  43. $this->assertEquals($testGroup[GroupInterface::ID], $group->getId());
  44. $this->assertEquals($testGroup[GroupInterface::CODE], $group->getCode());
  45. $this->assertEquals($testGroup[GroupInterface::TAX_CLASS_ID], $group->getTaxClassId());
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function getGroupsDataProvider()
  51. {
  52. return [
  53. [[GroupInterface::ID => 0, GroupInterface::CODE => 'NOT LOGGED IN', GroupInterface::TAX_CLASS_ID => 3]],
  54. [[GroupInterface::ID => 1, GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3]],
  55. [[GroupInterface::ID => 2, GroupInterface::CODE => 'Wholesale', GroupInterface::TAX_CLASS_ID => 3]],
  56. [[GroupInterface::ID => 3, GroupInterface::CODE => 'Retailer', GroupInterface::TAX_CLASS_ID => 3]],
  57. ];
  58. }
  59. /**
  60. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  61. * @expectedExceptionMessage No such entity with id = 9999
  62. */
  63. public function testGetGroupException()
  64. {
  65. $this->groupRepository->getById(9999);
  66. }
  67. /**
  68. * @magentoDbIsolation enabled
  69. */
  70. public function testCreateGroup()
  71. {
  72. $group = $this->groupFactory->create()->setId(null)->setCode('Create Group')->setTaxClassId(3);
  73. $groupId = $this->groupRepository->save($group)->getId();
  74. $this->assertNotNull($groupId);
  75. $newGroup = $this->groupRepository->getById($groupId);
  76. $this->assertEquals($groupId, $newGroup->getId());
  77. $this->assertEquals($group->getCode(), $newGroup->getCode());
  78. $this->assertEquals($group->getTaxClassId(), $newGroup->getTaxClassId());
  79. }
  80. /**
  81. * @magentoDbIsolation enabled
  82. */
  83. public function testCreateGroupDefaultTaxClass()
  84. {
  85. $group = $this->groupFactory->create()->setId(null)->setCode('Create Group')->setTaxClassId(null);
  86. $groupId = $this->groupRepository->save($group)->getId();
  87. $this->assertNotNull($groupId);
  88. $newGroup = $this->groupRepository->getById($groupId);
  89. $this->assertEquals($groupId, $newGroup->getId());
  90. $this->assertEquals($group->getCode(), $newGroup->getCode());
  91. $this->assertEquals(GroupRepository::DEFAULT_TAX_CLASS_ID, $newGroup->getTaxClassId());
  92. }
  93. /**
  94. * @magentoDbIsolation enabled
  95. */
  96. public function testUpdateGroup()
  97. {
  98. $group = $this->groupFactory->create()->setId(null)->setCode('New Group')->setTaxClassId(3);
  99. $groupId = $this->groupRepository->save($group)->getId();
  100. $this->assertNotNull($groupId);
  101. $newGroup = $this->groupRepository->getById($groupId);
  102. $this->assertEquals($groupId, $newGroup->getId());
  103. $this->assertEquals($group->getCode(), $newGroup->getCode());
  104. $this->assertEquals($group->getTaxClassId(), $newGroup->getTaxClassId());
  105. $updates = $this->groupFactory->create()->setId($groupId)->setCode('Updated Group')->setTaxClassId(3);
  106. $this->assertNotNull($this->groupRepository->save($updates));
  107. $updatedGroup = $this->groupRepository->getById($groupId);
  108. $this->assertEquals($updates->getCode(), $updatedGroup->getCode(), 'Code not updated.');
  109. $this->assertEquals($updates->getTaxClassId(), $updatedGroup->getTaxClassId(), 'Tax Class should not change.');
  110. }
  111. /**
  112. * @magentoDbIsolation enabled
  113. * @expectedException \Magento\Framework\Exception\InputException
  114. * @expectedExceptionMessage Invalid value of "9999" provided for the taxClassId field.
  115. */
  116. public function testUpdateGroupException()
  117. {
  118. $group = $this->groupFactory->create()->setId(null)->setCode('New Group')->setTaxClassId(3);
  119. $groupId = $this->groupRepository->save($group)->getId();
  120. $this->assertNotNull($groupId);
  121. $newGroup = $this->groupRepository->getById($groupId);
  122. $this->assertEquals($groupId, $newGroup->getId());
  123. $this->assertEquals($group->getCode(), $newGroup->getCode());
  124. $this->assertEquals($group->getTaxClassId(), $newGroup->getTaxClassId());
  125. $updates = $this->groupFactory->create()->setId($groupId)->setCode('Updated Group')->setTaxClassId(9999);
  126. $this->groupRepository->save($updates);
  127. $updatedGroup = $this->groupRepository->getById($groupId);
  128. $this->assertEquals($updates->getCode(), $updatedGroup->getCode());
  129. $this->assertEquals($updates->getTaxClassId(), $updatedGroup->getTaxClassId());
  130. }
  131. /**
  132. * @magentoDbIsolation enabled
  133. */
  134. public function testDelete()
  135. {
  136. $group = $this->groupFactory->create()->setId(null)->setCode('New Group')->setTaxClassId(3);
  137. $groupId = $this->groupRepository->save($group)->getId();
  138. $newGroup = $this->groupRepository->getById($groupId);
  139. $this->assertTrue($this->groupRepository->delete($newGroup));
  140. }
  141. /**
  142. * @magentoDbIsolation enabled
  143. */
  144. public function testDeleteById()
  145. {
  146. $group = $this->groupFactory->create()->setId(null)->setCode('New Group')->setTaxClassId(3);
  147. $groupId = $this->groupRepository->save($group)->getId();
  148. $this->assertTrue($this->groupRepository->deleteById($groupId));
  149. }
  150. /**
  151. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  152. * @expectedExceptionMessage No such entity with id = 9999
  153. */
  154. public function testDeleteDoesNotExist()
  155. {
  156. $this->assertFalse($this->groupRepository->deleteById(9999));
  157. }
  158. public function testGetAllGroups()
  159. {
  160. $searchResults = $this->groupRepository->getList($this->searchCriteriaBuilder->create());
  161. /** @var GroupInterface[] $results */
  162. $results = $searchResults->getItems();
  163. $this->assertEquals(4, count($results));
  164. }
  165. /**
  166. * @param array $filters
  167. * @param array $filterGroup
  168. * @param array $expectedResult array of expected results indexed by ID
  169. *
  170. * @dataProvider searchGroupsDataProvider
  171. */
  172. public function testGetList($filters, $filterGroup, $expectedResult)
  173. {
  174. foreach ($filters as $filter) {
  175. $this->searchCriteriaBuilder->addFilters([$filter]);
  176. }
  177. if ($filterGroup !== null) {
  178. $this->searchCriteriaBuilder->addFilters($filterGroup);
  179. }
  180. $searchResults = $this->groupRepository->getList($this->searchCriteriaBuilder->create());
  181. /** @var $item GroupInterface */
  182. foreach ($searchResults->getItems() as $item) {
  183. $this->assertEquals($expectedResult[$item->getId()][GroupInterface::CODE], $item->getCode());
  184. $this->assertEquals($expectedResult[$item->getId()][GroupInterface::TAX_CLASS_ID], $item->getTaxClassId());
  185. unset($expectedResult[$item->getId()]);
  186. }
  187. }
  188. public function searchGroupsDataProvider()
  189. {
  190. $builder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  191. ->create(\Magento\Framework\Api\FilterBuilder::class);
  192. return [
  193. 'eq' => [
  194. [$builder->setField(GroupInterface::CODE)->setValue('General')->create()],
  195. null,
  196. [1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3]],
  197. ],
  198. 'and' => [
  199. [
  200. $builder->setField(GroupInterface::CODE)->setValue('General')->create(),
  201. $builder->setField(GroupInterface::TAX_CLASS_ID)->setValue('3')->create(),
  202. $builder->setField(GroupInterface::ID)->setValue('1')->create(),
  203. ],
  204. [],
  205. [1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3]],
  206. ],
  207. 'or' => [
  208. [],
  209. [
  210. $builder->setField(GroupInterface::CODE)->setValue('General')->create(),
  211. $builder->setField(GroupInterface::CODE)->setValue('Wholesale')->create(),
  212. ],
  213. [
  214. 1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3],
  215. 2 => [GroupInterface::CODE => 'Wholesale', GroupInterface::TAX_CLASS_ID => 3]
  216. ],
  217. ],
  218. 'like' => [
  219. [
  220. $builder->setField(GroupInterface::CODE)->setValue('er')->setConditionType('like')
  221. ->create(),
  222. ],
  223. [],
  224. [
  225. 1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3],
  226. 3 => [GroupInterface::CODE => 'Retailer', GroupInterface::TAX_CLASS_ID => 3]
  227. ],
  228. ],
  229. 'like_tax_name' => [
  230. [
  231. $builder->setField(GroupInterface::TAX_CLASS_NAME)->setValue('Retail Customer')
  232. ->setConditionType('like')
  233. ->create(),
  234. ],
  235. [],
  236. [
  237. 0 => [GroupInterface::CODE => 'NOT LOGGED IN', GroupInterface::TAX_CLASS_ID => 3],
  238. 1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3],
  239. 2 => [GroupInterface::CODE => 'Wholesale', GroupInterface::TAX_CLASS_ID => 3],
  240. 3 => [GroupInterface::CODE => 'Retailer', GroupInterface::TAX_CLASS_ID => 3],
  241. ],
  242. ],
  243. ];
  244. }
  245. /**
  246. * @param string $field
  247. * @param string, $direction
  248. * @param string, $methodName
  249. * @param array $expectedResult
  250. *
  251. * @dataProvider sortOrderDataProvider
  252. */
  253. public function testGetListSortOrder($field, $direction, $methodName, $expectedResult)
  254. {
  255. /** @var SortOrder $sortOrder */
  256. /** @var string $direction */
  257. $direction = ($direction == 'ASC') ? SortOrder::SORT_ASC : SortOrder::SORT_DESC;
  258. $sortOrder = $this->sortOrderBuilder->setField($field)->setDirection($direction)->create();
  259. $this->searchCriteriaBuilder->addSortOrder($sortOrder);
  260. $searchResults = $this->groupRepository->getList($this->searchCriteriaBuilder->create());
  261. /** @var \Magento\Customer\Api\Data\GroupInterface[] $resultItems */
  262. $resultItems = $searchResults->getItems();
  263. $this->assertTrue(count($resultItems) > 0);
  264. $result = [];
  265. foreach ($resultItems as $item) {
  266. /** @var \Magento\Customer\Model\Data\Group $item */
  267. $result[] = $item->$methodName();
  268. }
  269. $this->assertEquals($expectedResult, $result);
  270. }
  271. /**
  272. * @return array
  273. */
  274. public function sortOrderDataProvider()
  275. {
  276. return [
  277. [
  278. GroupInterface::ID,
  279. 'ASC',
  280. 'getId',
  281. [0, 1, 2, 3],
  282. ],
  283. [
  284. GroupInterface::ID,
  285. 'DESC',
  286. 'getId',
  287. [3, 2, 1, 0],
  288. ],
  289. [
  290. GroupInterface::CODE,
  291. 'ASC',
  292. 'getCode',
  293. ['General', 'NOT LOGGED IN', 'Retailer', 'Wholesale'],
  294. ],
  295. [
  296. GroupInterface::CODE,
  297. 'DESC',
  298. 'getCode',
  299. ['Wholesale', 'Retailer', 'NOT LOGGED IN', 'General'],
  300. ],
  301. [
  302. GroupInterface::TAX_CLASS_NAME,
  303. 'ASC',
  304. 'getTaxClassName',
  305. ['Retail Customer', 'Retail Customer', 'Retail Customer', 'Retail Customer']
  306. ],
  307. [
  308. GroupInterface::TAX_CLASS_NAME,
  309. 'DESC',
  310. 'getTaxClassName',
  311. ['Retail Customer', 'Retail Customer', 'Retail Customer', 'Retail Customer']
  312. ],
  313. ];
  314. }
  315. }