GuestCartItemRepositoryTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 GuestCartItemRepositoryTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Quote\Model\GuestCart\GuestCartItemRepository
  12. */
  13. protected $guestCartItemRepository;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $cartItemRepositoryMock;
  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 $quoteItemMock;
  30. /**
  31. * @var string
  32. */
  33. protected $maskedCartId;
  34. /**
  35. * @var string
  36. */
  37. protected $cartId;
  38. /**
  39. * @return void
  40. */
  41. protected function setUp()
  42. {
  43. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  44. $this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
  45. $this->cartId = 33;
  46. /**
  47. * @var \Magento\Quote\Test\Unit\Model\GuestCart\GuestCartTestHelper
  48. */
  49. $guestCartTestHelper = new \Magento\Quote\Test\Unit\Model\GuestCart\GuestCartTestHelper($this);
  50. list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) =
  51. $guestCartTestHelper->mockQuoteIdMask(
  52. $this->maskedCartId,
  53. $this->cartId
  54. );
  55. $this->quoteIdMaskMock->expects($this->any())
  56. ->method('getMaskedId')
  57. ->willReturn($this->maskedCartId);
  58. $this->quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  59. $this->quoteItemMock->expects($this->any())
  60. ->method('getItemId')
  61. ->willReturn($this->maskedCartId);
  62. $this->quoteItemMock->expects($this->any())
  63. ->method('getQuoteId')
  64. ->willReturn($this->maskedCartId);
  65. $this->quoteItemMock->expects($this->any())
  66. ->method('setQuoteId')
  67. ->with($this->cartId);
  68. $this->cartItemRepositoryMock = $this->createMock(\Magento\Quote\Api\CartItemRepositoryInterface::class);
  69. $this->guestCartItemRepository =
  70. $objectManager->getObject(
  71. \Magento\Quote\Model\GuestCart\GuestCartItemRepository::class,
  72. [
  73. 'repository' => $this->cartItemRepositoryMock,
  74. 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
  75. ]
  76. );
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testSave()
  82. {
  83. $expectedValue = 'expected value';
  84. $this->cartItemRepositoryMock->expects($this->once())
  85. ->method('save')
  86. ->willReturn($expectedValue);
  87. $this->assertEquals($expectedValue, $this->guestCartItemRepository->save($this->quoteItemMock));
  88. }
  89. /**
  90. * @return void
  91. */
  92. public function testGetList()
  93. {
  94. $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  95. $itemMock->expects($this->any())
  96. ->method('setQuoteId')
  97. ->with($this->maskedCartId);
  98. $this->cartItemRepositoryMock->expects($this->once())
  99. ->method('getList')
  100. ->with($this->cartId)
  101. ->will($this->returnValue([$itemMock]));
  102. $this->assertEquals([$itemMock], $this->guestCartItemRepository->getList($this->maskedCartId));
  103. }
  104. /**
  105. * @return void
  106. */
  107. public function testDeleteById()
  108. {
  109. $itemId = 5;
  110. $this->cartItemRepositoryMock->expects($this->once())
  111. ->method('deleteById')
  112. ->with($this->cartId, $itemId)
  113. ->willReturn(true);
  114. $this->assertTrue($this->guestCartItemRepository->deleteById($this->maskedCartId, $itemId));
  115. }
  116. }