@@ -36,6 +36,16 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
36
36
*/
37
37
protected $ storeMock ;
38
38
39
+ /**
40
+ * @var \Magento\Reports\Model\Product\Index\ComparedFactory|\PHPUnit_Framework_MockObject_MockObject
41
+ */
42
+ protected $ productCompFactoryMock ;
43
+
44
+ /**
45
+ * @var \Magento\Reports\Model\Product\Index\Compared|\PHPUnit_Framework_MockObject_MockObject
46
+ */
47
+ protected $ productCompModelMock ;
48
+
39
49
public function setUp ()
40
50
{
41
51
$ objectManager = new \Magento \TestFramework \Helper \ObjectManager ($ this );
@@ -73,13 +83,21 @@ public function setUp()
73
83
$ storeManager ->expects ($ this ->any ())
74
84
->method ('getStore ' )
75
85
->willReturn ($ this ->storeMock );
86
+ $ this ->productCompFactoryMock = $ this ->getMockBuilder ('Magento\Reports\Model\Product\Index\ComparedFactory ' )
87
+ ->disableOriginalConstructor ()
88
+ ->setMethods (['create ' ])
89
+ ->getMock ();
90
+ $ this ->productCompModelMock = $ this ->getMockBuilder ('Magento\Reports\Model\Product\Index\Compared ' )
91
+ ->disableOriginalConstructor ()
92
+ ->getMock ();
76
93
77
94
$ this ->observer = $ objectManager ->getObject (
78
95
'Magento\Reports\Model\Event\Observer ' ,
79
96
[
80
97
'customerSession ' => $ this ->customerSessionMock ,
81
98
'customerVisitor ' => $ this ->customerVisitorMock ,
82
99
'productIndxFactory ' => $ productIndexFactoryMock ,
100
+ 'productCompFactory ' => $ this ->productCompFactoryMock ,
83
101
'storeManager ' => $ storeManager ,
84
102
'event ' => $ reportEventFactory
85
103
]
@@ -158,6 +176,63 @@ public function testCatalogProductViewVisitor()
158
176
$ this ->observer ->catalogProductView ($ eventObserver );
159
177
}
160
178
179
+ /**
180
+ * @param bool $isLoggedIn
181
+ * @param str $userKey
182
+ * @param int $userId
183
+ * @dataProvider catalogProductCompareAddProductDataProvider
184
+ */
185
+ public function testCatalogProductCompareAddProduct ($ isLoggedIn , $ userKey , $ userId )
186
+ {
187
+ $ productId = 111 ;
188
+ $ customerId = 222 ;
189
+ $ visitorId = 333 ;
190
+ $ viewData = [
191
+ 'product_id ' => $ productId ,
192
+ $ userKey => $ userId
193
+ ];
194
+ $ observerMock = $ this ->getObserverMock ($ productId );
195
+ $ this ->customerSessionMock ->expects ($ this ->any ())
196
+ ->method ('isLoggedIn ' )
197
+ ->willReturn ($ isLoggedIn );
198
+ $ this ->customerSessionMock ->expects ($ this ->any ())
199
+ ->method ('getCustomerId ' )
200
+ ->willReturn ($ customerId );
201
+ $ this ->customerVisitorMock ->expects ($ this ->any ())
202
+ ->method ('getId ' )
203
+ ->willReturn ($ visitorId );
204
+ $ this ->productCompFactoryMock ->expects ($ this ->any ())
205
+ ->method ('create ' )
206
+ ->willReturn ($ this ->productCompModelMock );
207
+ $ this ->productCompModelMock ->expects ($ this ->any ())
208
+ ->method ('setData ' )
209
+ ->with ($ viewData )
210
+ ->willReturnSelf ();
211
+ $ this ->productCompModelMock ->expects ($ this ->any ())
212
+ ->method ('save ' )
213
+ ->willReturnSelf ();
214
+ $ this ->productCompModelMock ->expects ($ this ->any ())
215
+ ->method ('calculate ' )
216
+ ->willReturnSelf ();
217
+ $ this ->assertEquals ($ this ->observer , $ this ->observer ->catalogProductCompareAddProduct ($ observerMock ));
218
+ }
219
+
220
+ public function catalogProductCompareAddProductDataProvider ()
221
+ {
222
+ return [
223
+ 'logged in ' => [
224
+ 'isLoggedIn ' => true ,
225
+ 'userKey ' => 'customer_id ' ,
226
+ 'userId ' => 222
227
+ ],
228
+ 'not logged in ' => [
229
+ 'isLoggedIn ' => false ,
230
+ 'userKey ' => 'visitor_id ' ,
231
+ 'userId ' => 333
232
+ ]
233
+ ];
234
+ }
235
+
161
236
/**
162
237
* @param array $expectedViewedData
163
238
* @return void
0 commit comments