GuestCartManagementTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model\GuestCart;
  8. class GuestCartManagementTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $quoteManagementMock;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $quoteRepositoryMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $quoteIdMaskFactoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $quoteIdMaskMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $cartRepositoryMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $quoteMock;
  34. /**
  35. * @var \Magento\Quote\Model\GuestCart\GuestCartManagement
  36. */
  37. protected $guestCartManagement;
  38. protected function setUp()
  39. {
  40. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  41. $this->quoteManagementMock = $this->getMockForAbstractClass(
  42. \Magento\Quote\Api\CartManagementInterface::class,
  43. [],
  44. '',
  45. false,
  46. true,
  47. true,
  48. []
  49. );
  50. $this->quoteIdMaskFactoryMock = $this->createPartialMock(
  51. \Magento\Quote\Model\QuoteIdMaskFactory::class,
  52. ['create']
  53. );
  54. $this->quoteIdMaskMock = $this->createPartialMock(
  55. \Magento\Quote\Model\QuoteIdMask::class,
  56. ['getQuoteId', 'getMaskedId', 'load', 'save', 'setQuoteId']
  57. );
  58. $this->cartRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  59. $this->quoteMock = $this->getMockForAbstractClass(
  60. \Magento\Quote\Api\Data\CartInterface::class,
  61. [],
  62. '',
  63. false,
  64. true,
  65. true,
  66. ['setCheckoutMethod']
  67. );
  68. $this->guestCartManagement = $objectManager->getObject(
  69. \Magento\Quote\Model\GuestCart\GuestCartManagement::class,
  70. [
  71. 'quoteManagement' => $this->quoteManagementMock,
  72. 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
  73. 'cartRepository' => $this->cartRepositoryMock
  74. ]
  75. );
  76. }
  77. public function testCreateEmptyCart()
  78. {
  79. $maskedCartId = 'masked1cart2id3';
  80. $cartId = 1;
  81. $this->quoteIdMaskMock->expects($this->once())->method('setQuoteId')->with($cartId)->willReturnSelf();
  82. $this->quoteIdMaskMock->expects($this->once())->method('save')->willReturnSelf();
  83. $this->quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willReturn($maskedCartId);
  84. $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
  85. $this->quoteManagementMock->expects($this->once())->method('createEmptyCart')->willReturn($cartId);
  86. $this->assertEquals($maskedCartId, $this->guestCartManagement->createEmptyCart());
  87. }
  88. public function testAssignCustomer()
  89. {
  90. $maskedCartId = 'masked1cart2id3';
  91. $cartId = 1;
  92. $customerId = 1;
  93. $storeId = 1;
  94. $this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
  95. $this->quoteIdMaskMock->expects($this->once())->method('getQuoteId')->willReturn($maskedCartId);
  96. $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
  97. $this->quoteManagementMock->expects($this->once())->method('assignCustomer')->willReturn(true);
  98. $this->assertEquals(true, $this->guestCartManagement->assignCustomer($cartId, $customerId, $storeId));
  99. }
  100. public function testPlaceOrder()
  101. {
  102. $maskedCartId = 'masked1cart2id3';
  103. $cartId = 1;
  104. $orderId = 1;
  105. $this->quoteIdMaskMock->expects($this->once())->method('load')->with($cartId, 'masked_id')->willReturnSelf();
  106. $this->cartRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock);
  107. $this->quoteMock->expects($this->once())->method('setCheckoutMethod');
  108. $this->quoteIdMaskMock->expects($this->any())->method('getQuoteId')->willReturn($maskedCartId);
  109. $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
  110. $this->quoteManagementMock->expects($this->once())->method('placeOrder')->willReturn($orderId);
  111. $this->assertEquals($orderId, $this->guestCartManagement->placeOrder($cartId));
  112. }
  113. }