From 83f5f59cc12cc533c123dfd9033cbaed70c225bf Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Thu, 27 Feb 2025 12:48:50 +0500 Subject: [PATCH] =?UTF-8?q?feat|tests:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20Dat?= =?UTF-8?q?eRange=20=D0=B8=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Filter/DateRange.php | 21 ++++ tests/FilterByDateRangeTest.php | 186 ++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/Filter/DateRange.php create mode 100644 tests/FilterByDateRangeTest.php diff --git a/src/Filter/DateRange.php b/src/Filter/DateRange.php new file mode 100644 index 0000000..5d7b488 --- /dev/null +++ b/src/Filter/DateRange.php @@ -0,0 +1,21 @@ +where( + $this->getColumn() . ' >= \'' . $this->getHttpValue()['from'] . ' 00:00:00\' AND ' . + $this->getColumn() . ' <= \'' . $this->getHttpValue()['to'] . ' 23:59:59\'', + ); + + return $queryBuilder; + } +} diff --git a/tests/FilterByDateRangeTest.php b/tests/FilterByDateRangeTest.php new file mode 100644 index 0000000..48f45bd --- /dev/null +++ b/tests/FilterByDateRangeTest.php @@ -0,0 +1,186 @@ +em->getRepository(Post::class); + + $firstDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); + $secondDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-05'); + + $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' => $firstDate->add(new DateInterval('P1D'))->format('Y-m-d'), + ], + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(1, $result); + } + + public function testSuccessFilterDateRangeWithSeveralResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $firstDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); + $secondDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-05'); + + $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' => $firstDate->add(new DateInterval('P10D'))->format('Y-m-d'), + ], + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(2, $result); + } + + public function testSuccessFilterDateRangeWithBoundaryResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $firstDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); + $secondDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-05'); + + $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); + $this->assertCount(2, $result); + } + + public function testSuccessFilterDateRangeWithoutResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $firstDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); + $secondDate = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-05'); + + $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('P1Y'))->format('Y-m-d'), + ], + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertEmpty($result); + } +} -- GitLab