Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingAddressManagementTest.php
Go to the documentation of this file.
1 <?php
9 
10 use \Magento\Quote\Model\ShippingAddressManagement;
11 
15 class ShippingAddressManagementTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $service;
21 
26 
30  protected $quoteAddressMock;
31 
35  protected $validatorMock;
36 
40  protected $scopeConfigMock;
41 
45  protected $objectManager;
46 
51 
55  private $addressRepository;
56 
60  private $amountErrorMessageMock;
61 
62  protected function setUp()
63  {
64  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
65  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
66  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
67 
68  $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
69  'setSameAsBilling',
70  'setCollectShippingRates',
71  '__wakeup',
72  'collectTotals',
73  'save',
74  'getId',
75  'getCustomerAddressId',
76  'getSaveInAddressBook',
77  'getSameAsBilling',
78  'importCustomerAddressData',
79  'setSaveInAddressBook',
80  ]);
81  $this->validatorMock = $this->createMock(\Magento\Quote\Model\QuoteAddressValidator::class);
82  $this->totalsCollectorMock = $this->createMock(\Magento\Quote\Model\Quote\TotalsCollector::class);
83  $this->addressRepository = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
84 
85  $this->amountErrorMessageMock = $this->createPartialMock(
86  \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class,
87  ['getMessage']
88  );
89 
90  $this->service = $this->objectManager->getObject(
91  \Magento\Quote\Model\ShippingAddressManagement::class,
92  [
93  'quoteRepository' => $this->quoteRepositoryMock,
94  'addressValidator' => $this->validatorMock,
95  'logger' => $this->createMock(\Psr\Log\LoggerInterface::class),
96  'scopeConfig' => $this->scopeConfigMock,
97  'totalsCollector' => $this->totalsCollectorMock,
98  'addressRepository' => $this->addressRepository
99  ]
100  );
101  }
102 
108  {
109  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
110  $this->quoteRepositoryMock->expects($this->once())
111  ->method('getActive')
112  ->with('cart654')
113  ->will($this->returnValue($quoteMock));
114 
115  $this->validatorMock->expects($this->once())->method('validate')
116  ->will($this->throwException(new \Magento\Framework\Exception\NoSuchEntityException(__('error345'))));
117 
118  $this->service->assign('cart654', $this->quoteAddressMock);
119  }
120 
121  public function testSetAddress()
122  {
123  $addressId = 1;
124  $customerAddressId = 150;
125 
126  $quoteMock = $this->createPartialMock(
127  \Magento\Quote\Model\Quote::class,
128  ['getIsMultiShipping', 'isVirtual', 'validateMinimumAmount', 'setShippingAddress', 'getShippingAddress']
129  );
130  $this->quoteRepositoryMock->expects($this->once())
131  ->method('getActive')
132  ->with('cart867')
133  ->willReturn($quoteMock);
134  $quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false));
135  $quoteMock->expects($this->once())
136  ->method('setShippingAddress')
137  ->with($this->quoteAddressMock)
138  ->willReturnSelf();
139 
140  $this->quoteAddressMock->expects($this->once())->method('getSaveInAddressBook')->willReturn(1);
141  $this->quoteAddressMock->expects($this->once())->method('getSameAsBilling')->willReturn(1);
142  $this->quoteAddressMock->expects($this->once())->method('getCustomerAddressId')->willReturn($customerAddressId);
143 
144  $customerAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
145 
146  $this->addressRepository->expects($this->once())
147  ->method('getById')
148  ->with($customerAddressId)
149  ->willReturn($customerAddressMock);
150 
151  $this->validatorMock->expects($this->once())->method('validate')
152  ->with($this->quoteAddressMock)
153  ->willReturn(true);
154 
155  $quoteMock->expects($this->exactly(3))->method('getShippingAddress')->willReturn($this->quoteAddressMock);
156  $this->quoteAddressMock->expects($this->once())
157  ->method('importCustomerAddressData')
158  ->with($customerAddressMock)
159  ->willReturnSelf();
160 
161  $this->quoteAddressMock->expects($this->once())->method('setSameAsBilling')->with(1)->willReturnSelf();
162  $this->quoteAddressMock->expects($this->once())->method('setSaveInAddressBook')->with(1)->willReturnSelf();
163  $this->quoteAddressMock->expects($this->once())
164  ->method('setCollectShippingRates')
165  ->with(true)
166  ->willReturnSelf();
167 
168  $this->quoteAddressMock->expects($this->once())->method('save')->willReturnSelf();
169  $this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId));
170 
171  $this->assertEquals($addressId, $this->service->assign('cart867', $this->quoteAddressMock));
172  }
173 
179  {
180  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
181  $this->quoteRepositoryMock->expects($this->once())
182  ->method('getActive')
183  ->with('cart867')
184  ->will($this->returnValue($quoteMock));
185  $quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(true));
186  $quoteMock->expects($this->never())->method('setShippingAddress');
187 
188  $this->quoteAddressMock->expects($this->never())->method('getCustomerAddressId');
189  $this->quoteAddressMock->expects($this->never())->method('setSaveInAddressBook');
190 
191  $quoteMock->expects($this->never())->method('save');
192 
193  $this->service->assign('cart867', $this->quoteAddressMock);
194  }
195 
201  {
202  $this->quoteAddressMock->expects($this->once())->method('save')->willThrowException(
203  new \Exception('The address failed to save. Verify the address and try again.')
204  );
205 
206  $customerAddressId = 150;
207 
208  $quoteMock = $this->createPartialMock(
209  \Magento\Quote\Model\Quote::class,
210  ['getIsMultiShipping', 'isVirtual', 'validateMinimumAmount', 'setShippingAddress', 'getShippingAddress']
211  );
212  $this->quoteRepositoryMock->expects($this->once())
213  ->method('getActive')
214  ->with('cart867')
215  ->willReturn($quoteMock);
216  $quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false));
217  $quoteMock->expects($this->once())
218  ->method('setShippingAddress')
219  ->with($this->quoteAddressMock)
220  ->willReturnSelf();
221 
222  $customerAddressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
223 
224  $this->addressRepository->expects($this->once())
225  ->method('getById')
226  ->with($customerAddressId)
227  ->willReturn($customerAddressMock);
228 
229  $this->validatorMock->expects($this->once())->method('validate')
230  ->with($this->quoteAddressMock)
231  ->willReturn(true);
232 
233  $this->quoteAddressMock->expects($this->once())->method('getSaveInAddressBook')->willReturn(1);
234  $this->quoteAddressMock->expects($this->once())->method('getSameAsBilling')->willReturn(1);
235  $this->quoteAddressMock->expects($this->once())->method('getCustomerAddressId')->willReturn($customerAddressId);
236 
237  $quoteMock->expects($this->exactly(2))->method('getShippingAddress')->willReturn($this->quoteAddressMock);
238  $this->quoteAddressMock->expects($this->once())
239  ->method('importCustomerAddressData')
240  ->with($customerAddressMock)
241  ->willReturnSelf();
242 
243  $this->quoteAddressMock->expects($this->once())->method('setSameAsBilling')->with(1)->willReturnSelf();
244  $this->quoteAddressMock->expects($this->once())->method('setSaveInAddressBook')->with(1)->willReturnSelf();
245  $this->quoteAddressMock->expects($this->once())
246  ->method('setCollectShippingRates')
247  ->with(true)
248  ->willReturnSelf();
249 
250  $this->service->assign('cart867', $this->quoteAddressMock);
251  }
252 
253  public function testGetAddress()
254  {
255  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
256  $this->quoteRepositoryMock->expects($this->once())->method('getActive')->with('cartId')->will(
257  $this->returnValue($quoteMock)
258  );
259 
260  $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
261  $quoteMock->expects($this->any())->method('getShippingAddress')->will($this->returnValue($addressMock));
262  $quoteMock->expects($this->any())->method('isVirtual')->will($this->returnValue(false));
263  $this->assertEquals($addressMock, $this->service->get('cartId'));
264  }
265 
271  {
272  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
273  $this->quoteRepositoryMock->expects($this->once())->method('getActive')->with('cartId')->will(
274  $this->returnValue($quoteMock)
275  );
276 
277  $quoteMock->expects($this->any())->method('isVirtual')->will($this->returnValue(true));
278  $quoteMock->expects($this->never())->method('getShippingAddress');
279 
280  $this->service->get('cartId');
281  }
282 }
__()
Definition: __.php:13