PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-checkout/Test/Unit/Helper/CartTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 246 lines | 172 code | 35 blank | 39 comment | 0 complexity | 733c93235e2eabaaaad0647296258dec MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Checkout\Test\Unit\Helper;
  8. use \Magento\Checkout\Helper\Cart;
  9. use Magento\Framework\App\Action\Action;
  10. use Magento\Framework\DataObject;
  11. use Magento\Quote\Model\Quote\Item;
  12. class CartTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $urlBuilderMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $requestMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $scopeConfigMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $cartMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $checkoutSessionMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Url\EncoderInterface
  36. */
  37. protected $urlEncoder;
  38. /**
  39. * @var Cart
  40. */
  41. protected $helper;
  42. protected function setUp()
  43. {
  44. $this->requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http')
  45. ->disableOriginalConstructor()->getMock();
  46. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  47. /** @var \Magento\Framework\App\Helper\Context $context */
  48. $context = $objectManagerHelper->getObject(
  49. 'Magento\Framework\App\Helper\Context',
  50. [
  51. 'httpRequest' => $this->requestMock,
  52. ]
  53. );
  54. $className = 'Magento\Checkout\Helper\Cart';
  55. $arguments = $objectManagerHelper->getConstructArguments($className, ['context' => $context]);
  56. $this->urlBuilderMock = $context->getUrlBuilder();
  57. $this->urlEncoder = $context->getUrlEncoder();
  58. $this->urlEncoder->expects($this->any())
  59. ->method('encode')
  60. ->willReturnCallback(function ($url) {
  61. return strtr(base64_encode($url), '+/=', '-_,');
  62. });
  63. $this->scopeConfigMock = $context->getScopeConfig();
  64. $this->cartMock = $arguments['checkoutCart'];
  65. $this->checkoutSessionMock = $arguments['checkoutSession'];
  66. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  67. }
  68. public function testGetCart()
  69. {
  70. $this->assertEquals($this->cartMock, $this->helper->getCart());
  71. }
  72. public function testGetRemoveUrl()
  73. {
  74. $quoteItemId = 1;
  75. $quoteItemMock = $this->getMock('\Magento\Quote\Model\Quote\Item', [], [], '', false);
  76. $quoteItemMock->expects($this->any())->method('getId')->will($this->returnValue($quoteItemId));
  77. $currentUrl = 'http://www.example.com/';
  78. $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
  79. $params = [
  80. 'id' => $quoteItemId,
  81. Action::PARAM_NAME_BASE64_URL => strtr(base64_encode($currentUrl), '+/=', '-_,'),
  82. ];
  83. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/delete', $params);
  84. $this->helper->getRemoveUrl($quoteItemMock);
  85. }
  86. public function testGetCartUrl()
  87. {
  88. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart', []);
  89. $this->helper->getCartUrl();
  90. }
  91. public function testGetQuote()
  92. {
  93. $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
  94. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  95. $this->assertEquals($quoteMock, $this->helper->getQuote());
  96. }
  97. public function testGetItemsCount()
  98. {
  99. $itemsCount = 1;
  100. $this->cartMock->expects($this->any())->method('getItemsCount')->will($this->returnValue($itemsCount));
  101. $this->assertEquals($itemsCount, $this->helper->getItemsCount());
  102. }
  103. public function testGetItemsQty()
  104. {
  105. $itemsQty = 1;
  106. $this->cartMock->expects($this->any())->method('getItemsQty')->will($this->returnValue($itemsQty));
  107. $this->assertEquals($itemsQty, $this->helper->getItemsQty());
  108. }
  109. public function testGetSummaryCount()
  110. {
  111. $summaryQty = 1;
  112. $this->cartMock->expects($this->any())->method('getSummaryQty')->will($this->returnValue($summaryQty));
  113. $this->assertEquals($summaryQty, $this->helper->getSummaryCount());
  114. }
  115. public function testGetIsVirtualQuote()
  116. {
  117. $isVirtual = true;
  118. $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
  119. $this->checkoutSessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  120. $quoteMock->expects($this->any())->method('isVirtual')->will($this->returnValue($isVirtual));
  121. $this->assertEquals($isVirtual, $this->helper->getIsVirtualQuote());
  122. }
  123. public function testGetShouldRedirectToCart()
  124. {
  125. $storeId = 1;
  126. $this->scopeConfigMock->expects($this->once())->method('isSetFlag')
  127. ->with(Cart::XML_PATH_REDIRECT_TO_CART, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
  128. ->will($this->returnValue(true));
  129. $this->assertTrue($this->helper->getShouldRedirectToCart($storeId));
  130. }
  131. public function testGetAddUrl()
  132. {
  133. $productEntityId = 1;
  134. $storeId = 1;
  135. $isRequestSecure = false;
  136. $productMock = $this->getMock('\Magento\Catalog\Model\Product',
  137. ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup'], [], '', false);
  138. $productMock->expects($this->any())->method('getEntityId')->will($this->returnValue($productEntityId));
  139. $productMock->expects($this->any())->method('hasUrlDataObject')->will($this->returnValue(true));
  140. $productMock->expects($this->any())->method('getUrlDataObject')
  141. ->will($this->returnValue(new DataObject(['store_id' => $storeId])));
  142. $currentUrl = 'http://www.example.com/';
  143. $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
  144. $this->requestMock->expects($this->any())->method('getRouteName')->will($this->returnValue('checkout'));
  145. $this->requestMock->expects($this->any())->method('getControllerName')->will($this->returnValue('cart'));
  146. $this->requestMock->expects($this->once())->method('isSecure')->willReturn($isRequestSecure);
  147. $params = [
  148. Action::PARAM_NAME_URL_ENCODED => strtr(base64_encode($currentUrl), '+/=', '-_,'),
  149. 'product' => $productEntityId,
  150. 'custom_param' => 'value',
  151. '_scope' => $storeId,
  152. '_scope_to_url' => true,
  153. 'in_cart' => 1,
  154. '_secure' => $isRequestSecure
  155. ];
  156. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
  157. $this->helper->getAddUrl($productMock, ['custom_param' => 'value']);
  158. }
  159. /**
  160. * @param integer $id
  161. * @param string $url
  162. * @param bool $isAjax
  163. * @param string $expectedPostData
  164. *
  165. * @dataProvider deletePostJsonDataProvider
  166. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  167. */
  168. public function testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
  169. {
  170. $item = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false);
  171. $item->expects($this->once())
  172. ->method('getId')
  173. ->will($this->returnValue($id));
  174. $this->requestMock->expects($this->once())
  175. ->method('isAjax')
  176. ->will($this->returnValue($isAjax));
  177. $this->urlBuilderMock->expects($this->any())
  178. ->method('getCurrentUrl')
  179. ->will($this->returnValue($url));
  180. $this->urlBuilderMock->expects($this->once())
  181. ->method('getUrl')
  182. ->will($this->returnValue($url));
  183. $result = $this->helper->getDeletePostJson($item);
  184. $this->assertEquals($expectedPostData, $result);
  185. }
  186. /**
  187. * @return array
  188. */
  189. public function deletePostJsonDataProvider()
  190. {
  191. $url = 'http://localhost.com/dev/checkout/cart/delete/';
  192. $uenc = strtr(base64_encode($url), '+/=', '-_,');
  193. $id = 1;
  194. $expectedPostData1 = json_encode(
  195. [
  196. 'action' => $url,
  197. 'data' => ['id' => $id, 'uenc' => $uenc],
  198. ]
  199. );
  200. $expectedPostData2 = json_encode(
  201. [
  202. 'action' => $url,
  203. 'data' => ['id' => $id],
  204. ]
  205. );
  206. return [
  207. [$id, $url, false, $expectedPostData1],
  208. [$id, $url, true, $expectedPostData2],
  209. ];
  210. }
  211. }