@@ -48,11 +48,21 @@ class LastOrderedItemsTest extends \PHPUnit\Framework\TestCase
48
48
*/
49
49
private $ orderMock ;
50
50
51
+ /**
52
+ * @var \PHPUnit_Framework_MockObject_MockObject
53
+ */
54
+ private $ productRepositoryMock ;
55
+
51
56
/**
52
57
* @var \Magento\Sales\CustomerData\LastOrderedItems
53
58
*/
54
59
private $ section ;
55
60
61
+ /**
62
+ * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
63
+ */
64
+ private $ loggerMock ;
65
+
56
66
protected function setUp ()
57
67
{
58
68
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
@@ -74,62 +84,97 @@ protected function setUp()
74
84
$ this ->orderMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order::class)
75
85
->disableOriginalConstructor ()
76
86
->getMock ();
87
+ $ this ->productRepositoryMock = $ this ->getMockBuilder (\Magento \Catalog \Api \ProductRepositoryInterface::class)
88
+ ->getMockForAbstractClass ();
89
+ $ this ->loggerMock = $ this ->getMockBuilder (\Psr \Log \LoggerInterface::class)
90
+ ->getMockForAbstractClass ();
91
+
77
92
$ this ->section = new \Magento \Sales \CustomerData \LastOrderedItems (
78
93
$ this ->orderCollectionFactoryMock ,
79
94
$ this ->orderConfigMock ,
80
95
$ this ->customerSessionMock ,
81
96
$ this ->stockRegistryMock ,
82
- $ this ->storeManagerMock
97
+ $ this ->storeManagerMock ,
98
+ $ this ->productRepositoryMock ,
99
+ $ this ->loggerMock
83
100
);
84
101
}
85
102
86
103
public function testGetSectionData ()
87
104
{
105
+ $ storeId = 1 ;
88
106
$ websiteId = 4 ;
89
- $ expectedItem = [
107
+ $ expectedItem1 = [
90
108
'id ' => 1 ,
91
- 'name ' => 'Product Name ' ,
109
+ 'name ' => 'Product Name 1 ' ,
92
110
'url ' => 'http://example.com ' ,
93
111
'is_saleable ' => true ,
94
112
];
95
- $ productId = 10 ;
113
+ $ expectedItem2 = [
114
+ 'id ' => 2 ,
115
+ 'name ' => 'Product Name 2 ' ,
116
+ 'url ' => null ,
117
+ 'is_saleable ' => true ,
118
+ ];
119
+ $ productIdVisible = 1 ;
120
+ $ productIdNotVisible = 2 ;
96
121
$ stockItemMock = $ this ->getMockBuilder (\Magento \CatalogInventory \Api \Data \StockItemInterface::class)
97
122
->getMockForAbstractClass ();
98
- $ itemWithProductMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
123
+ $ itemWithVisibleProduct = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
124
+ ->disableOriginalConstructor ()
125
+ ->getMock ();
126
+ $ itemWithNotVisibleProduct = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
99
127
->disableOriginalConstructor ()
100
128
->getMock ();
101
- $ itemWithoutProductMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item ::class)
129
+ $ productVisible = $ this ->getMockBuilder (\Magento \Catalog \Model \Product ::class)
102
130
->disableOriginalConstructor ()
103
131
->getMock ();
104
- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
132
+ $ productNotVisible = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
105
133
->disableOriginalConstructor ()
106
134
->getMock ();
107
- $ items = [$ itemWithoutProductMock , $ itemWithProductMock ];
135
+ $ items = [$ itemWithVisibleProduct , $ itemWithNotVisibleProduct ];
108
136
$ this ->getLastOrderMock ();
109
137
$ storeMock = $ this ->getMockBuilder (\Magento \Store \Api \Data \StoreInterface::class)->getMockForAbstractClass ();
110
- $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ storeMock );
138
+ $ this ->storeManagerMock ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ storeMock );
111
139
$ storeMock ->expects ($ this ->any ())->method ('getWebsiteId ' )->willReturn ($ websiteId );
140
+ $ storeMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ storeId );
112
141
$ this ->orderMock ->expects ($ this ->once ())
113
142
->method ('getParentItemsRandomCollection ' )
114
143
->with (\Magento \Sales \CustomerData \LastOrderedItems::SIDEBAR_ORDER_LIMIT )
115
144
->willReturn ($ items );
116
- $ itemWithProductMock ->expects ($ this ->once ())->method ('hasData ' )->with ('product ' )->willReturn (true );
117
- $ itemWithProductMock ->expects ($ this ->any ())->method ('getProduct ' )->willReturn ($ productMock );
118
- $ productMock ->expects ($ this ->once ())->method ('getWebsiteIds ' )->willReturn ([1 , 4 ]);
119
- $ itemWithProductMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ expectedItem ['id ' ]);
120
- $ itemWithProductMock ->expects ($ this ->once ())->method ('getName ' )->willReturn ($ expectedItem ['name ' ]);
121
- $ productMock ->expects ($ this ->once ())->method ('getProductUrl ' )->willReturn ($ expectedItem ['url ' ]);
122
- $ this ->stockRegistryMock ->expects ($ this ->once ())->method ('getStockItem ' )->willReturn ($ stockItemMock );
123
- $ productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
124
- $ itemWithProductMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ storeMock );
145
+ $ productVisible ->expects ($ this ->once ())->method ('isVisibleInSiteVisibility ' )->willReturn (true );
146
+ $ productVisible ->expects ($ this ->once ())->method ('getProductUrl ' )->willReturn ($ expectedItem1 ['url ' ]);
147
+ $ productVisible ->expects ($ this ->once ())->method ('getWebsiteIds ' )->willReturn ([1 , 4 ]);
148
+ $ productVisible ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productIdVisible );
149
+ $ productNotVisible ->expects ($ this ->once ())->method ('isVisibleInSiteVisibility ' )->willReturn (false );
150
+ $ productNotVisible ->expects ($ this ->never ())->method ('getProductUrl ' );
151
+ $ productNotVisible ->expects ($ this ->once ())->method ('getWebsiteIds ' )->willReturn ([1 , 4 ]);
152
+ $ productNotVisible ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productIdNotVisible );
153
+ $ itemWithVisibleProduct ->expects ($ this ->once ())->method ('getProductId ' )->willReturn ($ productIdVisible );
154
+ $ itemWithVisibleProduct ->expects ($ this ->once ())->method ('getProduct ' )->willReturn ($ productVisible );
155
+ $ itemWithVisibleProduct ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ expectedItem1 ['id ' ]);
156
+ $ itemWithVisibleProduct ->expects ($ this ->once ())->method ('getName ' )->willReturn ($ expectedItem1 ['name ' ]);
157
+ $ itemWithVisibleProduct ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ storeMock );
158
+ $ itemWithNotVisibleProduct ->expects ($ this ->once ())->method ('getProductId ' )->willReturn ($ productIdNotVisible );
159
+ $ itemWithNotVisibleProduct ->expects ($ this ->once ())->method ('getProduct ' )->willReturn ($ productNotVisible );
160
+ $ itemWithNotVisibleProduct ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ expectedItem2 ['id ' ]);
161
+ $ itemWithNotVisibleProduct ->expects ($ this ->once ())->method ('getName ' )->willReturn ($ expectedItem2 ['name ' ]);
162
+ $ itemWithNotVisibleProduct ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ storeMock );
163
+ $ this ->productRepositoryMock ->expects ($ this ->any ())
164
+ ->method ('getById ' )
165
+ ->willReturnMap ([
166
+ [$ productIdVisible , false , $ storeId , false , $ productVisible ],
167
+ [$ productIdNotVisible , false , $ storeId , false , $ productNotVisible ],
168
+ ]);
125
169
$ this ->stockRegistryMock
126
- ->expects ($ this ->once ())
170
+ ->expects ($ this ->any ())
127
171
->method ('getStockItem ' )
128
- ->with ($ productId , $ websiteId )
129
- ->willReturn ($ stockItemMock );
130
- $ stockItemMock ->expects ($ this ->once ())->method ('getIsInStock ' )->willReturn ($ expectedItem ['is_saleable ' ]);
131
- $ itemWithoutProductMock ->expects ($ this ->once ())->method ('hasData ' )->with ('product ' )->willReturn (false );
132
- $ this ->assertEquals (['items ' => [$ expectedItem ]], $ this ->section ->getSectionData ());
172
+ ->willReturnMap ([
173
+ [$ productIdVisible , $ websiteId , $ stockItemMock ],
174
+ [$ productIdNotVisible , $ websiteId , $ stockItemMock ],
175
+ ]);
176
+ $ stockItemMock ->expects ($ this ->exactly (2 ))->method ('getIsInStock ' )->willReturn ($ expectedItem1 ['is_saleable ' ]);
177
+ $ this ->assertEquals (['items ' => [$ expectedItem1 , $ expectedItem2 ]], $ this ->section ->getSectionData ());
133
178
}
134
179
135
180
private function getLastOrderMock ()
@@ -160,4 +205,34 @@ private function getLastOrderMock()
160
205
->willReturnSelf ();
161
206
return $ this ->orderMock ;
162
207
}
208
+
209
+ public function testGetSectionDataWithNotExistingProduct ()
210
+ {
211
+ $ storeId = 1 ;
212
+ $ websiteId = 4 ;
213
+ $ productId = 1 ;
214
+ $ exception = new \Magento \Framework \Exception \NoSuchEntityException (__ ("Product doesn't exist " ));
215
+ $ orderItemMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
216
+ ->disableOriginalConstructor ()
217
+ ->setMethods (['getProductId ' ])
218
+ ->getMock ();
219
+ $ storeMock = $ this ->getMockBuilder (\Magento \Store \Api \Data \StoreInterface::class)->getMockForAbstractClass ();
220
+
221
+ $ this ->getLastOrderMock ();
222
+ $ this ->storeManagerMock ->expects ($ this ->exactly (2 ))->method ('getStore ' )->willReturn ($ storeMock );
223
+ $ storeMock ->expects ($ this ->once ())->method ('getWebsiteId ' )->willReturn ($ websiteId );
224
+ $ storeMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ storeId );
225
+ $ this ->orderMock ->expects ($ this ->once ())
226
+ ->method ('getParentItemsRandomCollection ' )
227
+ ->with (\Magento \Sales \CustomerData \LastOrderedItems::SIDEBAR_ORDER_LIMIT )
228
+ ->willReturn ([$ orderItemMock ]);
229
+ $ orderItemMock ->expects ($ this ->once ())->method ('getProductId ' )->willReturn ($ productId );
230
+ $ this ->productRepositoryMock ->expects ($ this ->once ())
231
+ ->method ('getById ' )
232
+ ->with ($ productId , false , $ storeId )
233
+ ->willThrowException ($ exception );
234
+ $ this ->loggerMock ->expects ($ this ->once ())->method ('critical ' )->with ($ exception );
235
+
236
+ $ this ->assertEquals (['items ' => []], $ this ->section ->getSectionData ());
237
+ }
163
238
}
0 commit comments