CartPluginTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Test\Unit\Model\Cart\Controller;
  7. class CartPluginTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Multishipping\Model\Cart\Controller\CartPlugin
  11. */
  12. private $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $cartRepositoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $checkoutSessionMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $addressRepositoryMock;
  25. protected function setUp()
  26. {
  27. $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  28. $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  29. $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  30. $this->model = new \Magento\Multishipping\Model\Cart\Controller\CartPlugin(
  31. $this->cartRepositoryMock,
  32. $this->checkoutSessionMock,
  33. $this->addressRepositoryMock
  34. );
  35. }
  36. public function testBeforeDispatch()
  37. {
  38. $addressId = 100;
  39. $customerAddressId = 200;
  40. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  41. 'isMultipleShippingAddresses',
  42. 'getAllShippingAddresses',
  43. 'removeAddress',
  44. 'getShippingAddress',
  45. 'getCustomer'
  46. ]);
  47. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  48. $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  49. $addressMock->expects($this->once())->method('getId')->willReturn($addressId);
  50. $quoteMock->expects($this->once())->method('isMultipleShippingAddresses')->willReturn(true);
  51. $quoteMock->expects($this->once())->method('getAllShippingAddresses')->willReturn([$addressMock]);
  52. $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
  53. $shippingAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  54. $quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddressMock);
  55. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  56. $quoteMock->expects($this->once())->method('getCustomer')->willReturn($customerMock);
  57. $customerMock->expects($this->once())->method('getDefaultShipping')->willReturn($customerAddressId);
  58. $customerAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  59. $this->addressRepositoryMock->expects($this->once())
  60. ->method('getById')
  61. ->with($customerAddressId)
  62. ->willReturn($customerAddressMock);
  63. $shippingAddressMock->expects($this->once())
  64. ->method('importCustomerAddressData')
  65. ->with($customerAddressMock)
  66. ->willReturnSelf();
  67. $this->cartRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  68. $this->model->beforeDispatch(
  69. $this->createMock(\Magento\Checkout\Controller\Cart::class),
  70. $this->createMock(\Magento\Framework\App\RequestInterface::class)
  71. );
  72. }
  73. }