88namespace Magento \Catalog \Model \Product \ProductFrontendAction ;
99
1010use Magento \Catalog \Model \ProductRepository ;
11+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12+ use Magento \Customer \Model \Session ;
13+ use Magento \Customer \Model \Visitor ;
1114
1215/**
1316 * Test for \Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer.
@@ -30,17 +33,23 @@ class SynchronizerTest extends \PHPUnit\Framework\TestCase
3033 */
3134 private $ productRepository ;
3235
36+ /**
37+ * @var CustomerRepositoryInterface
38+ */
39+ private $ customerRepository ;
40+
3341 /**
3442 * @inheritDoc
3543 */
3644 protected function setUp (): void
3745 {
3846 $ 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);
4149
4250 $ this ->synchronizer = $ objectManager ->get (Synchronizer::class);
4351 $ this ->productRepository = $ objectManager ->get (ProductRepository::class);
52+ $ this ->customerRepository = $ objectManager ->get (CustomerRepositoryInterface::class);
4453 }
4554
4655 /**
@@ -120,27 +129,23 @@ public function testSyncActionsWithoutActionsType(): void
120129 }
121130
122131 /**
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
123137 * @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
134141 * @magentoDataFixture Magento/Catalog/_files/product_simple.php
135142 * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
136- *
137- * @return void
138- * @throws \Magento\Framework\Exception\LocalizedException
139- * @throws \Magento\Framework\Exception\NoSuchEntityException
140143 */
141- public function testGetAllActionsOfVisitor ( ): void
144+ public function testGetAllActions (? int $ visitorId , ? string $ customerEmail , int $ expectedCollectionSize ): void
142145 {
143- $ this ->visitor ->setId (123 );
146+ $ customerId = $ customerEmail ? $ this ->customerRepository ->get ($ customerEmail )->getId () : null ;
147+ $ this ->session ->setCustomerId ($ customerId );
148+ $ this ->visitor ->setId ($ visitorId );
144149 $ actionsType = 'recently_viewed_product ' ;
145150 $ productScope = 'website ' ;
146151 $ scopeId = 1 ;
@@ -161,61 +166,21 @@ public function testGetAllActionsOfVisitor(): void
161166
162167 $ this ->synchronizer ->syncActions ($ productsData , $ actionsType );
163168 $ collection = $ this ->synchronizer ->getAllActions ();
164- $ this ->assertEquals ($ collection ->getSize (), 2 );
169+
170+ $ this ->assertEquals ($ expectedCollectionSize , $ collection ->getSize ());
165171 }
166172
167173 /**
168- * @magentoDataFixture Magento/Catalog/_files/product_simple.php
169- * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
170- *
171- * @return void
174+ * @return array[]
172175 * @throws \Magento\Framework\Exception\LocalizedException
173176 * @throws \Magento\Framework\Exception\NoSuchEntityException
174177 */
175- public function testGetAllActionsOfCustomer (): void
178+ public function getAllActionsDataProvider ()
176179 {
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 ],
215184 ];
216-
217- $ this ->synchronizer ->syncActions ($ productsData , $ actionsType );
218- $ collection = $ this ->synchronizer ->getAllActions ();
219- $ this ->assertEquals ($ collection ->getSize (), 2 );
220185 }
221186}
0 commit comments