Skip to content

Commit d679a90

Browse files
committed
OXDEV-9583: Add matches method to DateFilter
1 parent 6c3abe6 commit d679a90

5 files changed

Lines changed: 128 additions & 68 deletions

File tree

src/DataType/Filter/DateFilter.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ function ($date) use ($zone) {
118118

119119
public function matches(mixed $value): bool
120120
{
121-
// TODO: Implement matches() method.
121+
if (!$value instanceof DateTimeInterface) {
122+
return false;
123+
}
124+
125+
if ($this->equals !== null && $value != $this->equals) {
126+
return false;
127+
}
128+
129+
if ($this->between !== null && !($value >= $this->between[0] && $value <= $this->between[1])) {
130+
return false;
131+
}
132+
133+
return true;
122134
}
123135
}

tests/Unit/DataType/Filter/DateFilterTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace OxidEsales\GraphQL\Base\Tests\Unit\DataType\Filter;
1111

1212
use DateTime;
13+
use DateTimeImmutable;
14+
use DateTimeInterface;
1315
use Doctrine\DBAL\Query\Expression\CompositeExpression;
1416
use Exception;
1517
use InvalidArgumentException;
@@ -177,4 +179,50 @@ public function testAddQueryPartWithAlias(): void
177179

178180
$this->assertEquals('db_table_alias.DB_FIELD = :db_field_eq', (string)$where);
179181
}
182+
183+
/** @dataProvider matchesDataProvider */
184+
public function testMatches(
185+
DateTimeInterface $trueCase,
186+
mixed $falseCase,
187+
DateFilter $filter
188+
): void {
189+
$this->assertTrue($filter->matches($trueCase));
190+
$this->assertFalse($filter->matches($falseCase));
191+
}
192+
193+
public static function matchesDataProvider(): \Generator
194+
{
195+
yield "test match equals" => [
196+
'trueCase' => new DateTimeImmutable('2020-01-30 12:37:21'),
197+
'falseCase' => new DateTimeImmutable('2020-01-30 13:47:31'),
198+
'filter' => new DateFilter(equals: new DateTimeImmutable('2020-01-30 12:37:21'))
199+
];
200+
201+
yield "test match between" => [
202+
'trueCase' => new DateTimeImmutable('2020-02-10 12:37:21'),
203+
'falseCase' => new DateTimeImmutable('2020-04-10 12:37:21'),
204+
'filter' => new DateFilter(between: [
205+
new DateTimeImmutable('2020-01-30 12:37:21'),
206+
new DateTimeImmutable('2020-02-30 12:37:22')
207+
])
208+
];
209+
210+
yield "test match equals and between" => [
211+
'trueCase' => new DateTimeImmutable('2020-03-20 12:37:21'),
212+
'falseCase' => new DateTimeImmutable('2020-04-20 12:37:21'),
213+
'filter' => new DateFilter(
214+
equals: new DateTimeImmutable('2020-03-20 12:37:21'),
215+
between: [
216+
new DateTimeImmutable('2020-01-30 12:37:21'),
217+
new DateTimeImmutable('2020-05-30 12:37:22')
218+
]
219+
)
220+
];
221+
222+
yield "test match is DateTime instance" => [
223+
'trueCase' => new DateTimeImmutable('2020-03-20 12:37:21'),
224+
'falseCase' => '2020-03-20 12:37:21',
225+
'filter' => new DateFilter(equals: new DateTimeImmutable('2020-03-20 12:37:21'))
226+
];
227+
}
180228
}

tests/Unit/DataType/Filter/FloatFilterTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,55 +229,55 @@ public function testAddQueryPartWithAlias(): void
229229

230230
/** @dataProvider matchesDataProvider */
231231
public function testMatches(
232-
float $stringForTrueCase,
233-
mixed $stringForFalseCase,
232+
float $trueCase,
233+
mixed $falseCase,
234234
FloatFilter $filter
235235
): void {
236-
$this->assertTrue($filter->matches($stringForTrueCase));
237-
$this->assertFalse($filter->matches($stringForFalseCase));
236+
$this->assertTrue($filter->matches($trueCase));
237+
$this->assertFalse($filter->matches($falseCase));
238238
}
239239

