Newer
Older
<?php
declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateInterval;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Filter\DateRange;
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 FilterByDateRangeTest extends TestCase
{
public function testSuccessFilterDateRangeWithOneResult(): void
{
/** @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-05 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(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$firstDateWithDay = $firstDate->add(new DateInterval('P1D'));
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $firstDate->format('Y-m-d'),
'to' => $firstDateWithDay->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertGreaterThanOrEqual($firstDate->setTime(0, 0), $post->createdAt);
$this->assertLessThanOrEqual($firstDateWithDay->setTime(23, 59, 59), $post->createdAt);
}
}
public function testSuccessFilterDateRangeWithSeveralResults(): void
{
/** @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-05 11: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(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$firstDateWithDays = $firstDate->add(new DateInterval('P10D'));
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $firstDate->format('Y-m-d'),
'to' => $firstDateWithDays->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertGreaterThanOrEqual($firstDate->setTime(0, 0), $post->createdAt);
$this->assertLessThanOrEqual($firstDateWithDays->setTime(23, 59, 59), $post->createdAt);
}
}
public function testSuccessFilterDateRangeWithBoundaryResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12:00:00');
118
119
120
121
122
123
124
125
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
$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' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $firstDate->format('Y-m-d'),
'to' => $secondDate->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
foreach ($result as $post) {
$this->assertGreaterThanOrEqual($firstDate->setTime(0, 0), $post->createdAt);
$this->assertLessThanOrEqual($secondDate->setTime(23, 59, 59), $post->createdAt);
}
}
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 15:00:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 00: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(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $firstDate->add(new DateInterval('P100Y'))->format('Y-m-d'),
'to' => $secondDate->add(new DateInterval('P100Y'))->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateRangeWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithNotDateParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => 'not date',
],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithNotDateParameterValues(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => 'not date',
],
],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
],
]))
->getQuery()
->getResult();
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
361
362
}
public function testFilterDateRangeWithOnlyFromParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12: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(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $secondDate->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertGreaterThanOrEqual(1, $result);
}
public function testFilterDateRangeWithOnlyToParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12: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(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'to' => $firstDate->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertGreaterThanOrEqual(1, $result);
}