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 testSuccessFilterDateWithoutTime(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $date = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); $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 testSuccessFilterDateWithSeveralResults(): 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); } public function testFilterDateWithoutParameterValue(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $this->expectException(FilterParameterValueIsNullException::class); $postRepository->createQueryByFilter([ 'createdAt' => Date::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [], ])) ->getQuery() ->getResult(); } public function testFilterDateWithNotDateParameterValue(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $this->expectException(FilterParameterValueIsNullException::class); $postRepository->createQueryByFilter([ 'createdAt' => Date::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => 'not date', ], ])) ->getQuery() ->getResult(); } public function testFilterDateWithDifferentKey(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $this->expectException(FilterParameterValueIsNullException::class); $postRepository->createQueryByFilter([ 'createdAt' => Date::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'notCreatedAt' => 'Not createdAt value', ], ])) ->getQuery() ->getResult(); } }