RestoreCustomerGroupIdTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Observer\Frontend;
  7. use Magento\Sales\Observer\Frontend\RestoreCustomerGroupId;
  8. /**
  9. * Tests Magento\Sales\Observer\Frontend\RestoreCustomerGroupIdTest
  10. */
  11. class RestoreCustomerGroupIdTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $customerAddressHelperMock;
  17. /**
  18. * @var RestoreCustomerGroupId
  19. */
  20. protected $quote;
  21. protected function setUp()
  22. {
  23. $this->customerAddressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
  24. $this->quote = new RestoreCustomerGroupId($this->customerAddressHelperMock);
  25. }
  26. /**
  27. * @param string|null $configAddressType
  28. * @dataProvider restoreCustomerGroupIdDataProvider
  29. */
  30. public function testExecute($configAddressType)
  31. {
  32. $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getShippingAssignment', 'getQuote']);
  33. $observer = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
  34. $observer->expects($this->exactly(2))->method('getEvent')->willReturn($eventMock);
  35. $shippingAssignmentMock = $this->createMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
  36. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  37. $eventMock->expects($this->once())->method('getShippingAssignment')->willReturn($shippingAssignmentMock);
  38. $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  39. $shippingMock = $this->createMock(\Magento\Quote\Api\Data\ShippingInterface::class);
  40. $shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock);
  41. $quoteAddress = $this->createPartialMock(
  42. \Magento\Quote\Model\Quote\Address::class,
  43. [
  44. 'getPrevQuoteCustomerGroupId',
  45. 'unsPrevQuoteCustomerGroupId',
  46. 'hasPrevQuoteCustomerGroupId',
  47. 'setCustomerGroupId',
  48. 'getQuote'
  49. ]
  50. );
  51. $shippingMock->expects($this->once())->method('getAddress')->willReturn($quoteAddress);
  52. $this->customerAddressHelperMock->expects($this->once())
  53. ->method('getTaxCalculationAddressType')
  54. ->will($this->returnValue($configAddressType));
  55. $quoteAddress->expects($this->once())->method('hasPrevQuoteCustomerGroupId');
  56. $id = $quoteAddress->expects($this->any())->method('getPrevQuoteCustomerGroupId');
  57. $quoteAddress->expects($this->any())->method('setCustomerGroupId')->with($id);
  58. $quoteAddress->expects($this->any())->method('getQuote');
  59. $quoteAddress->expects($this->any())->method('unsPrevQuoteCustomerGroupId');
  60. $this->quote->execute($observer);
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function restoreCustomerGroupIdDataProvider()
  66. {
  67. return [
  68. [\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING],
  69. [null],
  70. [\Magento\Customer\Model\Address\AbstractAddress::TYPE_BILLING],
  71. ];
  72. }
  73. }