GroupRegistryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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;
  7. /**
  8. * Unit test for registry \Magento\Customer\Model\GroupRegistry
  9. */
  10. class GroupRegistryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Customer\Model\GroupRegistry
  14. */
  15. private $unit;
  16. /**
  17. * @var \Magento\Customer\Model\CustomerGroupFactory|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $groupFactory;
  20. protected function setUp()
  21. {
  22. $this->groupFactory = $this->getMockBuilder(\Magento\Customer\Model\GroupFactory::class)
  23. ->disableOriginalConstructor()
  24. ->setMethods(['create'])
  25. ->getMock();
  26. $this->unit = new \Magento\Customer\Model\GroupRegistry($this->groupFactory);
  27. }
  28. /**
  29. * Tests that the same instance is returned from multiple retrieve calls with the same parameter.
  30. *
  31. * @return void
  32. */
  33. public function testRetrieve()
  34. {
  35. $groupId = 1;
  36. $group = $this->getMockBuilder(\Magento\Customer\Model\Group::class)
  37. ->setMethods(['load', 'getId', '__wakeup'])
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $group->expects($this->once())
  41. ->method('load')
  42. ->with($groupId)
  43. ->will($this->returnValue($group));
  44. $group->expects($this->exactly(2))
  45. ->method('getId')
  46. ->will($this->returnValue($groupId));
  47. $this->groupFactory->expects($this->once())
  48. ->method('create')
  49. ->will($this->returnValue($group));
  50. $actual = $this->unit->retrieve($groupId);
  51. $this->assertEquals($group, $actual);
  52. $actualCached = $this->unit->retrieve($groupId);
  53. $this->assertSame($group, $actualCached);
  54. }
  55. /**
  56. * Tests that attempting to retrieve a non-existing entity will result in an exception.
  57. *
  58. * @return void
  59. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  60. */
  61. public function testRetrieveException()
  62. {
  63. $groupId = 1;
  64. $group = $this->getMockBuilder(\Magento\Customer\Model\Group::class)
  65. ->setMethods(['load', 'getId', '__wakeup'])
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $group->expects($this->once())
  69. ->method('load')
  70. ->with($groupId)
  71. ->will($this->returnValue($group));
  72. $group->expects($this->once())
  73. ->method('getId')
  74. ->will($this->returnValue(null));
  75. $this->groupFactory->expects($this->once())
  76. ->method('create')
  77. ->will($this->returnValue($group));
  78. $this->unit->retrieve($groupId);
  79. }
  80. /**
  81. * Tests that an instance removed from the registry will cause the registry to load the model again.
  82. *
  83. * @return void
  84. */
  85. public function testRemove()
  86. {
  87. $groupId = 1;
  88. $group = $this->getMockBuilder(\Magento\Customer\Model\Group::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['load', 'getId', '__wakeup'])
  91. ->getMock();
  92. $group->expects($this->exactly(2))
  93. ->method('load')
  94. ->with($groupId)
  95. ->will($this->returnValue($group));
  96. $group->expects($this->exactly(4))
  97. ->method('getId')
  98. ->will($this->returnValue($groupId));
  99. $this->groupFactory->expects($this->exactly(2))
  100. ->method('create')
  101. ->will($this->returnValue($group));
  102. $actual = $this->unit->retrieve($groupId);
  103. $this->assertSame($group, $actual);
  104. $this->unit->remove($groupId);
  105. $actual = $this->unit->retrieve($groupId);
  106. $this->assertSame($group, $actual);
  107. }
  108. }