SidebarTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Block\Cart;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class SidebarTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  13. protected $_objectManager;
  14. /**
  15. * @var \Magento\Checkout\Block\Cart\Sidebar
  16. */
  17. protected $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $layoutMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $urlBuilderMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $storeManagerMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $imageHelper;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $scopeConfigMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $checkoutSessionMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $requestMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $serializer;
  50. protected function setUp()
  51. {
  52. $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  53. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  54. $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  55. $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  56. $this->urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  57. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  58. $this->imageHelper = $this->createMock(\Magento\Catalog\Helper\Image::class);
  59. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  60. $contextMock = $this->createPartialMock(
  61. \Magento\Framework\View\Element\Template\Context::class,
  62. ['getLayout', 'getUrlBuilder', 'getStoreManager', 'getScopeConfig', 'getRequest']
  63. );
  64. $contextMock->expects($this->once())
  65. ->method('getLayout')
  66. ->will($this->returnValue($this->layoutMock));
  67. $contextMock->expects($this->once())
  68. ->method('getUrlBuilder')
  69. ->will($this->returnValue($this->urlBuilderMock));
  70. $contextMock->expects($this->once())
  71. ->method('getStoreManager')
  72. ->will($this->returnValue($this->storeManagerMock));
  73. $contextMock->expects($this->once())
  74. ->method('getScopeConfig')
  75. ->will($this->returnValue($this->scopeConfigMock));
  76. $contextMock->expects($this->any())
  77. ->method('getRequest')
  78. ->will($this->returnValue($this->requestMock));
  79. $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
  80. $this->model = $this->_objectManager->getObject(
  81. \Magento\Checkout\Block\Cart\Sidebar::class,
  82. [
  83. 'context' => $contextMock,
  84. 'imageHelper' => $this->imageHelper,
  85. 'checkoutSession' => $this->checkoutSessionMock,
  86. 'serializer' => $this->serializer
  87. ]
  88. );
  89. }
  90. public function testGetTotalsHtml()
  91. {
  92. $totalsHtml = "$134.36";
  93. $totalsBlockMock = $this->getMockBuilder(\Magento\Checkout\Block\Shipping\Price::class)
  94. ->disableOriginalConstructor()
  95. ->setMethods(['toHtml'])
  96. ->getMock();
  97. $totalsBlockMock->expects($this->once())
  98. ->method('toHtml')
  99. ->will($this->returnValue($totalsHtml));
  100. $this->layoutMock->expects($this->once())
  101. ->method('getBlock')
  102. ->with('checkout.cart.minicart.totals')
  103. ->will($this->returnValue($totalsBlockMock));
  104. $this->assertEquals($totalsHtml, $this->model->getTotalsHtml());
  105. }
  106. public function testGetConfig()
  107. {
  108. $websiteId = 100;
  109. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  110. $shoppingCartUrl = 'http://url.com/cart';
  111. $checkoutUrl = 'http://url.com/checkout';
  112. $updateItemQtyUrl = 'http://url.com/updateItemQty';
  113. $removeItemUrl = 'http://url.com/removeItem';
  114. $baseUrl = 'http://url.com/';
  115. $imageTemplate = 'Magento_Catalog/product/image_with_borders';
  116. $expectedResult = [
  117. 'shoppingCartUrl' => $shoppingCartUrl,
  118. 'checkoutUrl' => $checkoutUrl,
  119. 'updateItemQtyUrl' => $updateItemQtyUrl,
  120. 'removeItemUrl' => $removeItemUrl,
  121. 'imageTemplate' => $imageTemplate,
  122. 'baseUrl' => $baseUrl,
  123. 'minicartMaxItemsVisible' => 3,
  124. 'websiteId' => 100,
  125. 'maxItemsToDisplay' => 8,
  126. 'storeId' => null
  127. ];
  128. $valueMap = [
  129. ['checkout/cart', [], $shoppingCartUrl],
  130. ['checkout', [], $checkoutUrl],
  131. ['checkout/sidebar/updateItemQty', ['_secure' => false], $updateItemQtyUrl],
  132. ['checkout/sidebar/removeItem', ['_secure' => false], $removeItemUrl]
  133. ];
  134. $this->requestMock->expects($this->any())
  135. ->method('isSecure')
  136. ->willReturn(false);
  137. $this->urlBuilderMock->expects($this->exactly(4))
  138. ->method('getUrl')
  139. ->willReturnMap($valueMap);
  140. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
  141. $storeMock->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl);
  142. $this->scopeConfigMock->expects($this->at(0))
  143. ->method('getValue')
  144. ->with(
  145. \Magento\Checkout\Block\Cart\Sidebar::XML_PATH_CHECKOUT_SIDEBAR_COUNT,
  146. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  147. )->willReturn(3);
  148. $this->scopeConfigMock->expects($this->at(1))
  149. ->method('getValue')
  150. ->with(
  151. 'checkout/sidebar/max_items_display_count',
  152. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  153. )->willReturn(8);
  154. $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
  155. $this->assertEquals($expectedResult, $this->model->getConfig());
  156. }
  157. public function testGetIsNeedToDisplaySideBar()
  158. {
  159. $this->scopeConfigMock->expects($this->once())
  160. ->method('getValue')
  161. ->with(
  162. \Magento\Checkout\Block\Cart\Sidebar::XML_PATH_CHECKOUT_SIDEBAR_DISPLAY,
  163. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  164. )->willReturn(true);
  165. $this->assertTrue($this->model->getIsNeedToDisplaySideBar());
  166. }
  167. public function testGetTotalsCache()
  168. {
  169. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  170. $totalsMock = ['totals'];
  171. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  172. $quoteMock->expects($this->once())->method('getTotals')->willReturn($totalsMock);
  173. $this->assertEquals($totalsMock, $this->model->getTotalsCache());
  174. }
  175. }