123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Test\Unit\Block\Cart;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class SidebarTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
- protected $_objectManager;
- /**
- * @var \Magento\Checkout\Block\Cart\Sidebar
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $layoutMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlBuilderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $imageHelper;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfigMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $checkoutSessionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $serializer;
- protected function setUp()
- {
- $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
- $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
- $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
- $this->urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $this->imageHelper = $this->createMock(\Magento\Catalog\Helper\Image::class);
- $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $contextMock = $this->createPartialMock(
- \Magento\Framework\View\Element\Template\Context::class,
- ['getLayout', 'getUrlBuilder', 'getStoreManager', 'getScopeConfig', 'getRequest']
- );
- $contextMock->expects($this->once())
- ->method('getLayout')
- ->will($this->returnValue($this->layoutMock));
- $contextMock->expects($this->once())
- ->method('getUrlBuilder')
- ->will($this->returnValue($this->urlBuilderMock));
- $contextMock->expects($this->once())
- ->method('getStoreManager')
- ->will($this->returnValue($this->storeManagerMock));
- $contextMock->expects($this->once())
- ->method('getScopeConfig')
- ->will($this->returnValue($this->scopeConfigMock));
- $contextMock->expects($this->any())
- ->method('getRequest')
- ->will($this->returnValue($this->requestMock));
- $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
- $this->model = $this->_objectManager->getObject(
- \Magento\Checkout\Block\Cart\Sidebar::class,
- [
- 'context' => $contextMock,
- 'imageHelper' => $this->imageHelper,
- 'checkoutSession' => $this->checkoutSessionMock,
- 'serializer' => $this->serializer
- ]
- );
- }
- public function testGetTotalsHtml()
- {
- $totalsHtml = "$134.36";
- $totalsBlockMock = $this->getMockBuilder(\Magento\Checkout\Block\Shipping\Price::class)
- ->disableOriginalConstructor()
- ->setMethods(['toHtml'])
- ->getMock();
- $totalsBlockMock->expects($this->once())
- ->method('toHtml')
- ->will($this->returnValue($totalsHtml));
- $this->layoutMock->expects($this->once())
- ->method('getBlock')
- ->with('checkout.cart.minicart.totals')
- ->will($this->returnValue($totalsBlockMock));
- $this->assertEquals($totalsHtml, $this->model->getTotalsHtml());
- }
- public function testGetConfig()
- {
- $websiteId = 100;
- $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $shoppingCartUrl = 'http://url.com/cart';
- $checkoutUrl = 'http://url.com/checkout';
- $updateItemQtyUrl = 'http://url.com/updateItemQty';
- $removeItemUrl = 'http://url.com/removeItem';
- $baseUrl = 'http://url.com/';
- $imageTemplate = 'Magento_Catalog/product/image_with_borders';
- $expectedResult = [
- 'shoppingCartUrl' => $shoppingCartUrl,
- 'checkoutUrl' => $checkoutUrl,
- 'updateItemQtyUrl' => $updateItemQtyUrl,
- 'removeItemUrl' => $removeItemUrl,
- 'imageTemplate' => $imageTemplate,
- 'baseUrl' => $baseUrl,
- 'minicartMaxItemsVisible' => 3,
- 'websiteId' => 100,
- 'maxItemsToDisplay' => 8,
- 'storeId' => null
- ];
- $valueMap = [
- ['checkout/cart', [], $shoppingCartUrl],
- ['checkout', [], $checkoutUrl],
- ['checkout/sidebar/updateItemQty', ['_secure' => false], $updateItemQtyUrl],
- ['checkout/sidebar/removeItem', ['_secure' => false], $removeItemUrl]
- ];
- $this->requestMock->expects($this->any())
- ->method('isSecure')
- ->willReturn(false);
- $this->urlBuilderMock->expects($this->exactly(4))
- ->method('getUrl')
- ->willReturnMap($valueMap);
- $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
- $storeMock->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl);
- $this->scopeConfigMock->expects($this->at(0))
- ->method('getValue')
- ->with(
- \Magento\Checkout\Block\Cart\Sidebar::XML_PATH_CHECKOUT_SIDEBAR_COUNT,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )->willReturn(3);
- $this->scopeConfigMock->expects($this->at(1))
- ->method('getValue')
- ->with(
- 'checkout/sidebar/max_items_display_count',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )->willReturn(8);
- $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
- $this->assertEquals($expectedResult, $this->model->getConfig());
- }
- public function testGetIsNeedToDisplaySideBar()
- {
- $this->scopeConfigMock->expects($this->once())
- ->method('getValue')
- ->with(
- \Magento\Checkout\Block\Cart\Sidebar::XML_PATH_CHECKOUT_SIDEBAR_DISPLAY,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- )->willReturn(true);
- $this->assertTrue($this->model->getIsNeedToDisplaySideBar());
- }
- public function testGetTotalsCache()
- {
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $totalsMock = ['totals'];
- $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
- $quoteMock->expects($this->once())->method('getTotals')->willReturn($totalsMock);
- $this->assertEquals($totalsMock, $this->model->getTotalsCache());
- }
- }
|