Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateInterval;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Date;
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 FilterByDateTest extends TestCase
{
public function testSuccessFilterDateWithOneResult(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 12:00:00');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$date,
);
$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([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => $date->format('Y-m-d'),
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertEquals($date->format('Y-m-d'), $post->createdAt->format('Y-m-d'));
}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}
public function testSuccessFilterDateWithoutTime(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$date,
);
$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([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => $date->format('Y-m-d'),
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertEquals($date->format('Y-m-d'), $post->createdAt->format('Y-m-d'));
}
public function testSuccessFilterDateWithSeveralResults(): void
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 12:00:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 19:30:00');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$firstDate,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => $firstDate->format('Y-m-d'),
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertEquals($firstDate->format('Y-m-d'), $post->createdAt->format('Y-m-d'));
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}
public function testSuccessFilterDateRangeWithoutResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 14:00:00');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$firstDate,
);
$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([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => $firstDate->add(new DateInterval('P100Y'))->format('Y-m-d'),
],
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterDateWithNotDateParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => 'not date',
],
]))
->getQuery()
->getResult();
}
public function testFilterDateWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
],
]))
->getQuery()
->getResult();