@@ -102,10 +102,7 @@ protected function setUp(): void
102
102
->onlyMethods (['getState ' , 'getProductCollection ' ])
103
103
->getMock ();
104
104
105
- $ this ->state = $ this ->getMockBuilder (State::class)
106
- ->disableOriginalConstructor ()
107
- ->onlyMethods (['addFilter ' ])
108
- ->getMock ();
105
+ $ this ->state = new State ();
109
106
$ this ->layer ->expects ($ this ->any ())
110
107
->method ('getState ' )
111
108
->willReturn ($ this ->state );
@@ -129,15 +126,24 @@ protected function setUp(): void
129
126
->onlyMethods (['create ' ])
130
127
->getMock ();
131
128
132
- $ filterItem = $ this ->getMockBuilder (Item::class)
133
- ->disableOriginalConstructor ()
134
- ->addMethods (['setFilter ' , 'setLabel ' , 'setValue ' , 'setCount ' ])
135
- ->getMock ();
136
- $ filterItem ->expects ($ this ->any ())
137
- ->method ($ this ->anything ())->willReturnSelf ();
138
129
$ this ->filterItemFactory ->expects ($ this ->any ())
139
130
->method ('create ' )
140
- ->willReturn ($ filterItem );
131
+ ->willReturnCallback (
132
+ function (array $ data ) {
133
+ return new Item (
134
+ $ this ->createMock (\Magento \Framework \UrlInterface::class),
135
+ $ this ->createMock (\Magento \Theme \Block \Html \Pager::class),
136
+ $ data
137
+ );
138
+ }
139
+ );
140
+ $ priceFormatter = $ this ->createMock (\Magento \Framework \Pricing \PriceCurrencyInterface::class);
141
+ $ priceFormatter ->method ('format ' )
142
+ ->willReturnCallback (
143
+ function ($ number ) {
144
+ return sprintf ('$%01.2f ' , $ number );
145
+ }
146
+ );
141
147
142
148
$ escaper = $ this ->getMockBuilder (Escaper::class)
143
149
->disableOriginalConstructor ()
@@ -160,7 +166,8 @@ protected function setUp(): void
160
166
'layer ' => $ this ->layer ,
161
167
'itemDataBuilder ' => $ this ->itemDataBuilder ,
162
168
'filterItemFactory ' => $ this ->filterItemFactory ,
163
- 'escaper ' => $ escaper
169
+ 'escaper ' => $ escaper ,
170
+ 'priceCurrency ' => $ priceFormatter ,
164
171
]
165
172
);
166
173
}
@@ -181,7 +188,13 @@ public function testApplyWithEmptyRequest(?int $requestValue, $idValue): void
181
188
182
189
$ this ->request
183
190
->method ('getParam ' )
184
- ->with ($ requestField );
191
+ ->with ($ requestField )
192
+ ->willReturnMap (
193
+ [
194
+ [$ requestField , $ requestValue ],
195
+ [$ idField , $ idValue ],
196
+ ]
197
+ );
185
198
186
199
$ result = $ this ->target ->apply ($ this ->request );
187
200
$ this ->assertSame ($ this ->target , $ result );
@@ -209,34 +222,60 @@ public function applyWithEmptyRequestDataProvider(): array
209
222
}
210
223
211
224
/**
212
- * @return void
213
- */
214
- public function testApply (): void
225
+ * @dataProvider applyDataProvider
226
+ */
227
+ public function testApply (string $ filter , array $ expected ): void
215
228
{
216
- $ priceId = '15-50 ' ;
217
- $ requestVar = 'test_request_var ' ;
218
-
219
- $ this ->target ->setRequestVar ($ requestVar );
229
+ $ requestVar = 'price ' ;
220
230
$ this ->request ->expects ($ this ->exactly (1 ))
221
231
->method ('getParam ' )
222
- ->willReturnCallback (
223
- function ($ field ) use ($ requestVar , $ priceId ) {
224
- $ this ->assertContains ($ field , [$ requestVar , 'id ' ]);
225
- return $ priceId ;
226
- }
227
- );
232
+ ->with ($ requestVar )
233
+ ->willReturn ($ filter );
228
234
229
235
$ this ->fulltextCollection ->expects ($ this ->once ())
230
236
->method ('addFieldToFilter ' )
231
237
->with ('price ' )->willReturnSelf ();
232
238
233
239
$ this ->target ->setCurrencyRate (1 );
234
240
$ this ->target ->apply ($ this ->request );
241
+ $ actual = [];
242
+ foreach ($ this ->state ->getFilters () as $ item ) {
243
+ $ actual [] = ['label ' => $ item ->getLabel (), 'value ' => $ item ->getValue (), 'count ' => $ item ->getCount ()];
244
+ }
245
+
246
+ $ this ->assertEquals ($ expected , $ actual );
235
247
}
236
248
237
249
/**
238
- * @return void
239
- */
250
+ * @return array
251
+ */
252
+ public function applyDataProvider (): array
253
+ {
254
+ return [
255
+ [
256
+ '10-50 ' ,
257
+ [
258
+ ['label ' => '$10.00 - $49.99 ' , 'value ' => ['10 ' , '50 ' ], 'count ' => '0 ' ],
259
+ ]
260
+ ],
261
+ [
262
+ '-50 ' ,
263
+ [
264
+ ['label ' => '$0.00 - $49.99 ' , 'value ' => ['' , '50 ' ], 'count ' => '0 ' ],
265
+ ]
266
+ ],
267
+ [
268
+ '10- ' ,
269
+ [
270
+ ['label ' => '$10.00 and above ' , 'value ' => ['10 ' , '' ], 'count ' => '0 ' ],
271
+ ]
272
+ ]
273
+ ];
274
+ }
275
+
276
+ /**
277
+ * @return void
278
+ */
240
279
public function testGetItems (): void
241
280
{
242
281
$ this ->target ->setAttributeModel ($ this ->attribute );
0 commit comments