9 use \Magento\Checkout\Helper\Cart;
15 class CartTest extends \PHPUnit\Framework\TestCase
52 protected function setUp()
54 $this->requestMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
55 ->disableOriginalConstructor()->getMock();
56 $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
58 $context = $objectManagerHelper->getObject(
61 'httpRequest' => $this->requestMock,
64 $className = \Magento\Checkout\Helper\Cart::class;
66 $this->urlBuilderMock = $context->getUrlBuilder();
67 $this->urlEncoder = $context->getUrlEncoder();
68 $this->urlEncoder->expects($this->any())
70 ->willReturnCallback(
function (
$url) {
71 return strtr(base64_encode(
$url),
'+/=',
'-_,');
73 $this->scopeConfigMock = $context->getScopeConfig();
75 $this->checkoutSessionMock =
$arguments[
'checkoutSession'];
82 $this->assertEquals($this->cartMock, $this->helper->getCart());
89 $quoteItemMock->expects($this->any())->method(
'getId')->will($this->returnValue(
$quoteItemId));
90 $currentUrl =
'http://www.example.com/';
91 $this->urlBuilderMock->expects($this->any())->method(
'getCurrentUrl')->will($this->returnValue($currentUrl));
96 $this->urlBuilderMock->expects($this->once())->method(
'getUrl')->with(
'checkout/cart/delete',
$params);
97 $this->helper->getRemoveUrl($quoteItemMock);
102 $this->urlBuilderMock->expects($this->once())->method(
'getUrl')->with(
'checkout/cart', []);
103 $this->helper->getCartUrl();
108 $quoteMock = $this->createMock(\
Magento\
Quote\Model\Quote::class);
109 $this->checkoutSessionMock->expects($this->once())->method(
'getQuote')->will($this->returnValue($quoteMock));
110 $this->assertEquals($quoteMock, $this->helper->getQuote());
116 $this->cartMock->expects($this->any())->method(
'getItemsCount')->will($this->returnValue($itemsCount));
117 $this->assertEquals($itemsCount, $this->helper->getItemsCount());
123 $this->cartMock->expects($this->any())->method(
'getItemsQty')->will($this->returnValue($itemsQty));
124 $this->assertEquals($itemsQty, $this->helper->getItemsQty());
130 $this->cartMock->expects($this->any())->method(
'getSummaryQty')->will($this->returnValue($summaryQty));
131 $this->assertEquals($summaryQty, $this->helper->getSummaryCount());
136 $productEntityId = 1;
138 $isRequestSecure =
false;
139 $productMock = $this->createPartialMock(
140 \
Magento\Catalog\Model\Product::class,
141 [
'getEntityId',
'hasUrlDataObject',
'getUrlDataObject',
'__wakeup']
143 $productMock->expects($this->any())->method(
'getEntityId')->will($this->returnValue($productEntityId));
144 $productMock->expects($this->any())->method(
'hasUrlDataObject')->will($this->returnValue(
true));
145 $productMock->expects($this->any())->method(
'getUrlDataObject')
148 $this->requestMock->expects($this->any())->method(
'getRouteName')->will($this->returnValue(
'checkout'));
149 $this->requestMock->expects($this->any())->method(
'getControllerName')->will($this->returnValue(
'cart'));
150 $this->requestMock->expects($this->once())->method(
'isSecure')->willReturn($isRequestSecure);
154 'product' => $productEntityId,
155 'custom_param' =>
'value',
157 '_scope_to_url' =>
true,
159 '_secure' => $isRequestSecure
162 $this->urlBuilderMock->expects($this->once())->method(
'getUrl')->with(
'checkout/cart/add',
$params);
163 $this->helper->getAddUrl($productMock, [
'custom_param' =>
'value',
'useUencPlaceholder' => 1]);
169 $quoteMock = $this->createMock(\
Magento\
Quote\Model\Quote::class);
170 $this->checkoutSessionMock->expects($this->once())->method(
'getQuote')->will($this->returnValue($quoteMock));
171 $quoteMock->expects($this->any())->method(
'isVirtual')->will($this->returnValue($isVirtual));
172 $this->assertEquals($isVirtual, $this->helper->getIsVirtualQuote());
178 $this->scopeConfigMock->expects($this->once())->method(
'isSetFlag')
180 ->will($this->returnValue(
true));
181 $this->assertTrue($this->helper->getShouldRedirectToCart(
$storeId));
186 $productEntityId = 1;
188 $isRequestSecure =
false;
189 $productMock = $this->createPartialMock(
190 \
Magento\Catalog\Model\Product::class,
191 [
'getEntityId',
'hasUrlDataObject',
'getUrlDataObject',
'__wakeup']
193 $productMock->expects($this->any())->method(
'getEntityId')->will($this->returnValue($productEntityId));
194 $productMock->expects($this->any())->method(
'hasUrlDataObject')->will($this->returnValue(
true));
195 $productMock->expects($this->any())->method(
'getUrlDataObject')
198 $currentUrl =
'http://www.example.com/';
199 $this->urlBuilderMock->expects($this->any())->method(
'getCurrentUrl')->will($this->returnValue($currentUrl));
201 $this->requestMock->expects($this->any())->method(
'getRouteName')->will($this->returnValue(
'checkout'));
202 $this->requestMock->expects($this->any())->method(
'getControllerName')->will($this->returnValue(
'cart'));
203 $this->requestMock->expects($this->once())->method(
'isSecure')->willReturn($isRequestSecure);
207 'product' => $productEntityId,
208 'custom_param' =>
'value',
210 '_scope_to_url' =>
true,
212 '_secure' => $isRequestSecure
215 $this->urlBuilderMock->expects($this->once())->method(
'getUrl')->with(
'checkout/cart/add',
$params);
216 $this->helper->getAddUrl($productMock, [
'custom_param' =>
'value']);
232 $item->expects($this->once())
234 ->will($this->returnValue(
$id));
236 $this->requestMock->expects($this->once())
238 ->will($this->returnValue($isAjax));
240 $this->urlBuilderMock->expects($this->any())
241 ->method(
'getCurrentUrl')
242 ->will($this->returnValue(
$url));
244 $this->urlBuilderMock->expects($this->once())
246 ->will($this->returnValue(
$url));
249 $this->assertEquals($expectedPostData,
$result);
257 $url =
'http://localhost.com/dev/checkout/cart/delete/';
258 $uenc = strtr(base64_encode(
$url),
'+/=',
'-_,');
260 $expectedPostData1 = json_encode(
263 'data' => [
'id' =>
$id,
'uenc' => $uenc],
266 $expectedPostData2 = json_encode(
269 'data' => [
'id' =>
$id],
274 [
$id,
$url,
false, $expectedPostData1],
275 [
$id,
$url,
true, $expectedPostData2],
const PARAM_NAME_BASE64_URL
testAddUrlWithUencPlaceholder()
const PARAM_NAME_URL_ENCODED
testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
deletePostJsonDataProvider()
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
testGetShouldRedirectToCart()
const XML_PATH_REDIRECT_TO_CART