240240
public static function matchesDataProvider(): \Generator
241241
{
242242
yield "test match equals" => [
243-
'stringForTrueCase' => 1.0,
244-
'stringForFalseCase' => 2.0,
243+
'trueCase' => 1.0,
244+
'falseCase' => 2.0,
245245
'filter' => new FloatFilter(equals: 1.0)
246246
];
247247

248248
yield "test match less than" => [
249-
'stringForTrueCase' => 1.3,
250-
'stringForFalseCase' => 5.4,
249+
'trueCase' => 1.3,
250+
'falseCase' => 5.4,
251251
'filter' => new FloatFilter(lessThan: 3.0)
252252
];
253253

254254
yield "test match greater than" => [
255-
'stringForTrueCase' => 6.6,
256-
'stringForFalseCase' => 2.3,
255+
'trueCase' => 6.6,
256+
'falseCase' => 2.3,
257257
'filter' => new FloatFilter(greaterThan: 4.5)
258258
];
259259

260260
yield "test match between" => [
261-
'stringForTrueCase' => 5.3,
262-
'stringForFalseCase' => 2.7,
261+
'trueCase' => 5.3,
262+
'falseCase' => 2.7,
263263
'filter' => new FloatFilter(between: [3.1, 5.3])
264264
];
265265

266266
yield "test match less and greater than" => [
267-
'stringForTrueCase' => 7.8,
268-
'stringForFalseCase' => 3.3,
267+
'trueCase' => 7.8,
268+
'falseCase' => 3.3,
269269
'filter' => new FloatFilter(lessThan: 10.5, greaterThan: 4.5)
270270
];
271271

272272
yield "test not matches with float" => [
273-
'stringForTrueCase' => 2.5,
274-
'stringForFalseCase' => 2,
273+
'trueCase' => 2.5,
274+
'falseCase' => 2,
275275
'filter' => new FloatFilter(equals: 2.5)
276276
];
277277

278278
yield "test not matches with string" => [
279-
'stringForTrueCase' => 3.6,
280-
'stringForFalseCase' => '3,6',
279+
'trueCase' => 3.6,
280+
'falseCase' => '3,6',
281281
'filter' => new FloatFilter(equals: 3.6)
282282
];
283283
}

tests/Unit/DataType/Filter/IntegerFilterTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -193,56 +193,56 @@ public function testAddQueryPartWithAlias(): void
193193

194194
/** @dataProvider matchesDataProvider */
195195
public function testMatches(
196-
int $stringForTrueCase,
197-
mixed $stringForFalseCase,
198-
IntegerFilter $initFilter
196+
int $trueCase,
197+
mixed $falseCase,
198+
IntegerFilter $filter
199199
): void {
200-
$this->assertTrue($initFilter->matches($stringForTrueCase));
201-
$this->assertFalse($initFilter->matches($stringForFalseCase));
200+
$this->assertTrue($filter->matches($trueCase));
201+
$this->assertFalse($filter->matches($falseCase));
202202
}
203203

204204
public static function matchesDataProvider(): \Generator
205205
{
206206
yield "test match equals" => [
207-
'stringForTrueCase' => 1,
208-
'stringForFalseCase' => 2,
209-
'initFilter' => new IntegerFilter(equals: 1)
207+
'trueCase' => 1,
208+
'falseCase' => 2,
209+
'filter' => new IntegerFilter(equals: 1)
210210
];
211211

212212
yield "test match less than" => [
213-
'stringForTrueCase' => 1,
214-
'stringForFalseCase' => 5,
215-
'initFilter' => new IntegerFilter(lessThan: 3)
213+
'trueCase' => 1,
214+
'falseCase' => 5,
215+
'filter' => new IntegerFilter(lessThan: 3)
216216
];
217217

218218
yield "test match greater than" => [
219-
'stringForTrueCase' => 6,
220-
'stringForFalseCase' => 2,
221-
'initFilter' => new IntegerFilter(greaterThan: 4)
219+
'trueCase' => 6,
220+
'falseCase' => 2,
221+
'filter' => new IntegerFilter(greaterThan: 4)
222222
];
223223

224224
yield "test match between" => [
225-
'stringForTrueCase' => 5,
226-
'stringForFalseCase' => 2,
227-
'initFilter' => new IntegerFilter(between: [3, 5])
225+
'trueCase' => 5,
226+
'falseCase' => 2,
227+
'filter' => new IntegerFilter(between: [3, 5])
228228
];
229229

230230
yield "test match less and greater than" => [
231-
'stringForTrueCase' => 7,
232-
'stringForFalseCase' => 3,
233-
'initFilter' => new IntegerFilter(lessThan: 10, greaterThan: 4)
231+
'trueCase' => 7,
232+
'falseCase' => 3,
233+
'filter' => new IntegerFilter(lessThan: 10, greaterThan: 4)
234234
];
235235

236236
yield "test not matches with float" => [
237-
'stringForTrueCase' => 2,
238-
'stringForFalseCase' => 2.1,
239-
'initFilter' => new IntegerFilter(equals: 2)
237+
'trueCase' => 2,
238+
'falseCase' => 2.1,
239+
'filter' => new IntegerFilter(equals: 2)
240240
];
241241

242242
yield "test not matches with string" => [
243-
'stringForTrueCase' => 3,
244-
'stringForFalseCase' => '3',
245-
'initFilter' => new IntegerFilter(equals: 3)
243+
'trueCase' => 3,
244+
'falseCase' => '3',
245+
'filter' => new IntegerFilter(equals: 3)
246246
];
247247
}
248248
}

