Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Checkout\Helper\Cart;
10 
14 
15 class CartTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $urlBuilderMock;
21 
25  protected $requestMock;
26 
30  protected $scopeConfigMock;
31 
35  protected $cartMock;
36 
41 
45  protected $urlEncoder;
46 
50  protected $helper;
51 
52  protected function setUp()
53  {
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(
59  \Magento\Framework\App\Helper\Context::class,
60  [
61  'httpRequest' => $this->requestMock,
62  ]
63  );
64  $className = \Magento\Checkout\Helper\Cart::class;
65  $arguments = $objectManagerHelper->getConstructArguments($className, ['context' => $context]);
66  $this->urlBuilderMock = $context->getUrlBuilder();
67  $this->urlEncoder = $context->getUrlEncoder();
68  $this->urlEncoder->expects($this->any())
69  ->method('encode')
70  ->willReturnCallback(function ($url) {
71  return strtr(base64_encode($url), '+/=', '-_,');
72  });
73  $this->scopeConfigMock = $context->getScopeConfig();
74  $this->cartMock = $arguments['checkoutCart'];
75  $this->checkoutSessionMock = $arguments['checkoutSession'];
76 
77  $this->helper = $objectManagerHelper->getObject($className, $arguments);
78  }
79 
80  public function testGetCart()
81  {
82  $this->assertEquals($this->cartMock, $this->helper->getCart());
83  }
84 
85  public function testGetRemoveUrl()
86  {
87  $quoteItemId = 1;
88  $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
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));
92  $params = [
93  'id' => $quoteItemId,
94  Action::PARAM_NAME_BASE64_URL => strtr(base64_encode($currentUrl), '+/=', '-_,'),
95  ];
96  $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/delete', $params);
97  $this->helper->getRemoveUrl($quoteItemMock);
98  }
99 
100  public function testGetCartUrl()
101  {
102  $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart', []);
103  $this->helper->getCartUrl();
104  }
105 
106  public function testGetQuote()
107  {
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());
111  }
112 
113  public function testGetItemsCount()
114  {
115  $itemsCount = 1;
116  $this->cartMock->expects($this->any())->method('getItemsCount')->will($this->returnValue($itemsCount));
117  $this->assertEquals($itemsCount, $this->helper->getItemsCount());
118  }
119 
120  public function testGetItemsQty()
121  {
122  $itemsQty = 1;
123  $this->cartMock->expects($this->any())->method('getItemsQty')->will($this->returnValue($itemsQty));
124  $this->assertEquals($itemsQty, $this->helper->getItemsQty());
125  }
126 
127  public function testGetSummaryCount()
128  {
129  $summaryQty = 1;
130  $this->cartMock->expects($this->any())->method('getSummaryQty')->will($this->returnValue($summaryQty));
131  $this->assertEquals($summaryQty, $this->helper->getSummaryCount());
132  }
133 
135  {
136  $productEntityId = 1;
137  $storeId = 1;
138  $isRequestSecure = false;
139  $productMock = $this->createPartialMock(
140  \Magento\Catalog\Model\Product::class,
141  ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup']
142  );
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')
146  ->will($this->returnValue(new DataObject(['store_id' => $storeId])));
147 
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);
151 
152  $params = [
153  Action::PARAM_NAME_URL_ENCODED => strtr("%uenc%", '+/=', '-_,'),
154  'product' => $productEntityId,
155  'custom_param' => 'value',
156  '_scope' => $storeId,
157  '_scope_to_url' => true,
158  'in_cart' => 1,
159  '_secure' => $isRequestSecure
160  ];
161 
162  $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
163  $this->helper->getAddUrl($productMock, ['custom_param' => 'value', 'useUencPlaceholder' => 1]);
164  }
165 
166  public function testGetIsVirtualQuote()
167  {
168  $isVirtual = true;
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());
173  }
174 
175  public function testGetShouldRedirectToCart()
176  {
177  $storeId = 1;
178  $this->scopeConfigMock->expects($this->once())->method('isSetFlag')
180  ->will($this->returnValue(true));
181  $this->assertTrue($this->helper->getShouldRedirectToCart($storeId));
182  }
183 
184  public function testGetAddUrl()
185  {
186  $productEntityId = 1;
187  $storeId = 1;
188  $isRequestSecure = false;
189  $productMock = $this->createPartialMock(
190  \Magento\Catalog\Model\Product::class,
191  ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup']
192  );
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')
196  ->will($this->returnValue(new DataObject(['store_id' => $storeId])));
197 
198  $currentUrl = 'http://www.example.com/';
199  $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
200 
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);
204 
205  $params = [
206  Action::PARAM_NAME_URL_ENCODED => strtr(base64_encode($currentUrl), '+/=', '-_,'),
207  'product' => $productEntityId,
208  'custom_param' => 'value',
209  '_scope' => $storeId,
210  '_scope_to_url' => true,
211  'in_cart' => 1,
212  '_secure' => $isRequestSecure
213  ];
214 
215  $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
216  $this->helper->getAddUrl($productMock, ['custom_param' => 'value']);
217  }
218 
228  public function testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
229  {
230  $item = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
231 
232  $item->expects($this->once())
233  ->method('getId')
234  ->will($this->returnValue($id));
235 
236  $this->requestMock->expects($this->once())
237  ->method('isAjax')
238  ->will($this->returnValue($isAjax));
239 
240  $this->urlBuilderMock->expects($this->any())
241  ->method('getCurrentUrl')
242  ->will($this->returnValue($url));
243 
244  $this->urlBuilderMock->expects($this->once())
245  ->method('getUrl')
246  ->will($this->returnValue($url));
247 
248  $result = $this->helper->getDeletePostJson($item);
249  $this->assertEquals($expectedPostData, $result);
250  }
251 
255  public function deletePostJsonDataProvider()
256  {
257  $url = 'http://localhost.com/dev/checkout/cart/delete/';
258  $uenc = strtr(base64_encode($url), '+/=', '-_,');
259  $id = 1;
260  $expectedPostData1 = json_encode(
261  [
262  'action' => $url,
263  'data' => ['id' => $id, 'uenc' => $uenc],
264  ]
265  );
266  $expectedPostData2 = json_encode(
267  [
268  'action' => $url,
269  'data' => ['id' => $id],
270  ]
271  );
272 
273  return [
274  [$id, $url, false, $expectedPostData1],
275  [$id, $url, true, $expectedPostData2],
276  ];
277  }
278 }
$id
Definition: fieldset.phtml:14
testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
Definition: CartTest.php:228
$arguments
$quoteItemId
Definition: cart.php:17
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31