Skip to content

Commit 8118353

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-89337' into 2.2.4-develop-pr23
2 parents fe46c9f + 5461965 commit 8118353

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

app/code/Magento/Sales/CustomerData/LastOrderedItems.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
use Magento\Customer\CustomerData\SectionSourceInterface;
99
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Framework\App\ObjectManager;
1013

1114
/**
1215
* Returns information for "Recently Ordered" widget.
1316
* It contains list of 5 salable products from the last placed order.
1417
* Qty of products to display is limited by LastOrderedItems::SIDEBAR_ORDER_LIMIT constant.
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1519
*/
1620
class LastOrderedItems implements SectionSourceInterface
1721
{
@@ -60,28 +64,36 @@ class LastOrderedItems implements SectionSourceInterface
6064
*/
6165
private $productRepository;
6266

67+
/**
68+
* @var LoggerInterface
69+
*/
70+
private $logger;
71+
6372
/**
6473
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
6574
* @param \Magento\Sales\Model\Order\Config $orderConfig
6675
* @param \Magento\Customer\Model\Session $customerSession
6776
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
6877
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
6978
* @param ProductRepositoryInterface $productRepository
79+
* @param LoggerInterface|null $logger
7080
*/
7181
public function __construct(
7282
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
7383
\Magento\Sales\Model\Order\Config $orderConfig,
7484
\Magento\Customer\Model\Session $customerSession,
7585
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
7686
\Magento\Store\Model\StoreManagerInterface $storeManager,
77-
ProductRepositoryInterface $productRepository
87+
ProductRepositoryInterface $productRepository,
88+
LoggerInterface $logger = null
7889
) {
7990
$this->_orderCollectionFactory = $orderCollectionFactory;
8091
$this->_orderConfig = $orderConfig;
8192
$this->_customerSession = $customerSession;
8293
$this->stockRegistry = $stockRegistry;
8394
$this->_storeManager = $storeManager;
8495
$this->productRepository = $productRepository;
96+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
8597
}
8698

8799
/**
@@ -118,12 +130,17 @@ protected function getItems()
118130
/** @var \Magento\Sales\Model\Order\Item $item */
119131
foreach ($order->getParentItemsRandomCollection($limit) as $item) {
120132
/** @var \Magento\Catalog\Model\Product $product */
121-
$product = $this->productRepository->getById(
122-
$item->getProductId(),
123-
false,
124-
$this->_storeManager->getStore()->getId()
125-
);
126-
if ($product && in_array($website, $product->getWebsiteIds())) {
133+
try {
134+
$product = $this->productRepository->getById(
135+
$item->getProductId(),
136+
false,
137+
$this->_storeManager->getStore()->getId()
138+
);
139+
} catch (NoSuchEntityException $noEntityException) {
140+
$this->logger->critical($noEntityException);
141+
continue;
142+
}
143+
if (isset($product) && in_array($website, $product->getWebsiteIds())) {
127144
$url = $product->isVisibleInSiteVisibility() ? $product->getProductUrl() : null;
128145
$items[] = [
129146
'id' => $item->getId(),
@@ -152,7 +169,7 @@ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $or
152169
$orderItem->getStore()->getWebsiteId()
153170
);
154171
return $stockItem->getIsInStock();
155-
} catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
172+
} catch (NoSuchEntityException $noEntityException) {
156173
return false;
157174
}
158175
}

app/code/Magento/Sales/Test/Unit/CustomerData/LastOrderedItemsTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class LastOrderedItemsTest extends \PHPUnit\Framework\TestCase
5858
*/
5959
private $section;
6060

61+
/**
62+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
63+
*/
64+
private $loggerMock;
65+
6166
protected function setUp()
6267
{
6368
$this->objectManagerHelper = new ObjectManagerHelper($this);
@@ -81,13 +86,16 @@ protected function setUp()
8186
->getMock();
8287
$this->productRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
8388
->getMockForAbstractClass();
89+
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
90+
->getMockForAbstractClass();
8491
$this->section = new \Magento\Sales\CustomerData\LastOrderedItems(
8592
$this->orderCollectionFactoryMock,
8693
$this->orderConfigMock,
8794
$this->customerSessionMock,
8895
$this->stockRegistryMock,
8996
$this->storeManagerMock,
90-
$this->productRepository
97+
$this->productRepository,
98+
$this->loggerMock
9199
);
92100
}
93101

@@ -196,4 +204,34 @@ private function getLastOrderMock()
196204
->willReturnSelf();
197205
return $this->orderMock;
198206
}
207+
208+
public function testGetSectionDataWithNotExistingProduct()
209+
{
210+
$storeId = 1;
211+
$websiteId = 4;
212+
$productId = 1;
213+
$exception = new \Magento\Framework\Exception\NoSuchEntityException(__("Product doesn't exist"));
214+
$orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
215+
->disableOriginalConstructor()
216+
->setMethods(['getProductId'])
217+
->getMock();
218+
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
219+
220+
$this->getLastOrderMock();
221+
$this->storeManagerMock->expects($this->exactly(2))->method('getStore')->willReturn($storeMock);
222+
$storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
223+
$storeMock->expects($this->once())->method('getId')->willReturn($storeId);
224+
$this->orderMock->expects($this->once())
225+
->method('getParentItemsRandomCollection')
226+
->with(\Magento\Sales\CustomerData\LastOrderedItems::SIDEBAR_ORDER_LIMIT)
227+
->willReturn([$orderItemMock]);
228+
$orderItemMock->expects($this->once())->method('getProductId')->willReturn($productId);
229+
$this->productRepository->expects($this->once())
230+
->method('getById')
231+
->with($productId, false, $storeId)
232+
->willThrowException($exception);
233+
$this->loggerMock->expects($this->once())->method('critical')->with($exception);
234+
235+
$this->assertEquals(['items' => []], $this->section->getSectionData());
236+
}
199237
}

0 commit comments

Comments
 (0)