Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteUpdaterTest.php
Go to the documentation of this file.
1 <?php
7 
16 use Magento\Quote\Api\Data\CartExtensionInterface;
17 
25 class QuoteUpdaterTest extends \PHPUnit\Framework\TestCase
26 {
27  const TEST_NONCE = '3ede7045-2aea-463e-9754-cd658ffeeb48';
28 
32  private $configMock;
33 
37  private $quoteRepositoryMock;
38 
42  private $billingAddressMock;
43 
47  private $shippingAddressMock;
48 
52  private $quoteUpdater;
53 
57  protected function setUp()
58  {
59  $this->configMock = $this->getMockBuilder(Config::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $this->quoteRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
63  ->getMockForAbstractClass();
64 
65  $this->billingAddressMock = $this->getMockBuilder(Address::class)
66  ->setMethods(
67  [
68  'setLastname',
69  'setFirstname',
70  'setEmail',
71  'setCollectShippingRates',
72  'setStreet',
73  'setCity',
74  'setRegionCode',
75  'setCountryId',
76  'setPostcode',
77  'setShouldIgnoreValidation',
78  'getEmail'
79  ]
80  )->disableOriginalConstructor()
81  ->getMock();
82  $this->shippingAddressMock = $this->getMockBuilder(Address::class)
83  ->setMethods(
84  [
85  'setLastname',
86  'setFirstname',
87  'setEmail',
88  'setCollectShippingRates',
89  'setStreet',
90  'setCity',
91  'setRegionCode',
92  'setCountryId',
93  'setPostcode',
94  'setShouldIgnoreValidation'
95  ]
96  )->disableOriginalConstructor()
97  ->getMock();
98 
99  $this->quoteUpdater = new QuoteUpdater(
100  $this->configMock,
101  $this->quoteRepositoryMock
102  );
103  }
104 
109  public function testExecute()
110  {
111  $details = $this->getDetails();
112  $quoteMock = $this->getQuoteMock();
113  $paymentMock = $this->getPaymentMock();
114 
115  $quoteMock->expects(self::once())
116  ->method('getPayment')
117  ->willReturn($paymentMock);
118 
119  $paymentMock->expects(self::once())
120  ->method('setMethod')
122  $paymentMock->expects(self::once())
123  ->method('setAdditionalInformation')
124  ->with(DataAssignObserver::PAYMENT_METHOD_NONCE, self::TEST_NONCE);
125 
126  $this->updateQuoteStep($quoteMock, $details);
127 
128  $this->quoteUpdater->execute(self::TEST_NONCE, $details, $quoteMock);
129  }
130 
134  private function disabledQuoteAddressValidationStep()
135  {
136  $this->billingAddressMock->expects(self::once())
137  ->method('setShouldIgnoreValidation')
138  ->with(true);
139  $this->shippingAddressMock->expects(self::once())
140  ->method('setShouldIgnoreValidation')
141  ->with(true);
142  $this->billingAddressMock->expects(self::once())
143  ->method('getEmail')
144  ->willReturn('[email protected]');
145  }
146 
150  private function getDetails()
151  {
152  return [
153  'email' => '[email protected]',
154  'payerId' => 'FAKE_PAYER_ID',
155  'firstName' => 'John',
156  'lastName' => 'Doe',
157  'phone' => '312-123-4567',
158  'countryCode' => 'US',
159  'shippingAddress' => [
160  'streetAddress' => '123 Division Street',
161  'extendedAddress' => 'Apt. #1',
162  'locality' => 'Chicago',
163  'region' => 'IL',
164  'postalCode' => '60618',
165  'countryCodeAlpha2' => 'US',
166  'recipientName' => 'John Doe',
167  ],
168  'billingAddress' => [
169  'streetAddress' => '123 Billing Street',
170  'extendedAddress' => 'Apt. #1',
171  'locality' => 'Chicago',
172  'region' => 'IL',
173  'postalCode' => '60618',
174  'countryCodeAlpha2' => 'US',
175  ],
176  ];
177  }
178 
182  private function updateShippingAddressStep(array $details)
183  {
184  $this->shippingAddressMock->expects(self::once())
185  ->method('setLastname')
186  ->with($details['lastName']);
187  $this->shippingAddressMock->expects(self::once())
188  ->method('setFirstname')
189  ->with($details['firstName']);
190  $this->shippingAddressMock->expects(self::once())
191  ->method('setEmail')
192  ->with($details['email']);
193  $this->shippingAddressMock->expects(self::once())
194  ->method('setCollectShippingRates')
195  ->with(true);
196 
197  $this->updateAddressDataStep($this->shippingAddressMock, $details['shippingAddress']);
198  }
199 
204  private function updateAddressDataStep(\PHPUnit_Framework_MockObject_MockObject $addressMock, array $addressData)
205  {
206  $addressMock->expects(self::once())
207  ->method('setStreet')
208  ->with([$addressData['streetAddress'], $addressData['extendedAddress']]);
209  $addressMock->expects(self::once())
210  ->method('setCity')
211  ->with($addressData['locality']);
212  $addressMock->expects(self::once())
213  ->method('setRegionCode')
214  ->with($addressData['region']);
215  $addressMock->expects(self::once())
216  ->method('setCountryId')
217  ->with($addressData['countryCodeAlpha2']);
218  $addressMock->expects(self::once())
219  ->method('setPostcode')
220  ->with($addressData['postalCode']);
221  }
222 
227  private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
228  {
229  $quoteMock->expects(self::exactly(2))
230  ->method('getIsVirtual')
231  ->willReturn(false);
232 
233  $this->updateShippingAddressStep($details);
234  $this->updateBillingAddressStep($details);
235  }
236 
240  private function updateBillingAddressStep(array $details)
241  {
242  $this->configMock->expects(self::once())
243  ->method('isRequiredBillingAddress')
244  ->willReturn(true);
245 
246  $this->updateAddressDataStep($this->billingAddressMock, $details['billingAddress']);
247 
248  $this->billingAddressMock->expects(self::once())
249  ->method('setLastname')
250  ->with($details['lastName']);
251  $this->billingAddressMock->expects(self::once())
252  ->method('setFirstname')
253  ->with($details['firstName']);
254  $this->billingAddressMock->expects(self::once())
255  ->method('setEmail')
256  ->with($details['email']);
257  }
258 
263  private function updateQuoteStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
264  {
265  $quoteMock->expects(self::once())
266  ->method('setMayEditShippingAddress')
267  ->with(false);
268  $quoteMock->expects(self::once())
269  ->method('setMayEditShippingMethod')
270  ->with(true);
271 
272  $quoteMock->expects(self::exactly(2))
273  ->method('getShippingAddress')
274  ->willReturn($this->shippingAddressMock);
275  $quoteMock->expects(self::exactly(2))
276  ->method('getBillingAddress')
277  ->willReturn($this->billingAddressMock);
278 
279  $this->updateQuoteAddressStep($quoteMock, $details);
280  $this->disabledQuoteAddressValidationStep();
281 
282  $quoteMock->expects(self::once())
283  ->method('collectTotals');
284 
285  $this->quoteRepositoryMock->expects(self::once())
286  ->method('save')
287  ->with($quoteMock);
288  }
289 
293  private function getQuoteMock()
294  {
295  $quoteMock = $this->getMockBuilder(Quote::class)
296  ->setMethods(
297  [
298  'getIsVirtual',
299  'getPayment',
300  'setMayEditShippingAddress',
301  'setMayEditShippingMethod',
302  'collectTotals',
303  'getShippingAddress',
304  'getBillingAddress',
305  'getExtensionAttributes'
306  ]
307  )->disableOriginalConstructor()
308  ->getMock();
309 
310  $cartExtensionMock = $this->getMockBuilder(CartExtensionInterface::class)
311  ->setMethods(['setShippingAssignments'])
312  ->disableOriginalConstructor()
313  ->getMockForAbstractClass();
314 
315  $quoteMock->expects(self::any())
316  ->method('getExtensionAttributes')
317  ->willReturn($cartExtensionMock);
318 
319  return $quoteMock;
320  }
321 
325  private function getPaymentMock()
326  {
327  return $this->getMockBuilder(Payment::class)
328  ->disableOriginalConstructor()
329  ->getMock();
330  }
331 }
$details
Definition: vault.phtml:10
$addressData
Definition: order.php:19