2727use Magento \Framework \DB \Adapter \AdapterInterface ;
2828use Magento \Framework \DB \Select ;
2929use PHPUnit \Framework \MockObject \Exception ;
30+ use PHPUnit \Framework \MockObject \MockObject ;
3031use PHPUnit \Framework \TestCase ;
31- use ReflectionClass ;
32- use ReflectionException ;
3332
3433class StorageTest extends TestCase
3534{
3635 /**
37- * @var Storage
36+ * @var MockObject| Storage
3837 */
39- private Storage $ storage ;
38+ private MockObject | Storage $ storage ;
4039
4140 /**
42- * @var Collection| MockObject
41+ * @var MockObject|Collection
4342 */
44- private mixed $ customerCollectionMock ;
43+ private MockObject | Collection $ customerCollectionMock ;
4544
4645 /**
47- * @var Share| MockObject
46+ * @var MockObject|Share
4847 */
49- private mixed $ configShareMock ;
48+ private MockObject | Share $ configShareMock ;
5049
5150 /**
52- * @var AdapterInterface| MockObject
51+ * @var MockObject|AdapterInterface
5352 */
54- private mixed $ connectionMock ;
53+ private MockObject | AdapterInterface $ connectionMock ;
5554
5655 /**
5756 * @inheritdoc
@@ -75,19 +74,60 @@ protected function setUp(): void
7574 }
7675
7776 /**
78- * Test loadCustomersData method when the scope is set to global.
77+ * Test prepareCustomers when the scope is set to global.
7978 *
80- * @throws Exception|ReflectionException
79+ * @dataProvider customerDataProvider
80+ * @throws Exception
8181 */
82- public function testLoadCustomersData ()
82+ public function testPrepareCustomers ( array $ customersToFind , array $ customersData , array $ expectedResults ): void
8383 {
84- $ customerIdentifiers = [
85- 'test@example.com_2 ' => ['email ' => 'test@example.com ' , 'website_id ' => 2 ],
86- 'test@example.com_3 ' => ['email ' => 'test@example.com ' , 'website_id ' => 3 ],
87- 'test@example.com_4 ' => ['email ' => 'test@example.com ' , 'website_id ' => 4 ],
88- 'test@example.com_5 ' => ['email ' => 'test@example.com ' , 'website_id ' => 5 ]
84+ $ this ->mockCustomerCollection ($ customersData );
85+ $ this ->storage ->prepareCustomers ($ customersToFind );
86+
87+ foreach ($ expectedResults as $ email => $ expectedResult ) {
88+ foreach ($ expectedResult as $ websiteId => $ expectedCustomerId ) {
89+ $ this ->assertEquals ($ expectedCustomerId , $ this ->storage ->getCustomerId ($ email , $ websiteId ));
90+ }
91+ }
92+ }
93+
94+ /**
95+ * Data provider for testPrepareCustomers.
96+ *
97+ * @return array[]
98+ */
99+ public static function customerDataProvider (): array
100+ {
101+ return [
102+ 'Test sample customers data ' => [
103+ 'customersToFind ' => [
104+ ['email ' => 'test@example.com ' , 'website_id ' => 3 ],
105+ ['email ' => 'test@example.com ' , 'website_id ' => 4 ],
106+ ['email ' => 'test@example.com ' , 'website_id ' => 5 ],
107+ ['email ' => 'test@example.com ' , 'website_id ' => 6 ],
108+ ],
109+ 'customersData ' => [
110+ ['email ' => 'test@example.com ' , 'website_id ' => 1 , 'entity_id ' => 1 , 'store_id ' => 1 ],
111+ ['email ' => 'test@example.com ' , 'website_id ' => 2 , 'entity_id ' => 2 , 'store_id ' => 2 ],
112+ ],
113+ 'expectedResults ' => [
114+ 'test@example.com ' => [
115+ 1 => 1 ,
116+ 2 => 2 ,
117+ ],
118+ ]
119+ ],
89120 ];
121+ }
90122
123+ /**
124+ * Mock the customer collection to return specific data.
125+ *
126+ * @param array $customersData
127+ * @throws Exception
128+ */
129+ private function mockCustomerCollection (array $ customersData ): void
130+ {
91131 $ selectMock = $ this ->createMock (Select::class);
92132 $ selectMock ->expects ($ this ->once ())
93133 ->method ('getPart ' )
@@ -96,41 +136,27 @@ public function testLoadCustomersData()
96136 $ this ->customerCollectionMock ->expects ($ this ->once ())
97137 ->method ('getSelect ' )
98138 ->willReturn ($ selectMock );
99- $ connectionMock = $ this ->getConnectionMock ();
100- $ this ->customerCollectionMock
101- ->expects ($ this ->once ())
139+
140+ $ this ->customerCollectionMock ->expects ($ this ->once ())
102141 ->method ('getConnection ' )
103- ->willReturn ($ connectionMock );
142+ ->willReturn ($ this -> mockConnection ( $ customersData ) );
104143
105- $ this ->configShareMock ->expects ($ this ->once ( ))
144+ $ this ->configShareMock ->expects ($ this ->exactly ( 2 ))
106145 ->method ('isGlobalScope ' )
107146 ->willReturn (true );
108-
109- $ reflection = new ReflectionClass ($ this ->storage );
110- $ customerIdsProperty = $ reflection ->getProperty ('_customerIds ' );
111- $ loadCustomersDataMethod = $ reflection ->getMethod ('loadCustomersData ' );
112- $ loadCustomersDataMethod ->setAccessible (true );
113- $ loadCustomersDataMethod ->invokeArgs ($ this ->storage , [$ customerIdentifiers ]);
114- $ customerIds = $ customerIdsProperty ->getValue ($ this ->storage );
115- $ this ->assertArrayHasKey ('test@example.com ' , $ customerIds );
116147 }
117148
118149 /**
119- * Mock DB connection and return customer data's
150+ * Mock the database connection to return specific customer data.
120151 *
121- * @return AdapterInterface
122- * @throws Exception
152+ * @param array $customersData
153+ * @return MockObject
123154 */
124- private function getConnectionMock ( ): AdapterInterface
155+ private function mockConnection ( array $ customersData ): MockObject
125156 {
126- $ customerData = [
127- 'email ' => 'test@example.com ' ,
128- 'website_id ' => 1 ,
129- 'entity_id ' => 1 ,
130- 'store_id ' => 1
131- ];
132157 $ this ->connectionMock ->expects ($ this ->once ())
133- ->method ('fetchAll ' )->willReturn ([$ customerData ]);
158+ ->method ('fetchAll ' )
159+ ->willReturn ($ customersData );
134160
135161 return $ this ->connectionMock ;
136162 }
0 commit comments