From 1ce62ba2cdd6d515a49ddd6cb423102acea3cc74 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 3 Mar 2025 11:14:25 +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=20In?= =?UTF-8?q?=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/In.php | 23 +++++++ tests/FilterByInTest.php | 129 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/Filter/In.php create mode 100644 tests/FilterByInTest.php diff --git a/src/Filter/In.php b/src/Filter/In.php new file mode 100644 index 0000000..5618b82 --- /dev/null +++ b/src/Filter/In.php @@ -0,0 +1,23 @@ + '\'' . $value . '\'', $this->getHttpValue()); + $stringValues = implode(',', $values); + + $queryBuilder->where( + $this->getColumn() . ' IN (' . $stringValues . ')', + ); + + return $queryBuilder; + } +} diff --git a/tests/FilterByInTest.php b/tests/FilterByInTest.php new file mode 100644 index 0000000..b023649 --- /dev/null +++ b/tests/FilterByInTest.php @@ -0,0 +1,129 @@ +em->getRepository(Post::class); + + $title = 'Спорт'; + $inValues = ['Спорт', 'Наука', 'Кулинария']; + + $post = new Post( + $title, + $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([ + 'title' => In::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $inValues, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(1, $result); + $this->assertStringContainsString($inValues[0], current($result)->title); + } + + public function testSuccessFilterInWithSeveralResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $titleFirst = 'Спорт'; + $titleSecond = 'Кулинария'; + $inValues = ['Спорт', 'Наука', 'Кулинария']; + + $post = new Post( + $titleFirst, + $this->faker->text(), + $this->faker->boolean(), + \DateTimeImmutable::createFromInterface($this->faker->dateTime()), + ); + + $post2 = new Post( + $titleSecond, + $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([ + 'title' => In::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $inValues, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(2, $result); + $this->assertStringContainsString($inValues[0], current($result)->title); + $this->assertStringContainsString($inValues[2], next($result)->title); + } + + public function testSuccessFilterInWithoutResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $title = 'Бизнес'; + $inValues = ['Спорт', 'Наука', 'Кулинария']; + + $post = new Post( + $title, + $this->faker->text(), + $this->faker->boolean(), + \DateTimeImmutable::createFromInterface($this->faker->dateTime()), + ); + + $this->em->persist($post); + $this->em->flush(); + + $result = $postRepository->createQueryByFilter([ + 'title' => In::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $inValues, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertEmpty($result); + } +} -- GitLab