SidebarTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Model;
  7. use Magento\Checkout\Model\Sidebar;
  8. class SidebarTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var Sidebar */
  11. protected $sidebar;
  12. /** @var \Magento\Checkout\Model\Cart|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $cartMock;
  14. /** @var \Magento\Checkout\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $checkoutHelperMock;
  16. /** @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $resolverMock;
  18. protected function setUp()
  19. {
  20. $this->cartMock = $this->createMock(\Magento\Checkout\Model\Cart::class);
  21. $this->checkoutHelperMock = $this->createMock(\Magento\Checkout\Helper\Data::class);
  22. $this->resolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
  23. $this->sidebar = new Sidebar(
  24. $this->cartMock,
  25. $this->checkoutHelperMock,
  26. $this->resolverMock
  27. );
  28. }
  29. /**
  30. * @param string $error
  31. * @param array $result
  32. *
  33. * @dataProvider dataProviderGetResponseData
  34. */
  35. public function testGetResponseData($error, $result)
  36. {
  37. $this->assertEquals($result, $this->sidebar->getResponseData($error));
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function dataProviderGetResponseData()
  43. {
  44. return [
  45. [
  46. '',
  47. ['success' => true],
  48. ],
  49. [
  50. '',
  51. ['success' => true],
  52. ],
  53. [
  54. '',
  55. ['success' => true],
  56. ],
  57. [
  58. 'Error',
  59. [
  60. 'success' => false,
  61. 'error_message' => 'Error',
  62. ],
  63. ],
  64. ];
  65. }
  66. public function testCheckQuoteItem()
  67. {
  68. $itemId = 1;
  69. $itemMock = $this->getMockBuilder(\Magento\Quote\Api\Data\CartItemInterface::class)
  70. ->getMock();
  71. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $quoteMock->expects($this->once())
  75. ->method('getItemById')
  76. ->with($itemId)
  77. ->willReturn($itemMock);
  78. $this->cartMock->expects($this->any())
  79. ->method('getQuote')
  80. ->willReturn($quoteMock);
  81. $this->assertEquals($this->sidebar, $this->sidebar->checkQuoteItem($itemId));
  82. }
  83. /**
  84. * @expectedException \Magento\Framework\Exception\LocalizedException
  85. * @expectedExceptionMessage The quote item isn't found. Verify the item and try again.
  86. */
  87. public function testCheckQuoteItemWithException()
  88. {
  89. $itemId = 2;
  90. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $quoteMock->expects($this->once())
  94. ->method('getItemById')
  95. ->with($itemId)
  96. ->willReturn(null);
  97. $this->cartMock->expects($this->any())
  98. ->method('getQuote')
  99. ->willReturn($quoteMock);
  100. $this->sidebar->checkQuoteItem($itemId);
  101. }
  102. public function testRemoveQuoteItem()
  103. {
  104. $itemId = 1;
  105. $this->cartMock->expects($this->once())
  106. ->method('removeItem')
  107. ->with($itemId)
  108. ->willReturnSelf();
  109. $this->cartMock->expects($this->once())
  110. ->method('save')
  111. ->willReturnSelf();
  112. $this->assertEquals($this->sidebar, $this->sidebar->removeQuoteItem($itemId));
  113. }
  114. public function testUpdateQuoteItem()
  115. {
  116. $itemId = 1;
  117. $itemQty = 2;
  118. $this->resolverMock->expects($this->once())
  119. ->method('getLocale')
  120. ->willReturn('en');
  121. $this->cartMock->expects($this->once())
  122. ->method('updateItems')
  123. ->with([$itemId => ['qty' => $itemQty]])
  124. ->willReturnSelf();
  125. $this->cartMock->expects($this->once())
  126. ->method('save')
  127. ->willReturnSelf();
  128. $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty));
  129. }
  130. public function testUpdateQuoteItemWithZeroQty()
  131. {
  132. $itemId = 1;
  133. $itemQty = 0;
  134. $this->resolverMock->expects($this->never())
  135. ->method('getLocale');
  136. $this->cartMock->expects($this->once())
  137. ->method('updateItems')
  138. ->with([$itemId => ['qty' => $itemQty]])
  139. ->willReturnSelf();
  140. $this->cartMock->expects($this->once())
  141. ->method('save')
  142. ->willReturnSelf();
  143. $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty));
  144. }
  145. }