@@ -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,20 @@ 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
+ ->getMock ();
89
+ $ this ->productCompModelMock = $ this ->getMockBuilder ('Magento\Reports\Model\Product\Index\Compared ' )
90
+ ->disableOriginalConstructor ()
91
+ ->getMock ();
76
92
77
93
$ this ->observer = $ objectManager ->getObject (
78
94
'Magento\Reports\Model\Event\Observer ' ,
79
95
[
80
96
'customerSession ' => $ this ->customerSessionMock ,
81
97
'customerVisitor ' => $ this ->customerVisitorMock ,
82
98
'productIndxFactory ' => $ productIndexFactoryMock ,
99
+ 'productCompFactory ' => $ this ->productCompFactoryMock ,
83
100
'storeManager ' => $ storeManager ,
84
101
'event ' => $ reportEventFactory
85
102
]
@@ -158,6 +175,63 @@ public function testCatalogProductViewVisitor()
158
175
$ this ->observer ->catalogProductView ($ eventObserver );
159
176
}
160
177
178
+ /**
179
+ * @param bool $isLoggedIn
180
+ * @param str $userKey
181
+ * @param int $userId
182
+ * @dataProvider catalogProductCompareAddProductDataProvider
183
+ */
184
+ public function testCatalogProductCompareAddProduct ($ isLoggedIn , $ userKey , $ userId )
185
+ {
186
+ $ productId = 111 ;
187
+ $ customerId = 222 ;
188
+ $ visitorId = 333 ;
189
+ $ viewData = [
190
+ 'product_id ' => $ productId ,
191
+ $ userKey => $ userId
192
+ ];
193
+ $ observerMock = $ this ->getObserverMock ($ productId );
194
+ $ this ->customerSessionMock ->expects ($ this ->any ())
195
+ ->method ('isLoggedIn ' )
196
+ ->willReturn ($ isLoggedIn );
197
+ $ this ->customerSessionMock ->expects ($ this ->any ())
198
+ ->method ('getCustomerId ' )
199
+ ->willReturn ($ customerId );
200
+ $ this ->customerVisitorMock ->expects ($ this ->any ())
201
+ ->method ('getId ' )
202
+ ->willReturn ($ visitorId );
203
+ $ this ->productCompFactoryMock ->expects ($ this ->any ())
204
+ ->method ('create ' )
205
+ ->willReturn ($ this ->productCompModelMock );
206
+ $ this ->productCompModelMock ->expects ($ this ->any ())
207
+ ->method ('setData ' )
208
+ ->with ($ viewData )
209
+ ->willReturnSelf ();
210
+ $ this ->productCompModelMock ->expects ($ this ->any ())
211
+ ->method ('save ' )
212
+ ->willReturnSelf ();
213
+ $ this ->productCompModelMock ->expects ($ this ->any ())
214
+ ->method ('calculate ' )
215
+ ->willReturnSelf ();
216
+ $ this ->assertEquals ($ this ->observer , $ this ->observer ->catalogProductCompareAddProduct ($ observerMock ));
217
+ }
218
+
219
+ public function catalogProductCompareAddProductDataProvider ()
220
+ {
221
+ return [
222
+ 'logged in ' => [
223
+ 'isLoggedIn ' => true ,
224
+ 'userKey ' => 'customer_id ' ,
225
+ 'userId ' => 222
226
+ ],
227
+ 'not logged in ' => [
228
+ 'isLoggedIn ' => false ,
229
+ 'userKey ' => 'visitor_id ' ,
230
+ 'userId ' => 333
231
+ ]
232
+ ];
233
+ }
234
+
161
235
/**
162
236
* @param array $expectedViewedData
163
237
* @return void
0 commit comments