1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/array-filter |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/array-filter/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/array-filter |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\ArrayFilter\Test\Unit\Factory; |
15
|
|
|
|
16
|
|
|
use Graze\ArrayFilter\ArrayFilterInterface; |
17
|
|
|
use Graze\ArrayFilter\Exception\UnknownPropertyDefinitionException; |
18
|
|
|
use Graze\ArrayFilter\Factory\FilterFactory; |
19
|
|
|
use Graze\ArrayFilter\Factory\FilterFactoryInterface; |
20
|
|
|
use Graze\ArrayFilter\Factory\ValueFactoryInterface; |
21
|
|
|
use Graze\ArrayFilter\Test\TestCase; |
22
|
|
|
use Mockery as m; |
23
|
|
|
use Mockery\MockInterface; |
24
|
|
|
|
25
|
|
|
class FilterFactoryTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var FilterFactory |
29
|
|
|
*/ |
30
|
|
|
protected $factory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ValueFactoryInterface|MockInterface |
34
|
|
|
*/ |
35
|
|
|
private $valueFactory; |
36
|
|
|
|
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->valueFactory = m::mock(ValueFactoryInterface::class); |
40
|
|
|
$this->factory = new FilterFactory($this->valueFactory); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testInstanceOf() |
44
|
|
|
{ |
45
|
|
|
static::assertInstanceOf( |
46
|
|
|
FilterFactoryInterface::class, |
47
|
|
|
$this->factory |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider getFilterData |
53
|
|
|
* |
54
|
|
|
* @param string $property |
55
|
|
|
* @param mixed $expected |
56
|
|
|
* @param array $metadata |
57
|
|
|
* @param bool $result |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testGetFilter($property, $expected, array $metadata, $result) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (is_string($expected)) { |
62
|
|
|
$this->valueFactory->shouldReceive('parseValue') |
63
|
|
|
->with($expected) |
64
|
|
|
->once() |
65
|
|
|
->andReturn($expected); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$filter = $this->factory->getFilter($property, $expected); |
69
|
|
|
static::assertEquals( |
70
|
|
|
$result, |
71
|
|
|
$filter->matches($metadata), |
72
|
|
|
sprintf( |
73
|
|
|
"Expected %s and %s to %s for property: %s", |
74
|
|
|
json_encode($expected), |
75
|
|
|
json_encode($metadata), |
76
|
|
|
$result, |
77
|
|
|
$property |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getFilterData() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
['test', 'value', ['test' => 'value'], true], |
89
|
|
|
['test', 'value', ['test' => 'value2'], false], |
90
|
|
|
['test =', 'value', ['test' => 'value'], true], |
91
|
|
|
['test =', 'value', ['test' => 'value2'], false], |
92
|
|
|
['test >', 10, ['test' => 12], true], |
93
|
|
|
['test >', 12, ['test' => 10], false], |
94
|
|
|
['test >', 10, ['test' => 10], false], |
95
|
|
|
['test >=', 10, ['test' => 12], true], |
96
|
|
|
['test >=', 12, ['test' => 10], false], |
97
|
|
|
['test >=', 10, ['test' => 10], true], |
98
|
|
|
['test <', 10, ['test' => 12], false], |
99
|
|
|
['test <', 12, ['test' => 10], true], |
100
|
|
|
['test <', 10, ['test' => 10], false], |
101
|
|
|
['test <=', 10, ['test' => 12], false], |
102
|
|
|
['test <=', 12, ['test' => 10], true], |
103
|
|
|
['test <=', 10, ['test' => 10], true], |
104
|
|
|
['test !=', 'value', ['test' => 'other'], true], |
105
|
|
|
['test !=', 'value', ['test' => 'value'], false], |
106
|
|
|
['test <>', 'value', ['test' => 'other'], true], |
107
|
|
|
['test <>', 'value', ['test' => 'value'], false], |
108
|
|
|
['test ~', '/word\d+stuff/i', ['test' => 'word12346234stuFF'], true], |
109
|
|
|
['test ~', '/word\d+stuff/', ['test' => 'word12346234stuFF'], false], |
110
|
|
|
['test ~=', '/word\d+stuff/i', ['test' => 'word12346234stuFF'], true], |
111
|
|
|
['test ~=', '/word\d+stuff/', ['test' => 'word12346234stuFF'], false], |
112
|
|
|
['test in', ['1', '2'], ['test' => '1'], true], |
113
|
|
|
['test in', ['1', '2'], ['test' => '2'], true], |
114
|
|
|
['test in', ['1', '2'], ['test' => '3'], false], |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider invalidPropertyNames |
120
|
|
|
* |
121
|
|
|
* @param string $property |
122
|
|
|
*/ |
123
|
|
|
public function testGetFilterWillThrowExceptionWithInvalidProperty($property) |
124
|
|
|
{ |
125
|
|
|
$this->expectException(UnknownPropertyDefinitionException::class); |
126
|
|
|
|
127
|
|
|
$this->factory->getFilter($property, ''); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function invalidPropertyNames() |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
['test = '], |
137
|
|
|
['test jdksla'], |
138
|
|
|
[' something'], |
139
|
|
|
['test !!'], |
140
|
|
|
['stuff space ='], |
141
|
|
|
['things <<'], |
142
|
|
|
['stuff >>'], |
143
|
|
|
[''], |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @dataProvider createFiltersTestData |
149
|
|
|
* |
150
|
|
|
* @param array $configuration |
151
|
|
|
* @param array $metadata |
152
|
|
|
* @param bool $result |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function testCreateFilters(array $configuration, array $metadata, $result) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
foreach ($configuration as $property => $value) { |
157
|
|
|
$this->valueFactory->shouldReceive('parseValue') |
158
|
|
|
->with($value) |
159
|
|
|
->andReturn($value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$filter = $this->factory->createFilters($configuration); |
163
|
|
|
static::assertEquals( |
164
|
|
|
$result, |
165
|
|
|
$filter->matches($metadata), |
166
|
|
|
sprintf( |
167
|
|
|
"Expected configuration: %s and data: %s to result in: %s", |
168
|
|
|
json_encode($configuration), |
169
|
|
|
json_encode($metadata), |
170
|
|
|
$result |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function createFiltersTestData() |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
[['test' => 'value'], ['test' => 'value'], true], |
182
|
|
|
[['test' => 'value', 'test2' => 'value'], ['test' => 'value', 'test2' => 'value'], true], |
183
|
|
|
[['test' => 'value', 'test2' => 'value'], ['test' => 'value', 'test2' => 'value2'], false], |
184
|
|
|
[['test >' => 4, 'test <' => 8], ['test' => 6], true], |
185
|
|
|
[['test >' => 4, 'test <' => 8], ['test' => 2], false], |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testStaticAccessors() |
190
|
|
|
{ |
191
|
|
|
$filter = FilterFactory::filter('name', 'value'); |
192
|
|
|
static::assertInstanceOf(ArrayFilterInterface::class, $filter); |
193
|
|
|
static::assertTrue($filter->matches(['name' => 'value'])); |
194
|
|
|
static::assertFalse($filter->matches(['name' => 'value2'])); |
195
|
|
|
|
196
|
|
|
$filter = FilterFactory::fromConfiguration([ |
197
|
|
|
'name' => 'value2', |
198
|
|
|
]); |
199
|
|
|
static::assertInstanceOf(ArrayFilterInterface::class, $filter); |
200
|
|
|
static::assertTrue($filter->matches(['name' => 'value2'])); |
201
|
|
|
static::assertFalse($filter->matches(['name' => 'value3'])); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.