Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GuestPaymentInformationManagementTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
19 class GuestPaymentInformationManagementTest extends \PHPUnit\Framework\TestCase
20 {
25 
30 
35 
40 
45 
49  protected $model;
50 
54  private $loggerMock;
55 
59  private $resourceConnectionMock;
60 
61  protected function setUp()
62  {
63  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
64  $this->billingAddressManagementMock = $this->createMock(
65  \Magento\Quote\Api\GuestBillingAddressManagementInterface::class
66  );
67  $this->paymentMethodManagementMock = $this->createMock(
68  \Magento\Quote\Api\GuestPaymentMethodManagementInterface::class
69  );
70  $this->cartManagementMock = $this->createMock(\Magento\Quote\Api\GuestCartManagementInterface::class);
71  $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
72 
73  $this->quoteIdMaskFactoryMock = $this->createPartialMock(
74  \Magento\Quote\Model\QuoteIdMaskFactory::class,
75  ['create']
76  );
77  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
78  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->model = $objectManager->getObject(
83  \Magento\Checkout\Model\GuestPaymentInformationManagement::class,
84  [
85  'billingAddressManagement' => $this->billingAddressManagementMock,
86  'paymentMethodManagement' => $this->paymentMethodManagementMock,
87  'cartManagement' => $this->cartManagementMock,
88  'cartRepository' => $this->cartRepositoryMock,
89  'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
90  'connectionPool' => $this->resourceConnectionMock,
91  ]
92  );
93  $objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
94  }
95 
97  {
98  $cartId = 100;
99  $orderId = 200;
101  $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
102  $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
103  $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
104 
105  $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
106 
107  $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
108  ->disableOriginalConstructor()
109  ->getMockForAbstractClass();
110  $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
111  ->disableOriginalConstructor()
112  ->getMockForAbstractClass();
113 
114  $this->resourceConnectionMock->expects($this->at(0))
115  ->method('getConnection')
116  ->with('sales')
117  ->willReturn($adapterMockForSales);
118  $adapterMockForSales->expects($this->once())->method('beginTransaction');
119  $adapterMockForSales->expects($this->once())->method('commit');
120 
121  $this->resourceConnectionMock->expects($this->at(1))
122  ->method('getConnection')
123  ->with('checkout')
124  ->willReturn($adapterMockForCheckout);
125  $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
126  $adapterMockForCheckout->expects($this->once())->method('commit');
127  $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
128  $this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
129 
130  $this->assertEquals(
131  $orderId,
132  $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock)
133  );
134  }
135 
140  {
141  $cartId = 100;
143  $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
144  $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
145 
146  $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
147  $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
148 
149  $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
150  ->disableOriginalConstructor()
151  ->getMockForAbstractClass();
152  $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
153  ->disableOriginalConstructor()
154  ->getMockForAbstractClass();
155 
156  $this->resourceConnectionMock->expects($this->at(0))
157  ->method('getConnection')
158  ->with('sales')
159  ->willReturn($adapterMockForSales);
160  $adapterMockForSales->expects($this->once())->method('beginTransaction');
161  $adapterMockForSales->expects($this->once())->method('rollback');
162 
163  $this->resourceConnectionMock->expects($this->at(1))
164  ->method('getConnection')
165  ->with('checkout')
166  ->willReturn($adapterMockForCheckout);
167  $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
168  $adapterMockForCheckout->expects($this->once())->method('rollback');
169 
170  $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
171  $exception = new \Magento\Framework\Exception\CouldNotSaveException(__('DB exception'));
172  $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
173 
174  $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
175 
176  $this->expectExceptionMessage(
177  'A server error stopped your order from being placed. Please try to place your order again.'
178  );
179  }
180 
181  public function testSavePaymentInformation()
182  {
183  $cartId = 100;
185  $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
186  $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
187  $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
188  $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
189 
190  $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
191 
192  $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock, $billingAddressMock));
193  }
194 
196  {
197  $cartId = 100;
199  $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
200  $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
201  $quoteMock = $this->createMock(Quote::class);
202 
203  $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
204 
205  $this->billingAddressManagementMock->expects($this->never())->method('assign');
206  $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
207  $quoteIdMaskMock = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
208  $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($quoteIdMaskMock);
209  $quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
210  $quoteIdMaskMock->expects($this->once())->method('getQuoteId')->willReturn($cartId);
211  $this->cartRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
212  $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
213  $billingAddressMock->expects($this->once())->method('setEmail')->with($email);
214  $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock));
215  }
216 
222  {
223  $cartId = 100;
225  $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
226  $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
227 
228  $quoteMock = $this->createMock(Quote::class);
229  $quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
230  $this->cartRepositoryMock->method('getActive')->with($cartId)->willReturn($quoteMock);
231 
232  $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
233  $this->quoteIdMaskFactoryMock->method('create')->willReturn($quoteIdMask);
234  $quoteIdMask->method('load')->with($cartId, 'masked_id')->willReturnSelf();
235  $quoteIdMask->method('getQuoteId')->willReturn($cartId);
236 
237  $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
238 
239  $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
240  ->disableOriginalConstructor()
241  ->getMockForAbstractClass();
242  $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
243  ->disableOriginalConstructor()
244  ->getMockForAbstractClass();
245 
246  $this->resourceConnectionMock->expects($this->at(0))
247  ->method('getConnection')
248  ->with('sales')
249  ->willReturn($adapterMockForSales);
250  $adapterMockForSales->expects($this->once())->method('beginTransaction');
251  $adapterMockForSales->expects($this->once())->method('rollback');
252 
253  $this->resourceConnectionMock->expects($this->at(1))
254  ->method('getConnection')
255  ->with('checkout')
256  ->willReturn($adapterMockForCheckout);
257  $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
258  $adapterMockForCheckout->expects($this->once())->method('rollback');
259 
260  $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
261  $phrase = new \Magento\Framework\Phrase(__('DB exception'));
262  $exception = new \Magento\Framework\Exception\LocalizedException($phrase);
263  $this->loggerMock->expects($this->never())->method('critical');
264  $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
265 
266  $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
267  }
268 
274  private function getMockForAssignBillingAddress(
275  int $cartId,
276  \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
277  ) : void {
278  $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
279  $this->quoteIdMaskFactoryMock->method('create')
280  ->willReturn($quoteIdMask);
281  $quoteIdMask->method('load')
282  ->with($cartId, 'masked_id')
283  ->willReturnSelf();
284  $quoteIdMask->method('getQuoteId')
285  ->willReturn($cartId);
286 
287  $billingAddressId = 1;
288  $quote = $this->createMock(Quote::class);
289  $quoteBillingAddress = $this->createMock(Address::class);
290  $quoteShippingAddress = $this->createPartialMock(
291  Address::class,
292  ['setLimitCarrier', 'getShippingMethod']
293  );
294  $this->cartRepositoryMock->method('getActive')
295  ->with($cartId)
296  ->willReturn($quote);
297  $quote->expects($this->once())
298  ->method('getBillingAddress')
299  ->willReturn($quoteBillingAddress);
300  $quote->expects($this->once())
301  ->method('getShippingAddress')
302  ->willReturn($quoteShippingAddress);
303  $quoteBillingAddress->expects($this->once())
304  ->method('getId')
305  ->willReturn($billingAddressId);
306  $quote->expects($this->once())
307  ->method('removeAddress')
308  ->with($billingAddressId);
309  $quote->expects($this->once())
310  ->method('setBillingAddress')
311  ->with($billingAddressMock);
312  $quote->expects($this->once())
313  ->method('setDataChanges')
314  ->willReturnSelf();
315  $quoteShippingAddress->method('getShippingMethod')
316  ->willReturn('flatrate_flatrate');
317  $quoteShippingAddress->expects($this->once())
318  ->method('setLimitCarrier')
319  ->with('flatrate');
320  }
321 }
$objectManager
Definition: bootstrap.php:17
$email
Definition: details.phtml:13
$quote
__()
Definition: __.php:13
$cartId
Definition: quote.php:22
$quoteShippingAddress