Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingSavedTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class ShippingSavedTest extends \PHPUnit\Framework\TestCase
15 {
19  private $controller;
20 
24  private $contextMock;
25 
29  private $addressRepositoryMock;
30 
34  private $filterBuilderMock;
35 
39  private $criteriaBuilderMock;
40 
44  private $objectManagerMock;
45 
49  private $checkoutMock;
50 
54  private $redirectMock;
55 
56  protected function setUp()
57  {
58  $this->checkoutMock = $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
59  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
60  $this->objectManagerMock->expects($this->any())
61  ->method('get')
62  ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class)
63  ->willReturn($this->checkoutMock);
64  $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
65  $requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
66  $responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
67  $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
68  $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
69  $this->contextMock->expects($this->any())->method('getRequest')->willReturn($requestMock);
70  $this->contextMock->expects($this->any())->method('getResponse')->willReturn($responseMock);
71  $this->contextMock->expects($this->any())->method('getRedirect')->willReturn($this->redirectMock);
72 
73  $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
74  $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
75  $this->criteriaBuilderMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
76  $this->controller = new \Magento\Multishipping\Controller\Checkout\Address\ShippingSaved(
77  $this->contextMock,
78  $this->addressRepositoryMock,
79  $this->filterBuilderMock,
80  $this->criteriaBuilderMock
81  );
82  }
83 
85  {
86  $customerId = 1;
87  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
88  $customerMock->expects($this->any())->method('getId')->willReturn($customerId);
89  $this->checkoutMock->expects($this->any())->method('getCustomer')->willReturn($customerMock);
90 
91  $this->mockCustomerAddressRepository(
93  [$this->createMock(\Magento\Customer\Api\Data\AddressInterface::class)]
94  );
95 
96  // check that checkout is reset
97  $this->checkoutMock->expects($this->once())->method('reset');
98  $this->controller->execute();
99  }
100 
102  {
103  $customerId = 1;
104  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
105  $customerMock->expects($this->any())->method('getId')->willReturn($customerId);
106  $this->checkoutMock->expects($this->any())->method('getCustomer')->willReturn($customerMock);
107 
108  $this->mockCustomerAddressRepository(
109  $customerId,
110  [
111  $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class),
112  $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class),
113  ]
114  );
115 
116  // check that checkout is not reset
117  $this->checkoutMock->expects($this->never())->method('reset');
118  $this->controller->execute();
119  }
120 
127  private function mockCustomerAddressRepository($customerId, array $addresses)
128  {
129  $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
130  $this->filterBuilderMock->expects($this->once())->method('setField')->with('parent_id')->willReturnSelf();
131  $this->filterBuilderMock->expects($this->once())->method('setValue')->with($customerId)->willReturnSelf();
132  $this->filterBuilderMock->expects($this->once())->method('setConditionType')->with('eq')->willReturnSelf();
133  $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
134 
135  $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
136  $this->criteriaBuilderMock->expects($this->once())->method('addFilters')->with([$filterMock])->willReturnSelf();
137  $this->criteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
138 
139  $searchResultMock = $this->createMock(\Magento\Customer\Api\Data\AddressSearchResultsInterface::class);
140  $this->addressRepositoryMock->expects($this->once())
141  ->method('getList')
142  ->with($searchCriteriaMock)
143  ->willReturn($searchResultMock);
144 
145  $searchResultMock->expects($this->once())->method('getItems')->willReturn($addresses);
146  }
147 }
$addresses
Definition: address_list.php:7