diff --git a/src/Filter/In.php b/src/Filter/In.php new file mode 100644 index 0000000000000000000000000000000000000000..5618b827f65122619db70495f4e330a5da693706 --- /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 0000000000000000000000000000000000000000..b02364962cc5d83ed2e392b60ad21d96ce7988f1 --- /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); + } +}