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.
@@ -30,17 +33,23 @@ class SynchronizerTest extends \PHPUnit\Framework\TestCase
30
33
*/
31
34
private $ productRepository ;
32
35
36
+ /**
37
+ * @var CustomerRepositoryInterface
38
+ */
39
+ private $ customerRepository ;
40
+
33
41
/**
34
42
* @inheritDoc
35
43
*/
36
44
protected function setUp (): void
37
45
{
38
46
$ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
39
- $ this ->session = $ objectManager ->get (\ Magento \ Customer \ Model \ Session::class);
40
- $ this ->visitor = $ objectManager ->get (\ Magento \ Customer \ Model \ Visitor::class);
47
+ $ this ->session = $ objectManager ->get (Session::class);
48
+ $ this ->visitor = $ objectManager ->get (Visitor::class);
41
49
42
50
$ this ->synchronizer = $ objectManager ->get (Synchronizer::class);
43
51
$ this ->productRepository = $ objectManager ->get (ProductRepository::class);
52
+ $ this ->customerRepository = $ objectManager ->get (CustomerRepositoryInterface::class);
44
53
}
45
54
46
55
/**
@@ -120,27 +129,23 @@ public function testSyncActionsWithoutActionsType(): void
120
129
}
121
130
122
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
123
137
* @return void
124
- * @throws \Magento\Framework\Exception\LocalizedException
125
- * @throws \Magento\Framework\Exception\NoSuchEntityException
126
- */
127
- public function testGetAllActionsWithoutCustomerAndVisitor (): void
128
- {
129
- $ collection = $ this ->synchronizer ->getAllActions ();
130
- $ this ->assertEquals ($ collection ->getSize (), 0 );
131
- }
132
-
133
- /**
138
+ * @dataProvider getAllActionsDataProvider
139
+ *
140
+ * @magentoDataFixture Magento/Customer/_files/customer.php
134
141
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
135
142
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
136
- *
137
- * @return void
138
- * @throws \Magento\Framework\Exception\LocalizedException
139
- * @throws \Magento\Framework\Exception\NoSuchEntityException
140
143
*/
141
- public function testGetAllActionsOfVisitor ( ): void
144
+ public function testGetAllActions (? int $ visitorId , ? string $ customerEmail , int $ expectedCollectionSize ): void
142
145
{
143
- $ this ->visitor ->setId (123 );
146
+ $ customerId = $ customerEmail ? $ this ->customerRepository ->get ($ customerEmail )->getId () : null ;
147
+ $ this ->session ->setCustomerId ($ customerId );
148
+ $ this ->visitor ->setId ($ visitorId );
144
149
$ actionsType = 'recently_viewed_product ' ;
145
150
$ productScope = 'website ' ;
146
151
$ scopeId = 1 ;
@@ -161,61 +166,21 @@ public function testGetAllActionsOfVisitor(): void
161
166
162
167
$ this ->synchronizer ->syncActions ($ productsData , $ actionsType );
163
168
$ collection = $ this ->synchronizer ->getAllActions ();
164
- $ this ->assertEquals ($ collection ->getSize (), 2 );
169
+
170
+ $ this ->assertEquals ($ expectedCollectionSize , $ collection ->getSize ());
165
171
}
166
172
167
173
/**
168
- * @magentoDataFixture Magento/Catalog/_files/product_simple.php
169
- * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
170
- *
171
- * @return void
174
+ * @return array[]
172
175
* @throws \Magento\Framework\Exception\LocalizedException
173
176
* @throws \Magento\Framework\Exception\NoSuchEntityException
174
177
*/
175
- public function testGetAllActionsOfCustomer (): void
178
+ public function getAllActionsDataProvider ()
176
179
{
177
- $ customer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
178
- ->create (\Magento \Customer \Model \Customer::class);
179
- /** @var \Magento\Customer\Model\Customer $customer */
180
- $ customer
181
- ->setWebsiteId (1 )
182
- ->setId (1 )
183
- ->setEntityTypeId (1 )
184
- ->setAttributeSetId (1 )
185
- ->setEmail ('customer@example.com ' )
186
- ->setPassword ('password ' )
187
- ->setGroupId (1 )
188
- ->setStoreId (1 )
189
- ->setIsActive (1 )
190
- ->setFirstname ('Firstname ' )
191
- ->setLastname ('Lastname ' )
192
- ->setDefaultBilling (1 )
193
- ->setDefaultShipping (1 );
194
- $ customer ->isObjectNew (true );
195
- $ customer ->save ();
196
-
197
- $ this ->session ->setCustomerId (1 );
198
- $ this ->visitor ->setId (null );
199
- $ actionsType = 'recently_viewed_product ' ;
200
- $ productScope = 'website ' ;
201
- $ scopeId = 1 ;
202
- $ product1 = $ this ->productRepository ->get ('simple ' );
203
- $ product2 = $ this ->productRepository ->get ('simple2 ' );
204
- $ product1Id = $ product1 ->getId ();
205
- $ product2Id = $ product2 ->getId ();
206
- $ productsData = [
207
- $ productScope . '- ' . $ scopeId . '- ' . $ product1Id => [
208
- 'added_at ' => '1576582660 ' ,
209
- 'product_id ' => $ product1Id ,
210
- ],
211
- $ productScope . '- ' . $ scopeId . '- ' . $ product2Id => [
212
- 'added_at ' => '1576587153 ' ,
213
- 'product_id ' => $ product2Id ,
214
- ],
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 ],
215
184
];
216
-
217
- $ this ->synchronizer ->syncActions ($ productsData , $ actionsType );
218
- $ collection = $ this ->synchronizer ->getAllActions ();
219
- $ this ->assertEquals ($ collection ->getSize (), 2 );
220
185
}
221
186
}
0 commit comments