Newer
Older
<?php
declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Range;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Repository\PostRepository;
use Symfony\Component\HttpFoundation\Request;
class FilterByRangeTest extends TestCase
{
public function testSuccessFilterRangeWithOneResult(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$countOfViews = 20;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 0,
'max' => 40,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(1, $result);
}
public function testSuccessFilterRangeWithSeveralResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 20;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 10,
'max' => 40,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
public function testSuccessFilterRangeWithBoundaryResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 10;
$secondCountOfViews = 15;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => $firstCountOfViews,
'max' => $secondCountOfViews,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
public function testSuccessFilterRangeWithoutResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 25;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 100,
],
],
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterRangeWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithNotNumberParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => 'not number',
],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithNotNumberParameterValues(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 'not number',
'max' => 'not number',
],
],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notCountOfViews' => 'Not countOfViews value',
],
]))
->getQuery()
->getResult();
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
}
public function testFilterRangeWithOnlyMinParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 25;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $firstCountOfViews,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $secondCountOfViews,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 20,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
public function testFilterRangeWithOnlyMaxParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 25;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $firstCountOfViews,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $secondCountOfViews,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'max' => 40,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}