CustomerGroupRetrieverTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model;
  7. use Magento\Backend\Model\Session\Quote;
  8. use Magento\Customer\Api\GroupManagementInterface;
  9. /**
  10. * Test for class CustomerGroupRetriever.
  11. */
  12. class CustomerGroupRetrieverTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Sales\Model\CustomerGroupRetriever
  16. */
  17. private $retriever;
  18. /**
  19. * @var Quote|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $quoteSession;
  22. /**
  23. * @var GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $groupManagement;
  26. /**
  27. * @inheritdoc
  28. */
  29. protected function setUp()
  30. {
  31. $this->quoteSession = $this->getMockBuilder(Quote::class)
  32. ->disableOriginalConstructor()
  33. ->setMethods(['getQuoteId', 'getQuote'])
  34. ->getMock();
  35. $this->groupManagement = $this->getMockBuilder(GroupManagementInterface::class)
  36. ->disableOriginalConstructor()
  37. ->getMockForAbstractClass();
  38. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  39. $this->retriever = $helper->getObject(
  40. \Magento\Sales\Model\CustomerGroupRetriever::class,
  41. [
  42. 'quoteSession' => $this->quoteSession,
  43. 'groupManagement' => $this->groupManagement
  44. ]
  45. );
  46. }
  47. /**
  48. * Test method getCustomerGroupId with quote session.
  49. */
  50. public function testGetCustomerGroupIdQuote()
  51. {
  52. $this->quoteSession->expects($this->atLeastOnce())->method('getQuoteId')->willReturn(1);
  53. $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->quoteSession->expects($this->atLeastOnce())->method('getQuote')->willReturn($quote);
  57. $quote->expects($this->once())->method('getCustomerGroupId')->willReturn(2);
  58. $this->assertEquals(2, $this->retriever->getCustomerGroupId());
  59. }
  60. /**
  61. * Test method getCustomerGroupId without quote session.
  62. */
  63. public function testGetCustomerGroupIdDefault()
  64. {
  65. $this->quoteSession->expects($this->atLeastOnce())->method('getQuoteId')->willReturn(0);
  66. $this->quoteSession->expects($this->never())->method('getQuote');
  67. $group = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMockForAbstractClass();
  70. $this->groupManagement->expects($this->once())->method('getNotLoggedInGroup')->willReturn($group);
  71. $group->expects($this->once())->method('getId')->willReturn(2);
  72. $this->assertEquals(2, $this->retriever->getCustomerGroupId());
  73. }
  74. }