8
8
namespace Magento \Catalog \Model \Product \ProductFrontendAction ;
9
9
10
10
use Magento \Catalog \Model \ProductRepository ;
11
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12
+ use Magento \Customer \Model \Session ;
13
+ use Magento \Customer \Model \Visitor ;
11
14
12
15
/**
13
16
* Test for \Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer.
@@ -19,20 +22,34 @@ class SynchronizerTest extends \PHPUnit\Framework\TestCase
19
22
*/
20
23
private $ synchronizer ;
21
24
25
+ /** @var Session */
26
+ private $ session ;
27
+
28
+ /** @var Visitor */
29
+ private $ visitor ;
30
+
22
31
/**
23
32
* @var ProductRepository
24
33
*/
25
34
private $ productRepository ;
26
35
36
+ /**
37
+ * @var CustomerRepositoryInterface
38
+ */
39
+ private $ customerRepository ;
40
+
27
41
/**
28
42
* @inheritDoc
29
43
*/
30
44
protected function setUp (): void
31
45
{
32
46
$ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
47
+ $ this ->session = $ objectManager ->get (Session::class);
48
+ $ this ->visitor = $ objectManager ->get (Visitor::class);
33
49
34
50
$ this ->synchronizer = $ objectManager ->get (Synchronizer::class);
35
51
$ this ->productRepository = $ objectManager ->get (ProductRepository::class);
52
+ $ this ->customerRepository = $ objectManager ->get (CustomerRepositoryInterface::class);
36
53
}
37
54
38
55
/**
@@ -110,4 +127,60 @@ public function testSyncActionsWithoutActionsType(): void
110
127
111
128
$ this ->synchronizer ->syncActions ($ productsData , '' );
112
129
}
130
+
131
+ /**
132
+ * Tests that product actions are returned correctly according to the provided customer or visitor.
133
+ *
134
+ * @param int|null $visitorId
135
+ * @param string|null $customerEmail
136
+ * @param int $expectedCollectionSize
137
+ * @return void
138
+ * @dataProvider getAllActionsDataProvider
139
+ *
140
+ * @magentoDataFixture Magento/Customer/_files/customer.php
141
+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
142
+ * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
143
+ */
144
+ public function testGetAllActions (?int $ visitorId , ?string $ customerEmail , int $ expectedCollectionSize ): void
145
+ {
146
+ $ customerId = $ customerEmail ? $ this ->customerRepository ->get ($ customerEmail )->getId () : null ;
147
+ $ this ->session ->setCustomerId ($ customerId );
148
+ $ this ->visitor ->setId ($ visitorId );
149
+ $ actionsType = 'recently_viewed_product ' ;
150
+ $ productScope = 'website ' ;
151
+ $ scopeId = 1 ;
152
+ $ product1 = $ this ->productRepository ->get ('simple ' );
153
+ $ product2 = $ this ->productRepository ->get ('simple2 ' );
154
+ $ product1Id = $ product1 ->getId ();
155
+ $ product2Id = $ product2 ->getId ();
156
+ $ productsData = [
157
+ $ productScope . '- ' . $ scopeId . '- ' . $ product1Id => [
158
+ 'added_at ' => '1576582660 ' ,
159
+ 'product_id ' => $ product1Id ,
160
+ ],
161
+ $ productScope . '- ' . $ scopeId . '- ' . $ product2Id => [
162
+ 'added_at ' => '1576587153 ' ,
163
+ 'product_id ' => $ product2Id ,
164
+ ],
165
+ ];
166
+
167
+ $ this ->synchronizer ->syncActions ($ productsData , $ actionsType );
168
+ $ collection = $ this ->synchronizer ->getAllActions ();
169
+
170
+ $ this ->assertEquals ($ expectedCollectionSize , $ collection ->getSize ());
171
+ }
172
+
173
+ /**
174
+ * @return array[]
175
+ * @throws \Magento\Framework\Exception\LocalizedException
176
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
177
+ */
178
+ public function getAllActionsDataProvider ()
179
+ {
180
+ return [
181
+ ['visitorId ' => null , 'customerEmail ' => 'customer@example.com ' , 'expectedCollectionSize ' => 2 ],
182
+ ['visitorId ' => 123 , 'customerEmail ' => null , 'expectedCollectionSize ' => 2 ],
183
+ ['visitorId ' => null , 'customerEmail ' => null , 'expectedCollectionSize ' => 0 ],
184
+ ];
185
+ }
113
186
}
0 commit comments