7
7
8
8
namespace Magento \CatalogInventory \Test \Unit \Model \Indexer \Stock ;
9
9
10
+ use Magento \Catalog \Model \Category ;
10
11
use Magento \Catalog \Model \Product ;
11
12
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
12
13
use Magento \CatalogInventory \Model \Indexer \Stock \CacheCleaner ;
20
21
use PHPUnit \Framework \MockObject \MockObject ;
21
22
use PHPUnit \Framework \TestCase ;
22
23
24
+ /**
25
+ * Test for CacheCleaner
26
+ */
23
27
class CacheCleanerTest extends TestCase
24
28
{
25
29
/**
@@ -70,14 +74,16 @@ protected function setUp(): void
70
74
$ this ->connectionMock = $ this ->getMockBuilder (AdapterInterface::class)
71
75
->getMock ();
72
76
$ this ->stockConfigurationMock = $ this ->getMockBuilder (StockConfigurationInterface::class)
73
- ->setMethods (['getStockThresholdQty ' ])->getMockForAbstractClass ();
77
+ ->setMethods (['getStockThresholdQty ' ])
78
+ ->getMockForAbstractClass ();
74
79
$ this ->cacheContextMock = $ this ->getMockBuilder (CacheContext::class)
75
80
->disableOriginalConstructor ()
76
81
->getMock ();
77
82
$ this ->eventManagerMock = $ this ->getMockBuilder (ManagerInterface::class)
78
83
->getMock ();
79
84
$ this ->metadataPoolMock = $ this ->getMockBuilder (MetadataPool::class)
80
- ->setMethods (['getMetadata ' , 'getLinkField ' ])->disableOriginalConstructor ()
85
+ ->setMethods (['getMetadata ' , 'getLinkField ' ])
86
+ ->disableOriginalConstructor ()
81
87
->getMock ();
82
88
$ this ->selectMock = $ this ->getMockBuilder (Select::class)
83
89
->disableOriginalConstructor ()
@@ -100,37 +106,63 @@ protected function setUp(): void
100
106
}
101
107
102
108
/**
109
+ * Test clean cache by product ids and category ids
110
+ *
103
111
* @param bool $stockStatusBefore
104
112
* @param bool $stockStatusAfter
105
113
* @param int $qtyAfter
106
114
* @param bool|int $stockThresholdQty
107
115
* @dataProvider cleanDataProvider
116
+ * @return void
108
117
*/
109
- public function testClean ($ stockStatusBefore , $ stockStatusAfter , $ qtyAfter , $ stockThresholdQty )
118
+ public function testClean ($ stockStatusBefore , $ stockStatusAfter , $ qtyAfter , $ stockThresholdQty ): void
110
119
{
111
120
$ productId = 123 ;
112
- $ this ->selectMock ->expects ($ this ->any ())->method ('from ' )->willReturnSelf ();
113
- $ this ->selectMock ->expects ($ this ->any ())->method ('where ' )->willReturnSelf ();
114
- $ this ->selectMock ->expects ($ this ->any ())->method ('joinLeft ' )->willReturnSelf ();
115
- $ this ->connectionMock ->expects ($ this ->exactly (2 ))->method ('select ' )->willReturn ($ this ->selectMock );
116
- $ this ->connectionMock ->expects ($ this ->exactly (2 ))->method ('fetchAll ' )->willReturnOnConsecutiveCalls (
117
- [
118
- ['product_id ' => $ productId , 'stock_status ' => $ stockStatusBefore ],
119
- ],
120
- [
121
- ['product_id ' => $ productId , 'stock_status ' => $ stockStatusAfter , 'qty ' => $ qtyAfter ],
122
- ]
123
- );
124
- $ this ->stockConfigurationMock ->expects ($ this ->once ())->method ('getStockThresholdQty ' )
121
+ $ categoryId = 3 ;
122
+ $ this ->selectMock ->expects ($ this ->any ())
123
+ ->method ('from ' )
124
+ ->willReturnSelf ();
125
+ $ this ->selectMock ->expects ($ this ->any ())
126
+ ->method ('where ' )
127
+ ->willReturnSelf ();
128
+ $ this ->selectMock ->expects ($ this ->any ())
129
+ ->method ('joinLeft ' )
130
+ ->willReturnSelf ();
131
+ $ this ->connectionMock ->expects ($ this ->exactly (3 ))
132
+ ->method ('select ' )
133
+ ->willReturn ($ this ->selectMock );
134
+ $ this ->connectionMock ->expects ($ this ->exactly (2 ))
135
+ ->method ('fetchAll ' )
136
+ ->willReturnOnConsecutiveCalls (
137
+ [
138
+ ['product_id ' => $ productId , 'stock_status ' => $ stockStatusBefore ],
139
+ ],
140
+ [
141
+ ['product_id ' => $ productId , 'stock_status ' => $ stockStatusAfter , 'qty ' => $ qtyAfter ],
142
+ ]
143
+ );
144
+ $ this ->connectionMock ->expects ($ this ->exactly (1 ))
145
+ ->method ('fetchCol ' )
146
+ ->willReturn ([$ categoryId ]);
147
+ $ this ->stockConfigurationMock ->expects ($ this ->once ())
148
+ ->method ('getStockThresholdQty ' )
125
149
->willReturn ($ stockThresholdQty );
126
- $ this ->cacheContextMock ->expects ($ this ->once ())->method ('registerEntities ' )
127
- ->with (Product::CACHE_TAG , [$ productId ]);
128
- $ this ->eventManagerMock ->expects ($ this ->once ())->method ('dispatch ' )
150
+ $ this ->cacheContextMock ->expects ($ this ->exactly (2 ))
151
+ ->method ('registerEntities ' )
152
+ ->withConsecutive (
153
+ [Product::CACHE_TAG , [$ productId ]],
154
+ [Category::CACHE_TAG , [$ categoryId ]],
155
+ );
156
+ $ this ->eventManagerMock ->expects ($ this ->exactly (2 ))
157
+ ->method ('dispatch ' )
129
158
->with ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContextMock ]);
130
- $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))->method ('getMetadata ' )
159
+ $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))
160
+ ->method ('getMetadata ' )
131
161
->willReturnSelf ();
132
- $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))->method ('getLinkField ' )
162
+ $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))
163
+ ->method ('getLinkField ' )
133
164
->willReturn ('row_id ' );
165
+
134
166
$ callback = function () {
135
167
};
136
168
$ this ->unit ->clean ([], $ callback );
@@ -139,7 +171,7 @@ public function testClean($stockStatusBefore, $stockStatusAfter, $qtyAfter, $sto
139
171
/**
140
172
* @return array
141
173
*/
142
- public function cleanDataProvider ()
174
+ public function cleanDataProvider (): array
143
175
{
144
176
return [
145
177
[true , false , 1 , false ],
@@ -155,29 +187,42 @@ public function cleanDataProvider()
155
187
* @param int $qtyAfter
156
188
* @param bool|int $stockThresholdQty
157
189
* @dataProvider notCleanCacheDataProvider
190
+ * @return void
158
191
*/
159
- public function testNotCleanCache ($ stockStatusBefore , $ stockStatusAfter , $ qtyAfter , $ stockThresholdQty )
192
+ public function testNotCleanCache ($ stockStatusBefore , $ stockStatusAfter , $ qtyAfter , $ stockThresholdQty ): void
160
193
{
161
194
$ productId = 123 ;
162
- $ this ->selectMock ->expects ($ this ->any ())->method ('from ' )->willReturnSelf ();
163
- $ this ->selectMock ->expects ($ this ->any ())->method ('where ' )->willReturnSelf ();
164
- $ this ->selectMock ->expects ($ this ->any ())->method ('joinLeft ' )->willReturnSelf ();
165
- $ this ->connectionMock ->expects ($ this ->exactly (2 ))->method ('select ' )->willReturn ($ this ->selectMock );
166
- $ this ->connectionMock ->expects ($ this ->exactly (2 ))->method ('fetchAll ' )->willReturnOnConsecutiveCalls (
167
- [
168
- ['product_id ' => $ productId , 'stock_status ' => $ stockStatusBefore ],
169
- ],
170
- [
171
- ['product_id ' => $ productId , 'stock_status ' => $ stockStatusAfter , 'qty ' => $ qtyAfter ],
172
- ]
173
- );
174
- $ this ->stockConfigurationMock ->expects ($ this ->once ())->method ('getStockThresholdQty ' )
195
+ $ this ->selectMock ->expects ($ this ->any ())->method ('from ' )
196
+ ->willReturnSelf ();
197
+ $ this ->selectMock ->expects ($ this ->any ())->method ('where ' )
198
+ ->willReturnSelf ();
199
+ $ this ->selectMock ->expects ($ this ->any ())->method ('joinLeft ' )
200
+ ->willReturnSelf ();
201
+ $ this ->connectionMock ->expects ($ this ->exactly (2 ))
202
+ ->method ('select ' )
203
+ ->willReturn ($ this ->selectMock );
204
+ $ this ->connectionMock ->expects ($ this ->exactly (2 ))
205
+ ->method ('fetchAll ' )
206
+ ->willReturnOnConsecutiveCalls (
207
+ [
208
+ ['product_id ' => $ productId , 'stock_status ' => $ stockStatusBefore ],
209
+ ],
210
+ [
211
+ ['product_id ' => $ productId , 'stock_status ' => $ stockStatusAfter , 'qty ' => $ qtyAfter ],
212
+ ]
213
+ );
214
+ $ this ->stockConfigurationMock ->expects ($ this ->once ())
215
+ ->method ('getStockThresholdQty ' )
175
216
->willReturn ($ stockThresholdQty );
176
- $ this ->cacheContextMock ->expects ($ this ->never ())->method ('registerEntities ' );
177
- $ this ->eventManagerMock ->expects ($ this ->never ())->method ('dispatch ' );
178
- $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))->method ('getMetadata ' )
217
+ $ this ->cacheContextMock ->expects ($ this ->never ())
218
+ ->method ('registerEntities ' );
219
+ $ this ->eventManagerMock ->expects ($ this ->never ())
220
+ ->method ('dispatch ' );
221
+ $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))
222
+ ->method ('getMetadata ' )
179
223
->willReturnSelf ();
180
- $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))->method ('getLinkField ' )
224
+ $ this ->metadataPoolMock ->expects ($ this ->exactly (2 ))
225
+ ->method ('getLinkField ' )
181
226
->willReturn ('row_id ' );
182
227
183
228
$ callback = function () {
@@ -188,7 +233,7 @@ public function testNotCleanCache($stockStatusBefore, $stockStatusAfter, $qtyAft
188
233
/**
189
234
* @return array
190
235
*/
191
- public function notCleanCacheDataProvider ()
236
+ public function notCleanCacheDataProvider (): array
192
237
{
193
238
return [
194
239
[true , true , 1 , false ],
0 commit comments