Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartPluginTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CartPluginTest extends \PHPUnit\Framework\TestCase
9 {
13  private $model;
14 
18  private $cartRepositoryMock;
19 
23  private $checkoutSessionMock;
24 
28  private $addressRepositoryMock;
29 
30  protected function setUp()
31  {
32  $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
33  $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
34  $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
35  $this->model = new \Magento\Multishipping\Model\Cart\Controller\CartPlugin(
36  $this->cartRepositoryMock,
37  $this->checkoutSessionMock,
38  $this->addressRepositoryMock
39  );
40  }
41 
42  public function testBeforeDispatch()
43  {
44  $addressId = 100;
45  $customerAddressId = 200;
46  $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
47  'isMultipleShippingAddresses',
48  'getAllShippingAddresses',
49  'removeAddress',
50  'getShippingAddress',
51  'getCustomer'
52  ]);
53  $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
54 
55  $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
56  $addressMock->expects($this->once())->method('getId')->willReturn($addressId);
57 
58  $quoteMock->expects($this->once())->method('isMultipleShippingAddresses')->willReturn(true);
59  $quoteMock->expects($this->once())->method('getAllShippingAddresses')->willReturn([$addressMock]);
60  $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
61 
62  $shippingAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
63  $quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddressMock);
64  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
65  $quoteMock->expects($this->once())->method('getCustomer')->willReturn($customerMock);
66  $customerMock->expects($this->once())->method('getDefaultShipping')->willReturn($customerAddressId);
67 
68  $customerAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
69  $this->addressRepositoryMock->expects($this->once())
70  ->method('getById')
71  ->with($customerAddressId)
72  ->willReturn($customerAddressMock);
73 
74  $shippingAddressMock->expects($this->once())
75  ->method('importCustomerAddressData')
76  ->with($customerAddressMock)
77  ->willReturnSelf();
78 
79  $this->cartRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
80 
81  $this->model->beforeDispatch(
82  $this->createMock(\Magento\Checkout\Controller\Cart::class),
83  $this->createMock(\Magento\Framework\App\RequestInterface::class)
84  );
85  }
86 }