@@ -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
}
@@ -209,29 +216,55 @@ public function applyWithEmptyRequestDataProvider(): array
209
216
}
210
217
211
218
/**
212
- * @return void
213
- */
214
- public function testApply (): void
219
+ * @dataProvider applyDataProvider
220
+ */
221
+ public function testApply (string $ filter , array $ expected ): void
215
222
{
216
- $ priceId = '15-50 ' ;
217
- $ requestVar = 'test_request_var ' ;
218
-
219
- $ this ->target ->setRequestVar ($ requestVar );
223
+ $ requestVar = 'price ' ;
220
224
$ this ->request ->expects ($ this ->exactly (1 ))
221
225
->method ('getParam ' )
222
- ->willReturnCallback (
223
- function ($ field ) use ($ requestVar , $ priceId ) {
224
- $ this ->assertContains ($ field , [$ requestVar , 'id ' ]);
225
- return $ priceId ;
226
- }
227
- );
226
+ ->with ($ requestVar )
227
+ ->willReturn ($ filter );
228
228
229
229
$ this ->fulltextCollection ->expects ($ this ->once ())
230
230
->method ('addFieldToFilter ' )
231
231
->with ('price ' )->willReturnSelf ();
232
232
233
233
$ this ->target ->setCurrencyRate (1 );
234
234
$ this ->target ->apply ($ this ->request );
235
+ $ actual = [];
236
+ foreach ($ this ->state ->getFilters () as $ item ) {
237
+ $ actual [] = ['label ' => $ item ->getLabel (), 'value ' => $ item ->getValue (), 'count ' => $ item ->getCount ()];
238
+ }
239
+
240
+ $ this ->assertEquals ($ expected , $ actual );
241
+ }
242
+
243
+ /**
244
+ * @return array
245
+ */
246
+ public function applyDataProvider (): array
247
+ {
248
+ return [
249
+ [
250
+ '10-50 ' ,
251
+ [
252
+ ['label ' => '$10.00 - $49.99 ' , 'value ' => ['10 ' , '50 ' ], 'count ' => '0 ' ],
253
+ ]
254
+ ],
255
+ [
256
+ '-50 ' ,
257
+ [
258
+ ['label ' => '$0.00 - $49.99 ' , 'value ' => ['' , '50 ' ], 'count ' => '0 ' ],
259
+ ]
260
+ ],
261
+ [
262
+ '10- ' ,
263
+ [
264
+ ['label ' => '$10.00 and above ' , 'value ' => ['10 ' , '' ], 'count ' => '0 ' ],
265
+ ]
266
+ ]
267
+ ];
235
268
}
236
269
237
270
/**
0 commit comments