123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Checkout\Test\Unit\Model;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Framework\DB\Adapter\AdapterInterface;
- use Magento\Quote\Model\Quote;
- use Magento\Quote\Model\Quote\Address;
- use Magento\Quote\Model\QuoteIdMask;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class GuestPaymentInformationManagementTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $billingAddressManagementMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $paymentMethodManagementMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $cartManagementMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $cartRepositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteIdMaskFactoryMock;
- /**
- * @var \Magento\Checkout\Model\GuestPaymentInformationManagement
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $loggerMock;
- /**
- * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
- */
- private $resourceConnectionMock;
- protected function setUp()
- {
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->billingAddressManagementMock = $this->createMock(
- \Magento\Quote\Api\GuestBillingAddressManagementInterface::class
- );
- $this->paymentMethodManagementMock = $this->createMock(
- \Magento\Quote\Api\GuestPaymentMethodManagementInterface::class
- );
- $this->cartManagementMock = $this->createMock(\Magento\Quote\Api\GuestCartManagementInterface::class);
- $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
- $this->quoteIdMaskFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\QuoteIdMaskFactory::class,
- ['create']
- );
- $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
- $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = $objectManager->getObject(
- \Magento\Checkout\Model\GuestPaymentInformationManagement::class,
- [
- 'billingAddressManagement' => $this->billingAddressManagementMock,
- 'paymentMethodManagement' => $this->paymentMethodManagementMock,
- 'cartManagement' => $this->cartManagementMock,
- 'cartRepository' => $this->cartRepositoryMock,
- 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
- 'connectionPool' => $this->resourceConnectionMock,
- ]
- );
- $objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
- }
- public function testSavePaymentInformationAndPlaceOrder()
- {
- $cartId = 100;
- $orderId = 200;
- $email = 'email@magento.com';
- $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
- $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->resourceConnectionMock->expects($this->at(0))
- ->method('getConnection')
- ->with('sales')
- ->willReturn($adapterMockForSales);
- $adapterMockForSales->expects($this->once())->method('beginTransaction');
- $adapterMockForSales->expects($this->once())->method('commit');
- $this->resourceConnectionMock->expects($this->at(1))
- ->method('getConnection')
- ->with('checkout')
- ->willReturn($adapterMockForCheckout);
- $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
- $adapterMockForCheckout->expects($this->once())->method('commit');
- $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
- $this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
- $this->assertEquals(
- $orderId,
- $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock)
- );
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testSavePaymentInformationAndPlaceOrderException()
- {
- $cartId = 100;
- $email = 'email@magento.com';
- $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
- $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->resourceConnectionMock->expects($this->at(0))
- ->method('getConnection')
- ->with('sales')
- ->willReturn($adapterMockForSales);
- $adapterMockForSales->expects($this->once())->method('beginTransaction');
- $adapterMockForSales->expects($this->once())->method('rollback');
- $this->resourceConnectionMock->expects($this->at(1))
- ->method('getConnection')
- ->with('checkout')
- ->willReturn($adapterMockForCheckout);
- $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
- $adapterMockForCheckout->expects($this->once())->method('rollback');
- $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
- $exception = new \Magento\Framework\Exception\CouldNotSaveException(__('DB exception'));
- $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
- $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
- $this->expectExceptionMessage(
- 'A server error stopped your order from being placed. Please try to place your order again.'
- );
- }
- public function testSavePaymentInformation()
- {
- $cartId = 100;
- $email = 'email@magento.com';
- $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
- $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
- $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock, $billingAddressMock));
- }
- public function testSavePaymentInformationWithoutBillingAddress()
- {
- $cartId = 100;
- $email = 'email@magento.com';
- $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $quoteMock = $this->createMock(Quote::class);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
- $this->billingAddressManagementMock->expects($this->never())->method('assign');
- $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
- $quoteIdMaskMock = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
- $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($quoteIdMaskMock);
- $quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
- $quoteIdMaskMock->expects($this->once())->method('getQuoteId')->willReturn($cartId);
- $this->cartRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
- $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email);
- $this->assertTrue($this->model->savePaymentInformation($cartId, $email, $paymentMock));
- }
- /**
- * @expectedExceptionMessage DB exception
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
- {
- $cartId = 100;
- $email = 'email@magento.com';
- $paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $quoteMock = $this->createMock(Quote::class);
- $quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
- $this->cartRepositoryMock->method('getActive')->with($cartId)->willReturn($quoteMock);
- $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
- $this->quoteIdMaskFactoryMock->method('create')->willReturn($quoteIdMask);
- $quoteIdMask->method('load')->with($cartId, 'masked_id')->willReturnSelf();
- $quoteIdMask->method('getQuoteId')->willReturn($cartId);
- $billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
- $adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->resourceConnectionMock->expects($this->at(0))
- ->method('getConnection')
- ->with('sales')
- ->willReturn($adapterMockForSales);
- $adapterMockForSales->expects($this->once())->method('beginTransaction');
- $adapterMockForSales->expects($this->once())->method('rollback');
- $this->resourceConnectionMock->expects($this->at(1))
- ->method('getConnection')
- ->with('checkout')
- ->willReturn($adapterMockForCheckout);
- $adapterMockForCheckout->expects($this->once())->method('beginTransaction');
- $adapterMockForCheckout->expects($this->once())->method('rollback');
- $this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
- $phrase = new \Magento\Framework\Phrase(__('DB exception'));
- $exception = new \Magento\Framework\Exception\LocalizedException($phrase);
- $this->loggerMock->expects($this->never())->method('critical');
- $this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
- $this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);
- }
- /**
- * @param int $cartId
- * @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
- * @return void
- */
- private function getMockForAssignBillingAddress(
- int $cartId,
- \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
- ) : void {
- $quoteIdMask = $this->createPartialMock(QuoteIdMask::class, ['getQuoteId', 'load']);
- $this->quoteIdMaskFactoryMock->method('create')
- ->willReturn($quoteIdMask);
- $quoteIdMask->method('load')
- ->with($cartId, 'masked_id')
- ->willReturnSelf();
- $quoteIdMask->method('getQuoteId')
- ->willReturn($cartId);
- $billingAddressId = 1;
- $quote = $this->createMock(Quote::class);
- $quoteBillingAddress = $this->createMock(Address::class);
- $quoteShippingAddress = $this->createPartialMock(
- Address::class,
- ['setLimitCarrier', 'getShippingMethod']
- );
- $this->cartRepositoryMock->method('getActive')
- ->with($cartId)
- ->willReturn($quote);
- $quote->expects($this->once())
- ->method('getBillingAddress')
- ->willReturn($quoteBillingAddress);
- $quote->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($quoteShippingAddress);
- $quoteBillingAddress->expects($this->once())
- ->method('getId')
- ->willReturn($billingAddressId);
- $quote->expects($this->once())
- ->method('removeAddress')
- ->with($billingAddressId);
- $quote->expects($this->once())
- ->method('setBillingAddress')
- ->with($billingAddressMock);
- $quote->expects($this->once())
- ->method('setDataChanges')
- ->willReturnSelf();
- $quoteShippingAddress->method('getShippingMethod')
- ->willReturn('flatrate_flatrate');
- $quoteShippingAddress->expects($this->once())
- ->method('setLimitCarrier')
- ->with('flatrate');
- }
- }
|