1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Provider; |
4
|
|
|
|
5
|
|
|
use OroCRM\Bundle\MagentoBundle\Provider\BatchFilterBag; |
6
|
|
|
|
7
|
|
|
class BatchFilterBagTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** @var BatchFilterBag */ |
10
|
|
|
protected $filter; |
11
|
|
|
|
12
|
|
|
protected function setUp() |
13
|
|
|
{ |
14
|
|
|
$this->filter = new BatchFilterBag(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function testFilters() |
18
|
|
|
{ |
19
|
|
|
// add only last id filter |
20
|
|
|
$this->filter->addLastIdFilter(1); |
21
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
22
|
|
|
$this->assertCount(1, $filters['complex_filter']); |
23
|
|
|
$this->assertNotEmpty($filters['complex_filter'][0]); |
24
|
|
|
|
25
|
|
|
// add date filter in initial mode |
26
|
|
|
$this->filter->addDateFilter('created_at', 'to', new \DateTime()); |
27
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
28
|
|
|
$this->assertCount(2, $filters['complex_filter']); |
29
|
|
|
$this->assertNotEmpty($filters['complex_filter'][1]); |
30
|
|
|
$this->assertContains('created_at', $filters['complex_filter'][1]); |
31
|
|
|
$this->assertEquals('to', $filters['complex_filter'][1]['value']['key']); |
32
|
|
|
|
33
|
|
|
$this->filter->addDateFilter('updated_at', 'from', new \DateTime()); |
34
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
35
|
|
|
$this->assertCount(3, $filters['complex_filter']); // still should be two filters |
36
|
|
|
$this->assertContains('updated_at', $filters['complex_filter'][2]); |
37
|
|
|
$this->assertEquals('from', $filters['complex_filter'][2]['value']['key']); |
38
|
|
|
|
39
|
|
|
// add website filter |
40
|
|
|
$this->filter->addWebsiteFilter([1]); |
41
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
42
|
|
|
$this->assertCount(4, $filters['complex_filter']); |
43
|
|
|
$this->assertContains('website_id', $filters['complex_filter'][3]); |
44
|
|
|
|
45
|
|
|
// add store filter |
46
|
|
|
$this->filter->addStoreFilter([1]); |
47
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
48
|
|
|
$this->assertCount(5, $filters['complex_filter']); |
49
|
|
|
$this->assertContains('store_id', $filters['complex_filter'][4]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testReset() |
53
|
|
|
{ |
54
|
|
|
// add only last id filter |
55
|
|
|
$this->filter->addLastIdFilter(1); |
56
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
57
|
|
|
$this->assertCount(1, $filters['complex_filter']); |
58
|
|
|
|
59
|
|
|
// add date filter in initial mode |
60
|
|
|
$this->filter->addDateFilter('created_at', 'to', new \DateTime()); |
61
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
62
|
|
|
$this->assertCount(2, $filters['complex_filter']); |
63
|
|
|
|
64
|
|
|
// add dummy simple filter |
65
|
|
|
$this->filter->addFilter('test', ['test' => true]); |
66
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
67
|
|
|
$this->assertCount(1, $filters['filter']); |
68
|
|
|
|
69
|
|
|
// reset only simple filter |
70
|
|
|
$this->filter->reset('filter'); |
71
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
72
|
|
|
$this->assertArrayNotHasKey('filter', $filters); |
73
|
|
|
$this->filter->reset(); |
74
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
75
|
|
|
$this->assertArrayNotHasKey('complex_filter', $filters); |
76
|
|
|
$this->assertArrayNotHasKey('filter', $filters); |
77
|
|
|
|
78
|
|
|
// add dummy simple filter |
79
|
|
|
$this->filter->addFilter('test', ['test' => true]); |
80
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
81
|
|
|
$this->assertCount(1, $filters['filter']); |
82
|
|
|
|
83
|
|
|
$this->filter->reset(BatchFilterBag::FILTER_TYPE_SIMPLE, 'test'); |
84
|
|
|
$filters = $this->getAppliedFilters($this->filter); |
85
|
|
|
$this->assertFalse(isset($filters['filter']['test'])); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testConstructAddition() |
89
|
|
|
{ |
90
|
|
|
$testFilters = ['testField1' => 'testValue1']; |
91
|
|
|
$testComplexFilters = ['testFieldComplex' => ['key' => 'in', 'value' => 'some, list, of, values']]; |
92
|
|
|
|
93
|
|
|
$bag = new BatchFilterBag($testFilters, $testComplexFilters); |
94
|
|
|
|
95
|
|
|
$result = $bag->getAppliedFilters(); |
96
|
|
|
|
97
|
|
|
$expected = [ |
98
|
|
|
'filters' => [ |
99
|
|
|
'filter' => [['key' => 'testField1', 'value' => 'testValue1']], |
100
|
|
|
'complex_filter' => [ |
101
|
|
|
[ |
102
|
|
|
'key' => 'testFieldComplex', |
103
|
|
|
'value' => [ |
104
|
|
|
'key' => 'in', |
105
|
|
|
'value' => 'some, list, of, values', |
106
|
|
|
] |
107
|
|
|
] |
108
|
|
|
] |
109
|
|
|
] |
110
|
|
|
]; |
111
|
|
|
$this->assertSame($expected, $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testMerge() |
115
|
|
|
{ |
116
|
|
|
$testFilters = ['testField1' => 'testValue1']; |
117
|
|
|
$testComplexFilters = ['testFieldComplex' => ['key' => 'in', 'value' => 'some, list, of, values']]; |
118
|
|
|
$bag = new BatchFilterBag($testFilters, $testComplexFilters); |
119
|
|
|
|
120
|
|
|
$testFilters2 = ['testField1' => 'Overriden', 'testField2' => 'testValue2']; |
121
|
|
|
$testComplexFilters2 = ['testField2Complex' => ['key' => 'gt', 'value' => 3]]; |
122
|
|
|
$bag2 = new BatchFilterBag($testFilters2, $testComplexFilters2); |
123
|
|
|
|
124
|
|
|
$bag->merge($bag2); |
125
|
|
|
$result = $bag->getAppliedFilters(); |
126
|
|
|
|
127
|
|
|
$expected = [ |
128
|
|
|
'filters' => [ |
129
|
|
|
'filter' => [ |
130
|
|
|
['key' => 'testField1', 'value' => 'Overriden'], |
131
|
|
|
['key' => 'testField2', 'value' => 'testValue2'] |
132
|
|
|
], |
133
|
|
|
'complex_filter' => [ |
134
|
|
|
[ |
135
|
|
|
'key' => 'testFieldComplex', |
136
|
|
|
'value' => [ |
137
|
|
|
'key' => 'in', |
138
|
|
|
'value' => 'some, list, of, values', |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
'key' => 'testField2Complex', |
143
|
|
|
'value' => [ |
144
|
|
|
'key' => 'gt', |
145
|
|
|
'value' => 3, |
146
|
|
|
] |
147
|
|
|
] |
148
|
|
|
] |
149
|
|
|
] |
150
|
|
|
]; |
151
|
|
|
$this->assertSame($expected, $result); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $filters |
156
|
|
|
* @param array $complexFilters |
157
|
|
|
* @param array $expectedBeforeReset |
158
|
|
|
* @param array $expectedAfterReset |
159
|
|
|
* |
160
|
|
|
* @dataProvider providerResetFilterWithEmptyValue |
161
|
|
|
*/ |
162
|
|
|
public function testResetFilterWithEmptyValue($filters, $complexFilters, $expectedBeforeReset, $expectedAfterReset) |
163
|
|
|
{ |
164
|
|
|
$bag = new BatchFilterBag($filters, $complexFilters); |
165
|
|
|
$filters = $bag->getAppliedFilters(); |
166
|
|
|
self::assertEquals($expectedBeforeReset, $filters); |
167
|
|
|
|
168
|
|
|
$bag->resetFilterWithEmptyValue(); |
169
|
|
|
$filters = $bag->getAppliedFilters(); |
170
|
|
|
self::assertEquals($expectedAfterReset, $filters); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Data provider for testResetFilterWithEmptyValue |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function providerResetFilterWithEmptyValue() |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
'all filters have empty value' => [ |
182
|
|
|
'filters' => [ |
183
|
|
|
'testField1' => null |
184
|
|
|
], |
185
|
|
|
'complexFilters' => [ |
186
|
|
|
'testFieldComplex' => null |
187
|
|
|
], |
188
|
|
|
'expectedBeforeReset' => [ |
189
|
|
|
'filters' => [ |
190
|
|
|
'filter' => [ |
191
|
|
|
['key' => 'testField1', 'value' => null] |
192
|
|
|
], |
193
|
|
|
'complex_filter' => [ |
194
|
|
|
['key' => 'testFieldComplex', 'value' => null] |
195
|
|
|
], |
196
|
|
|
] |
197
|
|
|
], |
198
|
|
|
'expectedAfterReset' => [ |
199
|
|
|
'filters' => [] |
200
|
|
|
] |
201
|
|
|
], |
202
|
|
|
'one filter with empty value' => [ |
203
|
|
|
'filters' => [ |
204
|
|
|
'testField1' => 'testValue1', |
205
|
|
|
'testField2' => null, |
206
|
|
|
], |
207
|
|
|
'complexFilters' => [ |
208
|
|
|
'testFieldComplex' => 'testValue1' |
209
|
|
|
], |
210
|
|
|
'expectedBeforeReset' => [ |
211
|
|
|
'filters' => [ |
212
|
|
|
'filter' => [ |
213
|
|
|
['key' => 'testField1', 'value' => 'testValue1'], |
214
|
|
|
['key' => 'testField2', 'value' => null] |
215
|
|
|
], |
216
|
|
|
'complex_filter' => [ |
217
|
|
|
['key' => 'testFieldComplex', 'value' => 'testValue1'] |
218
|
|
|
], |
219
|
|
|
] |
220
|
|
|
], |
221
|
|
|
'expectedAfterReset' => [ |
222
|
|
|
'filters' => [ |
223
|
|
|
'filter' => [ |
224
|
|
|
['key' => 'testField1', 'value' => 'testValue1'] |
225
|
|
|
], |
226
|
|
|
'complex_filter' => [ |
227
|
|
|
['key' => 'testFieldComplex', 'value' => 'testValue1'] |
228
|
|
|
], |
229
|
|
|
] |
230
|
|
|
] |
231
|
|
|
], |
232
|
|
|
'one complex_filter with empty value' => [ |
233
|
|
|
'filters' => [ |
234
|
|
|
'testField1' => 'testValue1', |
235
|
|
|
'testField2' => 'testValue2' |
236
|
|
|
], |
237
|
|
|
'complexFilters' => [ |
238
|
|
|
'testFieldComplex' => 'testValue1', |
239
|
|
|
'testFieldComplex2' => null |
240
|
|
|
], |
241
|
|
|
'expectedBeforeReset' => [ |
242
|
|
|
'filters' => [ |
243
|
|
|
'filter' => [ |
244
|
|
|
['key' => 'testField1', 'value' => 'testValue1'], |
245
|
|
|
['key' => 'testField2', 'value' => 'testValue2'] |
246
|
|
|
], |
247
|
|
|
'complex_filter' => [ |
248
|
|
|
['key' => 'testFieldComplex', 'value' => 'testValue1'], |
249
|
|
|
['key' => 'testFieldComplex2', 'value' => null] |
250
|
|
|
] |
251
|
|
|
], |
252
|
|
|
], |
253
|
|
|
'expectedAfterReset' => [ |
254
|
|
|
'filters' => [ |
255
|
|
|
'filter' => [ |
256
|
|
|
['key' => 'testField1', 'value' => 'testValue1'], |
257
|
|
|
['key' => 'testField2', 'value' => 'testValue2'] |
258
|
|
|
], |
259
|
|
|
'complex_filter' => [ |
260
|
|
|
['key' => 'testFieldComplex', 'value' => 'testValue1'] |
261
|
|
|
] |
262
|
|
|
] |
263
|
|
|
] |
264
|
|
|
] |
265
|
|
|
]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param BatchFilterBag $bag |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
protected function getAppliedFilters(BatchFilterBag $bag) |
274
|
|
|
{ |
275
|
|
|
$filters = $bag->getAppliedFilters(); |
276
|
|
|
$this->assertArrayHasKey('filters', $filters); |
277
|
|
|
$filters = $filters['filters']; |
278
|
|
|
|
279
|
|
|
return $filters; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|