From d038573ed4aae4875a8d01410ac8053f093fd9f4 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 3 Mar 2025 10:29:45 +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?e=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 --- composer.json | 3 +- src/Filter/Date.php | 20 ++++++ tests/FilterByDateTest.php | 134 +++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 2 + 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/Filter/Date.php create mode 100644 tests/FilterByDateTest.php diff --git a/composer.json b/composer.json index 9a44ded..5c38462 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "require": { "php": "^8.3", "symfony/http-foundation": "^7.2.3", - "doctrine/orm": "^3.3.2" + "doctrine/orm": "^3.3.2", + "beberlei/doctrineextensions": "^1.5" }, "require-dev": { "phpunit/phpunit": "^12.0", diff --git a/src/Filter/Date.php b/src/Filter/Date.php new file mode 100644 index 0000000..de40e23 --- /dev/null +++ b/src/Filter/Date.php @@ -0,0 +1,20 @@ +where( + 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') = \'' . $this->getHttpValue() . '\'', + ); + + return $queryBuilder; + } +} diff --git a/tests/FilterByDateTest.php b/tests/FilterByDateTest.php new file mode 100644 index 0000000..9e01aad --- /dev/null +++ b/tests/FilterByDateTest.php @@ -0,0 +1,134 @@ +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); + $this->assertCount(1, $result); + $this->assertEquals($date, current($result)->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-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); + $this->assertCount(2, $result); + } + + 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('P1Y'))->format('Y-m-d'), + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertEmpty($result); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 0e63318..bdb1212 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -12,6 +12,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMSetup; use Doctrine\ORM\Tools\SchemaTool; +use DoctrineExtensions\Query\Sqlite\StrfTime; use Faker\Factory; use Faker\Generator; use IQDEV\Packages\DoctrineHttpFilter\HttpFilterEntityRepository; @@ -59,6 +60,7 @@ abstract class TestCase extends BaseTestCase [__DIR__ . '/Entity'], true, ); + $config->addCustomStringFunction('strftime', StrfTime::class); $connection = DriverManager::getConnection([ 'driver' => 'pdo_sqlite', -- GitLab