27
27
use Magento \Framework \DB \Adapter \AdapterInterface ;
28
28
use Magento \Framework \DB \Select ;
29
29
use PHPUnit \Framework \MockObject \Exception ;
30
+ use PHPUnit \Framework \MockObject \MockObject ;
30
31
use PHPUnit \Framework \TestCase ;
31
- use ReflectionClass ;
32
- use ReflectionException ;
33
32
34
33
class StorageTest extends TestCase
35
34
{
36
35
/**
37
- * @var Storage
36
+ * @var MockObject| Storage
38
37
*/
39
- private Storage $ storage ;
38
+ private MockObject | Storage $ storage ;
40
39
41
40
/**
42
- * @var Collection| MockObject
41
+ * @var MockObject|Collection
43
42
*/
44
- private mixed $ customerCollectionMock ;
43
+ private MockObject | Collection $ customerCollectionMock ;
45
44
46
45
/**
47
- * @var Share| MockObject
46
+ * @var MockObject|Share
48
47
*/
49
- private mixed $ configShareMock ;
48
+ private MockObject | Share $ configShareMock ;
50
49
51
50
/**
52
- * @var AdapterInterface| MockObject
51
+ * @var MockObject|AdapterInterface
53
52
*/
54
- private mixed $ connectionMock ;
53
+ private MockObject | AdapterInterface $ connectionMock ;
55
54
56
55
/**
57
56
* @inheritdoc
@@ -75,19 +74,60 @@ protected function setUp(): void
75
74
}
76
75
77
76
/**
78
- * Test loadCustomersData method when the scope is set to global.
77
+ * Test prepareCustomers when the scope is set to global.
79
78
*
80
- * @throws Exception|ReflectionException
79
+ * @dataProvider customerDataProvider
80
+ * @throws Exception
81
81
*/
82
- public function testLoadCustomersData ()
82
+ public function testPrepareCustomers ( array $ customersToFind , array $ customersData , array $ expectedResults ): void
83
83
{
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
+ ],
89
120
];
121
+ }
90
122
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
+ {
91
131
$ selectMock = $ this ->createMock (Select::class);
92
132
$ selectMock ->expects ($ this ->once ())
93
133
->method ('getPart ' )
@@ -96,41 +136,27 @@ public function testLoadCustomersData()
96
136
$ this ->customerCollectionMock ->expects ($ this ->once ())
97
137
->method ('getSelect ' )
98
138
->willReturn ($ selectMock );
99
- $ connectionMock = $ this ->getConnectionMock ();
100
- $ this ->customerCollectionMock
101
- ->expects ($ this ->once ())
139
+
140
+ $ this ->customerCollectionMock ->expects ($ this ->once ())
102
141
->method ('getConnection ' )
103
- ->willReturn ($ connectionMock );
142
+ ->willReturn ($ this -> mockConnection ( $ customersData ) );
104
143
105
- $ this ->configShareMock ->expects ($ this ->once ( ))
144
+ $ this ->configShareMock ->expects ($ this ->exactly ( 2 ))
106
145
->method ('isGlobalScope ' )
107
146
->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 );
116
147
}
117
148
118
149
/**
119
- * Mock DB connection and return customer data's
150
+ * Mock the database connection to return specific customer data.
120
151
*
121
- * @return AdapterInterface
122
- * @throws Exception
152
+ * @param array $customersData
153
+ * @return MockObject
123
154
*/
124
- private function getConnectionMock ( ): AdapterInterface
155
+ private function mockConnection ( array $ customersData ): MockObject
125
156
{
126
- $ customerData = [
127
- 'email ' => 'test@example.com ' ,
128
- 'website_id ' => 1 ,
129
- 'entity_id ' => 1 ,
130
- 'store_id ' => 1
131
- ];
132
157
$ this ->connectionMock ->expects ($ this ->once ())
133
- ->method ('fetchAll ' )->willReturn ([$ customerData ]);
158
+ ->method ('fetchAll ' )
159
+ ->willReturn ($ customersData );
134
160
135
161
return $ this ->connectionMock ;
136
162
}
0 commit comments