tests/Unit/DataType/Filter/StringFilterTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,50 +147,50 @@ public function testAddQueryPartWithAlias(): void
147147

148148
/** @dataProvider matchesDataProvider */
149149
public function testMatches(
150-
string $stringForTrueCase,
151-
mixed $stringForFalseCase,
152-
StringFilter $initFilter
150+
string $trueCase,
151+
mixed $falseCase,
152+
StringFilter $filter
153153
): void {
154-
$this->assertTrue($initFilter->matches($stringForTrueCase));
155-
$this->assertFalse($initFilter->matches($stringForFalseCase));
154+
$this->assertTrue($filter->matches($trueCase));
155+
$this->assertFalse($filter->matches($falseCase));
156156
}
157157

158158
public static function matchesDataProvider(): \Generator
159159
{
160160
yield "test match equals" => [
161-
'stringForTrueCase' => 'test theme 1',
162-
'stringForFalseCase' => 'test theme 22',
163-
'initFilter' => new StringFilter(equals: 'TEST theme 1')
161+
'trueCase' => 'test theme 1',
162+
'falseCase' => 'test theme 22',
163+
'filter' => new StringFilter(equals: 'TEST theme 1')
164164
];
165165

166166
yield "test match contains" => [
167-
'stringForTrueCase' => 'test abc theme',
168-
'stringForFalseCase' => 'test xyz theme',
169-
'initFilter' => new StringFilter(contains: 'aBC')
167+
'trueCase' => 'test abc theme',
168+
'falseCase' => 'test xyz theme',
169+
'filter' => new StringFilter(contains: 'aBC')
170170
];
171171

172172
yield "test match begins with" => [
173-
'stringForTrueCase' => 'this start',
174-
'stringForFalseCase' => 'this does not start with',
175-
'initFilter' => new StringFilter(beginsWith: 'this START')
173+
'trueCase' => 'this start',
174+
'falseCase' => 'this does not start with',
175+
'filter' => new StringFilter(beginsWith: 'this START')
176176
];
177177

178178
yield "test match begins with and contains" => [
179-
'stringForTrueCase' => 'this start with abc',
180-
'stringForFalseCase' => 'this does not start with abc',
181-
'initFilter' => new StringFilter(contains: 'abc', beginsWith: 'THIS start')
179+
'trueCase' => 'this start with abc',
180+
'falseCase' => 'this does not start with abc',
181+
'filter' => new StringFilter(contains: 'abc', beginsWith: 'THIS start')
182182
];
183183

184184
yield "test match equals and contains" => [
185-
'stringForTrueCase' => 'this is abc',
186-
'stringForFalseCase' => 'this is not abc',
187-
'initFilter' => new StringFilter(equals: 'This Is Abc', contains: 'ABC')
185+
'trueCase' => 'this is abc',
186+
'falseCase' => 'this is not abc',
187+
'filter' => new StringFilter(equals: 'This Is Abc', contains: 'ABC')
188188
];
189189

190190
yield "test is and is not string" => [
191-
'stringForTrueCase' => '23',
192-
'stringForFalseCase' => 23,
193-
'initFilter' => new StringFilter(equals: '23')
191+
'trueCase' => '23',
192+
'falseCase' => 23,
193+
'filter' => new StringFilter(equals: '23')
194194
];
195195
}
196196
}

0 commit comments

Comments
 (0)