CustomerQuoteObserverTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Observer\Backend;
  7. class CustomerQuoteObserverTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Quote\Observer\Backend\CustomerQuoteObserver
  11. */
  12. protected $customerQuote;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
  15. */
  16. protected $storeManagerMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Config\Share
  19. */
  20. protected $configMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Api\CartRepositoryInterface
  23. */
  24. protected $quoteRepositoryMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer
  27. */
  28. protected $observerMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event
  31. */
  32. protected $eventMock;
  33. protected function setUp()
  34. {
  35. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->configMock = $this->getMockBuilder(\Magento\Customer\Model\Config\Share::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  42. $this->observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->eventMock = $this->getMockBuilder(\Magento\Framework\Event::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods(['getCustomerDataObject', 'getOrigCustomerDataObject'])
  48. ->getMock();
  49. $this->observerMock->expects($this->any())->method('getEvent')->will($this->returnValue($this->eventMock));
  50. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $this->customerQuote = $objectManager->getObject(
  52. \Magento\Quote\Observer\Backend\CustomerQuoteObserver::class,
  53. [
  54. 'storeManager' => $this->storeManagerMock,
  55. 'config' => $this->configMock,
  56. 'quoteRepository' => $this->quoteRepositoryMock,
  57. ]
  58. );
  59. }
  60. public function testDispatchNoCustomerGroupChange()
  61. {
  62. $customerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $customerDataObjectMock->expects($this->any())
  66. ->method('getGroupId')
  67. ->will($this->returnValue(1));
  68. $origCustomerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $origCustomerDataObjectMock->expects($this->any())
  72. ->method('getGroupId')
  73. ->will($this->returnValue(1));
  74. $this->eventMock->expects($this->any())
  75. ->method('getCustomerDataObject')
  76. ->will($this->returnValue($customerDataObjectMock));
  77. $this->eventMock->expects($this->any())
  78. ->method('getOrigCustomerDataObject')
  79. ->will($this->returnValue($origCustomerDataObjectMock));
  80. $this->quoteRepositoryMock->expects($this->once())
  81. ->method('getForCustomer')
  82. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
  83. $this->customerQuote->execute($this->observerMock);
  84. }
  85. /**
  86. * @param bool $isWebsiteScope
  87. * @param array $websites
  88. * @dataProvider dispatchDataProvider
  89. */
  90. public function testDispatch($isWebsiteScope, $websites)
  91. {
  92. $this->configMock->expects($this->once())
  93. ->method('isWebsiteScope')
  94. ->will($this->returnValue($isWebsiteScope));
  95. $customerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $customerDataObjectMock->expects($this->any())
  99. ->method('getGroupId')
  100. ->will($this->returnValue(1));
  101. $customerDataObjectMock->expects($this->any())
  102. ->method('getWebsiteId')
  103. ->will($this->returnValue(2));
  104. if ($isWebsiteScope) {
  105. $websites = $websites[0];
  106. $this->storeManagerMock->expects($this->once())
  107. ->method('getWebsite')
  108. ->with(2)
  109. ->will($this->returnValue($websites));
  110. } else {
  111. $this->storeManagerMock->expects($this->once())
  112. ->method('getWebsites')
  113. ->will($this->returnValue($websites));
  114. }
  115. $this->eventMock->expects($this->any())
  116. ->method('getCustomerDataObject')
  117. ->will($this->returnValue($customerDataObjectMock));
  118. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote $quoteMock */
  119. $quoteMock = $this->getMockBuilder(
  120. \Magento\Quote\Model\Quote::class
  121. )->setMethods(
  122. [
  123. 'setWebsite',
  124. 'setCustomerGroupId',
  125. 'getCustomerGroupId',
  126. 'collectTotals',
  127. '__wakeup',
  128. ]
  129. )->disableOriginalConstructor()->getMock();
  130. $websiteCount = count($websites);
  131. $this->quoteRepositoryMock->expects($this->once())
  132. ->method('getForCustomer')
  133. ->will($this->returnValue($quoteMock));
  134. $quoteMock->expects($this->exactly($websiteCount))
  135. ->method('setWebsite');
  136. $quoteMock->expects($this->exactly($websiteCount))
  137. ->method('setCustomerGroupId');
  138. $quoteMock->expects($this->exactly($websiteCount))
  139. ->method('collectTotals');
  140. $this->quoteRepositoryMock->expects($this->exactly($websiteCount))
  141. ->method('save')
  142. ->with($quoteMock);
  143. $quoteMock->expects($this->once())
  144. ->method('getCustomerGroupId')
  145. ->willReturn(2);
  146. $this->customerQuote->execute($this->observerMock);
  147. }
  148. /**
  149. * @return array
  150. */
  151. public function dispatchDataProvider()
  152. {
  153. return [
  154. [true, [['website1']]],
  155. [true, [['website1'], ['website2']]],
  156. [false, ['website1']],
  157. [false, ['website1', 'website2']],
  158. ];
  159. }
  160. }