GroupRegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. /**
  8. * Test for \Magento\Customer\Model\GroupRegistry
  9. */
  10. class GroupRegistryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * The group code from the fixture data.
  14. */
  15. const GROUP_CODE = 'custom_group';
  16. /**
  17. * @var \Magento\Customer\Model\GroupRegistry
  18. */
  19. protected $_model;
  20. protected function setUp()
  21. {
  22. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  23. ->create(\Magento\Customer\Model\GroupRegistry::class);
  24. }
  25. /**
  26. * Find the group with a given code.
  27. *
  28. * @param string $code
  29. * @return int
  30. */
  31. protected function _findGroupIdWithCode($code)
  32. {
  33. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  34. /** @var \Magento\Customer\Api\GroupRepositoryInterface $groupRepository */
  35. $groupRepository = $objectManager->create(\Magento\Customer\Api\GroupRepositoryInterface::class);
  36. /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
  37. $searchBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  38. foreach ($groupRepository->getList($searchBuilder->create())->getItems() as $group) {
  39. if ($group->getCode() === $code) {
  40. return $group->getId();
  41. }
  42. }
  43. return -1;
  44. }
  45. /**
  46. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  47. */
  48. public function testRetrieve()
  49. {
  50. $groupId = $this->_findGroupIdWithCode(self::GROUP_CODE);
  51. $group = $this->_model->retrieve($groupId);
  52. $this->assertInstanceOf(\Magento\Customer\Model\Group::class, $group);
  53. $this->assertEquals($groupId, $group->getId());
  54. }
  55. /**
  56. * Ensure can retrieve group with id 0 which is a valid group ID.
  57. */
  58. public function testRetrieveGroup0()
  59. {
  60. $groupId = 0;
  61. $group = $this->_model->retrieve($groupId);
  62. $this->assertInstanceOf(\Magento\Customer\Model\Group::class, $group);
  63. $this->assertEquals($groupId, $group->getId());
  64. }
  65. /**
  66. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  67. */
  68. public function testRetrieveCached()
  69. {
  70. $groupId = $this->_findGroupIdWithCode(self::GROUP_CODE);
  71. $groupBeforeDeletion = $this->_model->retrieve($groupId);
  72. $group2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  73. ->create(\Magento\Customer\Model\Group::class);
  74. $group2->load($groupId)
  75. ->delete();
  76. $groupAfterDeletion = $this->_model->retrieve($groupId);
  77. $this->assertEquals($groupBeforeDeletion, $groupAfterDeletion);
  78. $this->assertInstanceOf(\Magento\Customer\Model\Group::class, $groupAfterDeletion);
  79. $this->assertEquals($groupId, $groupAfterDeletion->getId());
  80. }
  81. /**
  82. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  83. */
  84. public function testRetrieveException()
  85. {
  86. $groupId = $this->_findGroupIdWithCode(self::GROUP_CODE);
  87. $this->_model->retrieve($groupId);
  88. }
  89. /**
  90. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  91. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  92. */
  93. public function testRemove()
  94. {
  95. $groupId = $this->_findGroupIdWithCode(self::GROUP_CODE);
  96. $group = $this->_model->retrieve($groupId);
  97. $this->assertInstanceOf(\Magento\Customer\Model\Group::class, $group);
  98. $group->delete();
  99. $this->_model->remove($groupId);
  100. $this->_model->retrieve($groupId);
  101. }
  102. }