1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Test\Unit\CustomerData;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LastOrderedItemsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $orderCollectionFactoryMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $orderConfigMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $customerSessionMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $stockRegistryMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;
/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $orderMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $productRepositoryMock;
/**
* @var \Magento\Sales\CustomerData\LastOrderedItems
*/
private $section;
/**
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $loggerMock;
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->orderCollectionFactoryMock =
$this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->orderConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Config::class)
->disableOriginalConstructor()
->getMock();
$this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
->disableOriginalConstructor()
->getMock();
$this->stockRegistryMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockRegistryInterface::class)
->getMockForAbstractClass();
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
->getMockForAbstractClass();
$this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
->disableOriginalConstructor()
->getMock();
$this->productRepositoryMock = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
->getMockForAbstractClass();
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
->getMockForAbstractClass();
$this->section = new \Magento\Sales\CustomerData\LastOrderedItems(
$this->orderCollectionFactoryMock,
$this->orderConfigMock,
$this->customerSessionMock,
$this->stockRegistryMock,
$this->storeManagerMock,
$this->productRepositoryMock,
$this->loggerMock
);
}
public function testGetSectionData()
{
$storeId = 1;
$websiteId = 4;
$expectedItem1 = [
'id' => 1,
'name' => 'Product Name 1',
'url' => 'http://example.com',
'is_saleable' => true,
];
$expectedItem2 = [
'id' => 2,
'name' => 'Product Name 2',
'url' => null,
'is_saleable' => true,
];
$productIdVisible = 1;
$productIdNotVisible = 2;
$stockItemMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
->getMockForAbstractClass();
$itemWithVisibleProduct = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
->disableOriginalConstructor()
->getMock();
$itemWithNotVisibleProduct = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
->disableOriginalConstructor()
->getMock();
$productVisible = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$productNotVisible = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$items = [$itemWithVisibleProduct, $itemWithNotVisibleProduct];
$this->getLastOrderMock();
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn($websiteId);
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
$this->orderMock->expects($this->once())
->method('getParentItemsRandomCollection')
->with(\Magento\Sales\CustomerData\LastOrderedItems::SIDEBAR_ORDER_LIMIT)
->willReturn($items);
$productVisible->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(true);
$productVisible->expects($this->once())->method('getProductUrl')->willReturn($expectedItem1['url']);
$productVisible->expects($this->once())->method('getWebsiteIds')->willReturn([1, 4]);
$productVisible->expects($this->once())->method('getId')->willReturn($productIdVisible);
$productNotVisible->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(false);
$productNotVisible->expects($this->never())->method('getProductUrl');
$productNotVisible->expects($this->once())->method('getWebsiteIds')->willReturn([1, 4]);
$productNotVisible->expects($this->once())->method('getId')->willReturn($productIdNotVisible);
$itemWithVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdVisible);
$itemWithVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productVisible);
$itemWithVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem1['id']);
$itemWithVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem1['name']);
$itemWithVisibleProduct->expects($this->once())->method('getStore')->willReturn($storeMock);
$itemWithNotVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdNotVisible);
$itemWithNotVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productNotVisible);
$itemWithNotVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem2['id']);
$itemWithNotVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem2['name']);
$itemWithNotVisibleProduct->expects($this->once())->method('getStore')->willReturn($storeMock);
$this->productRepositoryMock->expects($this->any())
->method('getById')
->willReturnMap([
[$productIdVisible, false, $storeId, false, $productVisible],
[$productIdNotVisible, false, $storeId, false, $productNotVisible],
]);
$this->stockRegistryMock
->expects($this->any())
->method('getStockItem')
->willReturnMap([
[$productIdVisible, $websiteId, $stockItemMock],
[$productIdNotVisible, $websiteId, $stockItemMock],
]);
$stockItemMock->expects($this->exactly(2))->method('getIsInStock')->willReturn($expectedItem1['is_saleable']);
$this->assertEquals(['items' => [$expectedItem1, $expectedItem2]], $this->section->getSectionData());
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function getLastOrderMock()
{
$customerId = 1;
$visibleOnFrontStatuses = ['complete'];
$orderCollectionMock = $this->objectManagerHelper
->getCollectionMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [$this->orderMock]);
$this->customerSessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
$this->orderConfigMock
->expects($this->once())
->method('getVisibleOnFrontStatuses')
->willReturn($visibleOnFrontStatuses);
$this->orderCollectionFactoryMock->expects($this->once())->method('create')->willReturn($orderCollectionMock);
$orderCollectionMock->expects($this->at(0))
->method('addAttributeToFilter')
->with('customer_id', $customerId)
->willReturnSelf();
$orderCollectionMock->expects($this->at(1))
->method('addAttributeToFilter')
->with('status', ['in' => $visibleOnFrontStatuses])
->willReturnSelf();
$orderCollectionMock->expects($this->once())
->method('addAttributeToSort')
->willReturnSelf();
$orderCollectionMock->expects($this->once())
->method('setPage')
->willReturnSelf();
return $this->orderMock;
}
public function testGetSectionDataWithNotExistingProduct()
{
$storeId = 1;
$websiteId = 4;
$productId = 1;
$exception = new \Magento\Framework\Exception\NoSuchEntityException(__("Product doesn't exist"));
$orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
->disableOriginalConstructor()
->setMethods(['getProductId'])
->getMock();
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
$this->getLastOrderMock();
$this->storeManagerMock->expects($this->exactly(2))->method('getStore')->willReturn($storeMock);
$storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
$storeMock->expects($this->once())->method('getId')->willReturn($storeId);
$this->orderMock->expects($this->once())
->method('getParentItemsRandomCollection')
->with(\Magento\Sales\CustomerData\LastOrderedItems::SIDEBAR_ORDER_LIMIT)
->willReturn([$orderItemMock]);
$orderItemMock->expects($this->once())->method('getProductId')->willReturn($productId);
$this->productRepositoryMock->expects($this->once())
->method('getById')
->with($productId, false, $storeId)
->willThrowException($exception);
$this->loggerMock->expects($this->once())->method('critical')->with($exception);
$this->assertEquals(['items' => []], $this->section->getSectionData());
}
}