6 declare(strict_types=1);
59 private $resourceConnectionMock;
63 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
64 $this->billingAddressManagementMock = $this->createMock(
65 \
Magento\
Quote\Api\GuestBillingAddressManagementInterface::class
67 $this->paymentMethodManagementMock = $this->createMock(
68 \
Magento\
Quote\Api\GuestPaymentMethodManagementInterface::class
70 $this->cartManagementMock = $this->createMock(\
Magento\
Quote\Api\GuestCartManagementInterface::class);
71 $this->cartRepositoryMock = $this->createMock(\
Magento\
Quote\Api\CartRepositoryInterface::class);
73 $this->quoteIdMaskFactoryMock = $this->createPartialMock(
77 $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
78 $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
79 ->disableOriginalConstructor()
83 \
Magento\Checkout\Model\GuestPaymentInformationManagement::class,
85 'billingAddressManagement' => $this->billingAddressManagementMock,
86 'paymentMethodManagement' => $this->paymentMethodManagementMock,
87 'cartManagement' => $this->cartManagementMock,
88 'cartRepository' => $this->cartRepositoryMock,
89 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
90 'connectionPool' => $this->resourceConnectionMock,
93 $objectManager->setBackwardCompatibleProperty($this->model,
'logger', $this->loggerMock);
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);
105 $billingAddressMock->expects($this->once())->method(
'setEmail')->with(
$email)->willReturnSelf();
107 $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
108 ->disableOriginalConstructor()
109 ->getMockForAbstractClass();
110 $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
111 ->disableOriginalConstructor()
112 ->getMockForAbstractClass();
114 $this->resourceConnectionMock->expects($this->at(0))
115 ->method(
'getConnection')
117 ->willReturn($adapterMockForSales);
118 $adapterMockForSales->expects($this->once())->method(
'beginTransaction');
119 $adapterMockForSales->expects($this->once())->method(
'commit');
121 $this->resourceConnectionMock->expects($this->at(1))
122 ->method(
'getConnection')
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);
132 $this->model->savePaymentInformationAndPlaceOrder(
$cartId,
$email, $paymentMock, $billingAddressMock)
143 $paymentMock = $this->createMock(\
Magento\
Quote\Api\Data\PaymentInterface::class);
144 $billingAddressMock = $this->createMock(\
Magento\
Quote\Api\Data\AddressInterface::class);
146 $this->getMockForAssignBillingAddress(
$cartId, $billingAddressMock);
147 $billingAddressMock->expects($this->once())->method(
'setEmail')->with(
$email)->willReturnSelf();
149 $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
150 ->disableOriginalConstructor()
151 ->getMockForAbstractClass();
152 $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
153 ->disableOriginalConstructor()
154 ->getMockForAbstractClass();
156 $this->resourceConnectionMock->expects($this->at(0))
157 ->method(
'getConnection')
159 ->willReturn($adapterMockForSales);
160 $adapterMockForSales->expects($this->once())->method(
'beginTransaction');
161 $adapterMockForSales->expects($this->once())->method(
'rollback');
163 $this->resourceConnectionMock->expects($this->at(1))
164 ->method(
'getConnection')
166 ->willReturn($adapterMockForCheckout);
167 $adapterMockForCheckout->expects($this->once())->method(
'beginTransaction');
168 $adapterMockForCheckout->expects($this->once())->method(
'rollback');
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);
174 $this->model->savePaymentInformationAndPlaceOrder(
$cartId,
$email, $paymentMock, $billingAddressMock);
176 $this->expectExceptionMessage(
177 'A server error stopped your order from being placed. Please try to place your order again.' 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();
190 $this->paymentMethodManagementMock->expects($this->once())->method(
'set')->with(
$cartId, $paymentMock);
192 $this->assertTrue($this->model->savePaymentInformation(
$cartId,
$email, $paymentMock, $billingAddressMock));
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);
203 $billingAddressMock->expects($this->once())->method(
'setEmail')->with(
$email)->willReturnSelf();
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));
225 $paymentMock = $this->createMock(\
Magento\
Quote\Api\Data\PaymentInterface::class);
226 $billingAddressMock = $this->createMock(\
Magento\
Quote\Api\Data\AddressInterface::class);
228 $quoteMock = $this->createMock(Quote::class);
229 $quoteMock->method(
'getBillingAddress')->willReturn($billingAddressMock);
230 $this->cartRepositoryMock->method(
'getActive')->with(
$cartId)->willReturn($quoteMock);
232 $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, [
'getQuoteId',
'load']);
233 $this->quoteIdMaskFactoryMock->method(
'create')->willReturn(
$quoteIdMask);
237 $billingAddressMock->expects($this->once())->method(
'setEmail')->with(
$email)->willReturnSelf();
239 $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
240 ->disableOriginalConstructor()
241 ->getMockForAbstractClass();
242 $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
243 ->disableOriginalConstructor()
244 ->getMockForAbstractClass();
246 $this->resourceConnectionMock->expects($this->at(0))
247 ->method(
'getConnection')
249 ->willReturn($adapterMockForSales);
250 $adapterMockForSales->expects($this->once())->method(
'beginTransaction');
251 $adapterMockForSales->expects($this->once())->method(
'rollback');
253 $this->resourceConnectionMock->expects($this->at(1))
254 ->method(
'getConnection')
256 ->willReturn($adapterMockForCheckout);
257 $adapterMockForCheckout->expects($this->once())->method(
'beginTransaction');
258 $adapterMockForCheckout->expects($this->once())->method(
'rollback');
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);
266 $this->model->savePaymentInformationAndPlaceOrder(
$cartId,
$email, $paymentMock, $billingAddressMock);
274 private function getMockForAssignBillingAddress(
276 \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
278 $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, [
'getQuoteId',
'load']);
279 $this->quoteIdMaskFactoryMock->method(
'create')
287 $billingAddressId = 1;
288 $quote = $this->createMock(Quote::class);
289 $quoteBillingAddress = $this->createMock(Address::class);
292 [
'setLimitCarrier',
'getShippingMethod']
294 $this->cartRepositoryMock->method(
'getActive')
297 $quote->expects($this->once())
298 ->method(
'getBillingAddress')
299 ->willReturn($quoteBillingAddress);
300 $quote->expects($this->once())
301 ->method(
'getShippingAddress')
303 $quoteBillingAddress->expects($this->once())
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')
316 ->willReturn(
'flatrate_flatrate');
318 ->method(
'setLimitCarrier')