diff --git a/tests/FilterByLikeTest.php b/tests/FilterByLikeTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2b8eee68792fbdbad7af7b3f6fb0162f2491b2b6 --- /dev/null +++ b/tests/FilterByLikeTest.php @@ -0,0 +1,129 @@ +em->getRepository(Post::class); + + $title = 'Видеопредставление'; + $subtitle = 'Видео'; + + $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' => Like::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $subtitle, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(1, $result); + $this->assertStringContainsString($subtitle, current($result)->title); + } + + public function testSuccessFilterLikeWithSeveralResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $titleFirst = 'Видеопредставление'; + $titleSecond = 'Видеомагнитофон'; + $subtitle = 'Видео'; + + $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' => Like::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $subtitle, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(2, $result); + $this->assertStringContainsString($subtitle, current($result)->title); + $this->assertStringContainsString($subtitle, next($result)->title); + } + + public function testSuccessFilterLikeWithoutResults(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $title = 'Видеопредставление'; + $subtitle = 'видео'; + + $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' => Like::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'title' => $subtitle, + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertEmpty($result); + } +}