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'), 'to' => $secondDate->add(new DateInterval('P1Y'))->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertEmpty($result); } public function testFilterDateRangeWithoutParameterValue(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $countPosts = $postRepository->count(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [], ])) ->getQuery() ->getResult(); $this->assertCount($countPosts, $result); } public function testFilterDateRangeWithNotDateParameterValue(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $countPosts = $postRepository->count(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => 'not date', ], ])) ->getQuery() ->getResult(); $this->assertCount($countPosts, $result); } public function testFilterDateRangeWithNotDateParameterValues(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $countPosts = $postRepository->count(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'createdAt' => [ 'from' => 'not date', 'to' => 'not date', ], ], ])) ->getQuery() ->getResult(); $this->assertCount($countPosts, $result); } public function testFilterDateRangeWithDifferentKey(): void { /** @var PostRepository $postRepository */ $postRepository = $this->em->getRepository(Post::class); $countPosts = $postRepository->count(); $result = $postRepository->createQueryByFilter([ 'createdAt' => DateRange::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'notCreatedAt' => 'Not createdAt value', ], ])) ->getQuery() ->getResult(); $this->assertCount($countPosts, $result); } public function testFilterDateRangeWithOnlyFromParameterValue(): 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' => $secondDate->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertNotEmpty($result); $this->assertGreaterThanOrEqual(1, $result); } public function testFilterDateRangeWithOnlyToParameterValue(): 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' => [ 'to' => $firstDate->format('Y-m-d'), ], ], ])) ->getQuery() ->getResult(); $this->assertNotEmpty($result); $this->assertGreaterThanOrEqual(1, $result); } }