Skip to content

Commit 3de597c

Browse files
committed
MAGETWO-21349: Advanced Mini Cart
1 parent 6844fc7 commit 3de597c

File tree

5 files changed

+703
-18
lines changed

5 files changed

+703
-18
lines changed

app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\Response\Http;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Json\Helper\Data;
14+
use Magento\Framework\View\Result\PageFactory;
1415
use Psr\Log\LoggerInterface;
1516

1617
class RemoveItem extends Action
@@ -30,15 +31,22 @@ class RemoveItem extends Action
3031
*/
3132
protected $jsonHelper;
3233

34+
/**
35+
* @var PageFactory
36+
*/
37+
protected $resultPageFactory;
38+
3339
public function __construct(
3440
Context $context,
3541
Sidebar $sidebar,
3642
LoggerInterface $logger,
37-
Data $jsonHelper
43+
Data $jsonHelper,
44+
PageFactory $resultPageFactory
3845
) {
3946
$this->sidebar = $sidebar;
4047
$this->logger = $logger;
4148
$this->jsonHelper = $jsonHelper;
49+
$this->resultPageFactory = $resultPageFactory;
4250
parent::__construct($context);
4351
}
4452

@@ -70,9 +78,8 @@ protected function jsonResponse($error = '')
7078
{
7179
$response = $this->sidebar->getResponseData($error);
7280
if (isset($response['cleanup']) && (bool)$response['cleanup']) {
73-
$this->_view->loadLayout(['default'], true, true, false);
74-
$layout = $this->_view->getLayout();
75-
$block = $layout->getBlock('minicart.content')->toHtml();
81+
$resultPage = $this->resultPageFactory->create();
82+
$block = $resultPage->getLayout()->getBlock('minicart.content')->toHtml();
7683
$response['content'] = $block;
7784
}
7885
return $this->getResponse()->representJson(

app/code/Magento/Checkout/Model/Sidebar.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Locale\ResolverInterface;
1212
use Magento\Quote\Api\Data\CartItemInterface;
13+
use Magento\Quote\Model\Quote\Address\Total;
1314

1415
class Sidebar
1516
{
@@ -56,25 +57,23 @@ public function __construct(
5657
*/
5758
public function getResponseData($error = '')
5859
{
59-
$response = [
60-
'success' => empty($error) ? true : false,
61-
];
62-
if ($response['success']) {
63-
if (!$this->getSummaryQty()) {
64-
$response['cleanup'] = true;
65-
}
66-
$response = array_merge($response, [
60+
if (empty($error)) {
61+
$response = [
62+
'success' => true,
6763
'data' => [
6864
'summary_qty' => $this->getSummaryQty(),
6965
'summary_text' => $this->getSummaryText(),
7066
'subtotal' => $this->getSubtotalHtml(),
7167
],
72-
]);
73-
}
74-
if (!empty($error)){
75-
$response = array_merge($response, [
68+
];
69+
if (!$this->getSummaryQty()) {
70+
$response['cleanup'] = true;
71+
}
72+
} else {
73+
$response = [
74+
'success' => false,
7675
'error_message' => $error,
77-
]);
76+
];
7877
}
7978
return $response;
8079
}
@@ -171,7 +170,9 @@ protected function getSummaryText()
171170
protected function getSubtotalHtml()
172171
{
173172
$totals = $this->cart->getQuote()->getTotals();
174-
$subtotal = isset($totals['subtotal']) ? $totals['subtotal']->getValue() : 0;
173+
$subtotal = isset($totals['subtotal']) && $totals['subtotal'] instanceof Total
174+
? $totals['subtotal']->getValue()
175+
: 0;
175176
return $this->helperData->formatPrice($subtotal);
176177
}
177178
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Checkout\Test\Unit\Controller\Sidebar;
7+
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
11+
class RemoveItemTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var \Magento\Checkout\Controller\Sidebar\RemoveItem */
14+
protected $removeItem;
15+
16+
/** @var ObjectManagerHelper */
17+
protected $objectManagerHelper;
18+
19+
/** @var \Magento\Checkout\Model\Sidebar|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $sidebarMock;
21+
22+
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $loggerMock;
24+
25+
/** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $jsonHelperMock;
27+
28+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
protected $requestMock;
30+
31+
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $responseMock;
33+
34+
/** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
35+
protected $resultPageFactoryMock;
36+
37+
protected function setUp()
38+
{
39+
$this->sidebarMock = $this->getMock('Magento\Checkout\Model\Sidebar', [], [], '', false);
40+
$this->loggerMock = $this->getMock('Psr\Log\LoggerInterface');
41+
$this->jsonHelperMock = $this->getMock('Magento\Framework\Json\Helper\Data', [], [], '', false);
42+
$this->requestMock = $this->getMock('Magento\Framework\App\RequestInterface');
43+
$this->responseMock = $this->getMockForAbstractClass(
44+
'Magento\Framework\App\ResponseInterface',
45+
[],
46+
'',
47+
false,
48+
true,
49+
true,
50+
['representJson']
51+
);
52+
$this->resultPageFactoryMock = $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false);
53+
54+
$this->objectManagerHelper = new ObjectManagerHelper($this);
55+
$this->removeItem = $this->objectManagerHelper->getObject(
56+
'Magento\Checkout\Controller\Sidebar\RemoveItem',
57+
[
58+
'sidebar' => $this->sidebarMock,
59+
'logger' => $this->loggerMock,
60+
'jsonHelper' => $this->jsonHelperMock,
61+
'request' => $this->requestMock,
62+
'response' => $this->responseMock,
63+
'resultPageFactory' => $this->resultPageFactoryMock,
64+
]
65+
);
66+
}
67+
68+
public function testExecute()
69+
{
70+
$this->requestMock->expects($this->once())
71+
->method('getParam')
72+
->with('item_id', null)
73+
->willReturn('1');
74+
75+
$this->sidebarMock->expects($this->once())
76+
->method('checkQuoteItem')
77+
->with(1)
78+
->willReturnSelf();
79+
$this->sidebarMock->expects($this->once())
80+
->method('removeQuoteItem')
81+
->with(1)
82+
->willReturnSelf();
83+
$this->sidebarMock->expects($this->once())
84+
->method('getResponseData')
85+
->with('')
86+
->willReturn(
87+
[
88+
'cleanup' => true,
89+
'data' => [
90+
'summary_qty' => 0,
91+
'summary_text' => __(' items'),
92+
'subtotal' => 0,
93+
],
94+
]
95+
);
96+
97+
$pageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page')
98+
->disableOriginalConstructor()
99+
->getMock();
100+
101+
$this->resultPageFactoryMock->expects($this->once())
102+
->method('create')
103+
->with(false, [])
104+
->willReturn($pageMock);
105+
106+
$layoutMock = $this->getMockBuilder('Magento\Framework\View\LayoutInterface')
107+
->getMock();
108+
109+
$pageMock->expects($this->once())
110+
->method('getLayout')
111+
->willReturn($layoutMock);
112+
113+
$blockMock = $this->getMockBuilder('Magento\Framework\View\Element\BlockInterface')
114+
->getMock();
115+
116+
$layoutMock->expects($this->once())
117+
->method('getBlock')
118+
->with('minicart.content')
119+
->willReturn($blockMock);
120+
121+
$blockMock->expects($this->once())
122+
->method('toHtml')
123+
->willReturn('block html');
124+
125+
$this->jsonHelperMock->expects($this->once())
126+
->method('jsonEncode')
127+
->with(
128+
[
129+
'cleanup' => true,
130+
'data' => [
131+
'summary_qty' => 0,
132+
'summary_text' => __(' items'),
133+
'subtotal' => 0,
134+
],
135+
'content' => 'block html',
136+
]
137+
)
138+
->willReturn('json encoded');
139+
140+
$this->responseMock->expects($this->once())
141+
->method('representJson')
142+
->with('json encoded')
143+
->willReturn('json represented');
144+
145+
$this->assertEquals('json represented', $this->removeItem->execute());
146+
}
147+
148+
public function testExecuteWithLocalizedException()
149+
{
150+
$this->requestMock->expects($this->once())
151+
->method('getParam')
152+
->with('item_id', null)
153+
->willReturn('1');
154+
155+
$this->sidebarMock->expects($this->once())
156+
->method('checkQuoteItem')
157+
->with(1)
158+
->willThrowException(new LocalizedException(__('Error message!')));
159+
160+
$this->sidebarMock->expects($this->once())
161+
->method('getResponseData')
162+
->with('Error message!')
163+
->willReturn(
164+
[
165+
'success' => false,
166+
'error_message' => 'Error message!',
167+
]
168+
);
169+
170+
$this->jsonHelperMock->expects($this->once())
171+
->method('jsonEncode')
172+
->with(
173+
[
174+
'success' => false,
175+
'error_message' => 'Error message!',
176+
]
177+
)
178+
->willReturn('json encoded');
179+
180+
$this->responseMock->expects($this->once())
181+
->method('representJson')
182+
->with('json encoded')
183+
->willReturn('json represented');
184+
185+
$this->assertEquals('json represented', $this->removeItem->execute());
186+
}
187+
188+
public function testExecuteWithException()
189+
{
190+
$this->requestMock->expects($this->once())
191+
->method('getParam')
192+
->with('item_id', null)
193+
->willReturn('1');
194+
195+
$exception = new \Exception('Error message!');
196+
197+
$this->sidebarMock->expects($this->once())
198+
->method('checkQuoteItem')
199+
->with(1)
200+
->willThrowException($exception);
201+
202+
$this->loggerMock->expects($this->once())
203+
->method('critical')
204+
->with($exception)
205+
->willReturn(null);
206+
207+
$this->sidebarMock->expects($this->once())
208+
->method('getResponseData')
209+
->with('Error message!')
210+
->willReturn(
211+
[
212+
'success' => false,
213+
'error_message' => 'Error message!',
214+
]
215+
);
216+
217+
$this->jsonHelperMock->expects($this->once())
218+
->method('jsonEncode')
219+
->with(
220+
[
221+
'success' => false,
222+
'error_message' => 'Error message!',
223+
]
224+
)
225+
->willReturn('json encoded');
226+
227+
$this->responseMock->expects($this->once())
228+
->method('representJson')
229+
->with('json encoded')
230+
->willReturn('json represented');
231+
232+
$this->assertEquals('json represented', $this->removeItem->execute());
233+
}
234+
}

0 commit comments

Comments
 (0)