Skip to content

Commit ab118d0

Browse files
committed
OXDEV-9583: Add matches-method to test filter against value for some filters
1 parent 6c44dbb commit ab118d0

9 files changed

Lines changed: 158 additions & 2 deletions

File tree

src/DataType/Filter/BoolFilter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function equals(): bool
2929
return $this->equals;
3030
}
3131

32+
public function matches(mixed $value): bool
33+
{
34+
return (is_bool($value) || $value === $this->equals);
35+
}
36+
3237
public function addToQuery(QueryBuilder $builder, string $field): void
3338
{
3439
/** @var array $from */

src/DataType/Filter/FilterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
interface FilterInterface
1515
{
1616
public function addToQuery(QueryBuilder $builder, string $field): void;
17+
18+
public function matches(mixed $value): bool;
1719
}

src/DataType/Filter/IDFilter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public function equals(): ID
2727
return $this->equals;
2828
}
2929

30+
public function matches(mixed $value): bool
31+
{
32+
if ($value instanceof ID) {
33+
$value = $value->val();
34+
}
35+
36+
return (is_string($value) && $this->equals->val() === $value);
37+
}
38+
3039
public function addToQuery(QueryBuilder $builder, string $field): void
3140
{
3241
/** @var array $from */

src/DataType/Filter/IntegerFilter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ public function between(): ?array
5151
return $this->between;
5252
}
5353

