Skip to content

Commit f0ae4fb

Browse files
committed
AC-2457: Checkout Improvements
1 parent f605b71 commit f0ae4fb

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
99
use Magento\Checkout\Model\Sidebar;
10-
use Magento\Framework\App\Action\Action;
11-
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Action\HttpPostActionInterface;
1211
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\RequestInterface;
1313
use Magento\Framework\App\Response\Http;
14+
use Magento\Framework\App\ResponseInterface;
15+
use Magento\Framework\Exception\InputException;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\Json\Helper\Data;
1618
use Psr\Log\LoggerInterface;
1719

18-
class UpdateItemQty extends Action
20+
class UpdateItemQty implements HttpPostActionInterface
1921
{
2022
/**
2123
* @var Sidebar
@@ -38,38 +40,57 @@ class UpdateItemQty extends Action
3840
private $quantityProcessor;
3941

4042
/**
41-
* @param Context $context
43+
* @var RequestInterface
44+
*/
45+
private $request;
46+
47+
/**
48+
* @var ResponseInterface
49+
*/
50+
private $response;
51+
52+
/**
4253
* @param Sidebar $sidebar
4354
* @param LoggerInterface $logger
4455
* @param Data $jsonHelper
56+
* @param RequestInterface $request
57+
* @param ResponseInterface $response
4558
* @param RequestQuantityProcessor|null $quantityProcessor
4659
* @codeCoverageIgnore
4760
*/
4861
public function __construct(
49-
Context $context,
5062
Sidebar $sidebar,
5163
LoggerInterface $logger,
5264
Data $jsonHelper,
65+
RequestInterface $request,
66+
ResponseInterface $response,
5367
?RequestQuantityProcessor $quantityProcessor = null
5468
) {
5569
$this->sidebar = $sidebar;
5670
$this->logger = $logger;
5771
$this->jsonHelper = $jsonHelper;
58-
parent::__construct($context);
72+
$this->request = $request;
73+
$this->response = $response;
5974
$this->quantityProcessor = $quantityProcessor
6075
?? ObjectManager::getInstance()->get(RequestQuantityProcessor::class);
6176
}
6277

6378
/**
79+
* Action for Quantity update
80+
*
6481
* @return $this
6582
*/
6683
public function execute()
6784
{
68-
$itemId = (int)$this->getRequest()->getParam('item_id');
69-
$itemQty = $this->getRequest()->getParam('item_qty') * 1;
70-
$itemQty = $this->quantityProcessor->prepareQuantity($itemQty);
85+
$itemId = (int)$this->request->getParam('item_id');
86+
$itemQty = $this->request->getParam('item_qty');
7187

88+
if (!is_numeric($itemQty) || ($itemQty <=0)) {
89+
$e = new InputException(__('A non-numeric value found')) ;
90+
return $this->jsonResponse($e->getMessage());
91+
}
7292
try {
93+
$itemQty = $this->quantityProcessor->prepareQuantity($itemQty*1);
7394
$this->sidebar->checkQuoteItem($itemId);
7495
$this->sidebar->updateQuoteItem($itemId, $itemQty);
7596
return $this->jsonResponse();
@@ -89,7 +110,7 @@ public function execute()
89110
*/
90111
protected function jsonResponse($error = '')
91112
{
92-
return $this->getResponse()->representJson(
113+
return $this->response->representJson(
93114
$this->jsonHelper->jsonEncode($this->sidebar->getResponseData($error))
94115
);
95116
}

app/code/Magento/Checkout/Test/Unit/Controller/Sidebar/UpdateItemQtyTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,42 @@ public function testExecuteWithException(): void
244244

245245
$this->assertEquals('json represented', $this->updateItemQty->execute());
246246
}
247+
248+
/**
249+
* @return void
250+
*/
251+
public function testExecuteWithInputException(): void
252+
{
253+
$this->requestMock
254+
->method('getParam')
255+
->withConsecutive(['item_id', null], ['item_qty', null])
256+
->willReturnOnConsecutiveCalls('1', '{{7+2}}');
257+
258+
$this->sidebarMock->expects($this->once())
259+
->method('getResponseData')
260+
->with('A non-numeric value found')
261+
->willReturn(
262+
[
263+
'success' => false,
264+
'error_message' => 'A non-numeric value found'
265+
]
266+
);
267+
268+
$this->jsonHelperMock->expects($this->once())
269+
->method('jsonEncode')
270+
->with(
271+
[
272+
'success' => false,
273+
'error_message' => 'A non-numeric value found'
274+
]
275+
)
276+
->willReturn('json encoded');
277+
278+
$this->responseMock->expects($this->once())
279+
->method('representJson')
280+
->with('json encoded')
281+
->willReturn('json represented');
282+
283+
$this->assertEquals('json represented', $this->updateItemQty->execute());
284+
}
247285
}

0 commit comments

Comments
 (0)