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-05 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(), $secondDate, ); $this->em->persist($post); $this->em->persist($post2); $this->em->flush(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => [ 'from' => $firstDate->format('Y-m-d'), 'to' => $firstDate->add(new DateInterval('P1D'))->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertNotEmpty($result); $this->assertCount(1, $result); } 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-05 11: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(), $secondDate, ); $this->em->persist($post); $this->em->persist($post2); $this->em->flush(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => [ 'from' => $firstDate->format('Y-m-d'), 'to' => $firstDate->add(new DateInterval('P10D'))->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertNotEmpty($result); $this->assertCount(2, $result); } public function testSuccessFilterDateRangeWithBoundaryResults(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00'); $secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12: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(), $secondDate, ); $this->em->persist($post); $this->em->persist($post2); $this->em->flush(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => [ 'from' => $firstDate->format('Y-m-d'), 'to' => $secondDate->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 15:00:00'); $secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 00: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(), $secondDate, ); $this->em->persist($post); $this->em->persist($post2); $this->em->flush(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => [ 'from' => $firstDate->add(new DateInterval('P1Y'))->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertEmpty($result); } }