54+
public function matches(mixed $value): bool
55+
{
56+
if (!is_int($value)) {
57+
return false;
58+
}
59+
60+
if ($this->equals !== null && $value !== $this->equals) {
61+
return false;
62+
}
63+
64+
if ($this->lessThan !== null && $value >= $this->lessThan) {
65+
return false;
66+
}
67+
68+
if ($this->greaterThan !== null && $value <= $this->greaterThan) {
69+
return false;
70+
}
71+
72+
if ($this->between !== null && ($value <= $this->between[0] || $value >= $this->between[1])) {
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
5479
/**
5580
* @Factory(name="IntegerFilterInput", default=true)
5681
*

src/DataType/Filter/StringFilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public function contains(): ?string
4242
return $this->contains;
4343
}
4444

45-
public function matches(string $value): bool
45+
public function matches(mixed $value): bool
4646
{
47+
if (!is_string($value)) {
48+
return false;
49+
}
50+
4751
if ($this->equals !== null && strcasecmp($value, $this->equals) !== 0) {
4852
return false;
4953
}

tests/Unit/DataType/Filter/BoolFilterTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,12 @@ public function testAddQueryPartWithAlias(): void
8686

8787
$this->assertEquals('db_table_alias.DB_FIELD = :db_field', (string)$where);
8888
}
89+
90+
public function testMatches(): void
91+
{
92+
$filter = new BoolFilter(false);
93+
$this->assertTrue($filter->matches(false));
94+
$this->assertFalse($filter->matches(true));
95+
$this->assertFalse($filter->matches('false string'));
96+
}
8997
}

tests/Unit/DataType/Filter/IDFilterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,46 @@ public function testAddQueryPartWithAlias(): void
7878

7979
$this->assertEquals('db_table_alias.DB_FIELD = :db_field', (string)$where);
8080
}
81+
82+
/** @dataProvider matchesDataProvider */
83+
public function testMatches(mixed $value, IDFilter $filter, bool $expected): void
84+
{
85+
$this->assertSame(
86+
$expected,
87+
$filter->matches($value)
88+
);
89+
}
90+
91+
public static function matchesDataProvider(): \Generator
92+
{
93+
yield "test string is matching" => [
94+
'value' => '1abc',
95+
'initFilter' => new IDFilter(equals: new ID('1abc')),
96+
'result' => true,
97+
];
98+
99+
yield "test id class is matching" => [
100+
'value' => new ID('id_text'),
101+
'initFilter' => new IDFilter(equals: new ID('id_text')),
102+
'result' => true,
103+
];
104+
105+
yield "test no string or no ID-class is not matching" => [
106+
'value' => 123,
107+
'initFilter' => new IDFilter(equals: new ID('123')),
108+
'result' => false,
109+
];
110+
111+
yield "test wrong string is not matching" => [
112+
'value' => 'wrong1abc',
113+
'initFilter' => new IDFilter(equals: new ID('1abc')),
114+
'result' => false,
115+
];
116+
117+
yield "test wrong ID-class is not matching" => [
118+
'value' => new ID('wrong_id_text'),
119+
'initFilter' => new IDFilter(equals: new ID('id_text')),
120+
'result' => false,
121+
];
122+
}
81123
}

tests/Unit/DataType/Filter/IntegerFilterTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,59 @@ public function testAddQueryPartWithAlias(): void
190190

191191
$this->assertEquals('db_table_alias.DB_FIELD = :db_field_eq', (string)$where);
192192
}
193+
194+
/** @dataProvider matchesDataProvider */
195+
public function testMatches(
196+
int $stringForTrueCase,
197+
mixed $stringForFalseCase,
198+
IntegerFilter $initFilter
199+
): void {
200+
$this->assertTrue($initFilter->matches($stringForTrueCase));
201+
$this->assertFalse($initFilter->matches($stringForFalseCase));
202+
}
203+
204+
public static function matchesDataProvider(): \Generator
205+
{
206+
yield "test match equals" => [
207+
'stringForTrueCase' => 1,
208+
'stringForFalseCase' => 2,
209+
'initFilter' => new IntegerFilter(equals: 1)
210+
];
211+
212+
yield "test match contains" => [
213+
'stringForTrueCase' => 1,
214+
'stringForFalseCase' => 5,
215+
'initFilter' => new IntegerFilter(lessThan: 3)
216+
];
217+
218+
yield "test match begins with" => [
219+
'stringForTrueCase' => 6,
220+
'stringForFalseCase' => 2,
221+
'initFilter' => new IntegerFilter(greaterThan: 4)
222+
];
223+
224+
yield "test match begins with and contains" => [
225+
'stringForTrueCase' => 5,
226+
'stringForFalseCase' => 2,
227+
'initFilter' => new IntegerFilter(between: [3, 5])
228+
];
229+
230+
yield "test match equals and contains" => [
231+
'stringForTrueCase' => 7,
232+
'stringForFalseCase' => 3,
233+
'initFilter' => new IntegerFilter(lessThan: 10, greaterThan: 4)
234+
];
235+
236+
yield "test is and is not integer" => [
237+
'stringForTrueCase' => 2,
238+
'stringForFalseCase' => 2.1,
239+
'initFilter' => new IntegerFilter(equals: 2)
240+
];
241+
242+
yield "test is and is not integer with string" => [
243+
'stringForTrueCase' => 3,
244+
'stringForFalseCase' => '3',
245+
'initFilter' => new IntegerFilter(equals: 3)
246+
];
247+
}
193248
}

tests/Unit/DataType/Filter/StringFilterTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testAddQueryPartWithAlias(): void
148148
/** @dataProvider matchesDataProvider */
149149
public function testMatches(
150150
string $stringForTrueCase,
151-
string $stringForFalseCase,
151+
mixed $stringForFalseCase,
152152
StringFilter $initFilter
153153
): void {
154154
$this->assertTrue($initFilter->matches($stringForTrueCase));
@@ -186,5 +186,11 @@ public static function matchesDataProvider(): \Generator
186186
'stringForFalseCase' => 'this is not abc',
187187
'initFilter' => new StringFilter(equals: 'This Is Abc', contains: 'ABC')
188188
];
189+
190+
yield "test is and is not string" => [
191+
'stringForTrueCase' => 'This is abc',
192+
'stringForFalseCase' => 23,
193+
'initFilter' => new StringFilter(equals: '23')
194+
];
189195
}
190196
}

0 commit comments

Comments
 (0)