diff --git a/tests/FilterByRangeTest.php b/tests/FilterByRangeTest.php index e89d0f1ca767e2ba91c8f4387faa1a682bd4c5b2..16297743719d9002831427c1842ed648f0d3d734 100644 --- a/tests/FilterByRangeTest.php +++ b/tests/FilterByRangeTest.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace IQDEV\Tests\Packages\DoctrineHttpFilter; use DateTimeImmutable; -use IQDEV\Packages\DoctrineHttpFilter\Filter\DateRange; +use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException; use IQDEV\Packages\DoctrineHttpFilter\Filter\Range; use IQDEV\Packages\DoctrineHttpFilter\HttpFilter; use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post; @@ -176,11 +176,12 @@ class FilterByRangeTest extends TestCase $this->em->flush(); $result = $postRepository->createQueryByFilter([ - 'createdAt' => DateRange::class, + 'countOfViews' => Range::class, ], new Request([ HttpFilter::REQUEST_FILTER_KEY => [ 'countOfViews' => [ 'min' => 100, + 'max' => 200, ], ], ])) @@ -189,4 +190,165 @@ class FilterByRangeTest extends TestCase $this->assertEmpty($result); } + + public function testFilterRangeWithoutParameterValue(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $this->expectException(FilterParameterValueIsNullException::class); + + $postRepository->createQueryByFilter([ + 'createdAt' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [], + ])) + ->getQuery() + ->getResult(); + } + + public function testFilterRangeWithNotNumberParameterValue(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $this->expectException(FilterParameterValueIsNullException::class); + + $postRepository->createQueryByFilter([ + 'countOfViews' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'countOfViews' => 'not number', + ], + ])) + ->getQuery() + ->getResult(); + } + + public function testFilterRangeWithNotNumberParameterValues(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $this->expectException(FilterParameterValueIsNullException::class); + + $postRepository->createQueryByFilter([ + 'countOfViews' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'countOfViews' => [ + 'min' => 'not number', + 'max' => 'not number', + ], + ], + ])) + ->getQuery() + ->getResult(); + } + + public function testFilterRangeWithDifferentKey(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $this->expectException(FilterParameterValueIsNullException::class); + + $postRepository->createQueryByFilter([ + 'countOfViews' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'notCountOfViews' => 'Not countOfViews value' + ], + ])) + ->getQuery() + ->getResult(); + } + + public function testFilterRangeWithOnlyMinParameterValue(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $firstCountOfViews = 25; + $secondCountOfViews = 35; + + $post = new Post( + $this->faker->name(), + $this->faker->text(), + $this->faker->boolean(), + DateTimeImmutable::createFromInterface($this->faker->dateTime()), + countOfViews: $firstCountOfViews, + ); + + $post2 = new Post( + $this->faker->name(), + $this->faker->text(), + $this->faker->boolean(), + DateTimeImmutable::createFromInterface($this->faker->dateTime()), + countOfViews: $secondCountOfViews, + ); + + $this->em->persist($post); + $this->em->persist($post2); + $this->em->flush(); + + $result = $postRepository->createQueryByFilter([ + 'countOfViews' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'countOfViews' => [ + 'min' => 20, + ], + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(2, $result); + } + + public function testFilterRangeWithOnlyMaxParameterValue(): void + { + /** @var PostRepository $postRepository */ + $postRepository = $this->em->getRepository(Post::class); + + $firstCountOfViews = 25; + $secondCountOfViews = 35; + + $post = new Post( + $this->faker->name(), + $this->faker->text(), + $this->faker->boolean(), + DateTimeImmutable::createFromInterface($this->faker->dateTime()), + countOfViews: $firstCountOfViews, + ); + + $post2 = new Post( + $this->faker->name(), + $this->faker->text(), + $this->faker->boolean(), + DateTimeImmutable::createFromInterface($this->faker->dateTime()), + countOfViews: $secondCountOfViews, + ); + + $this->em->persist($post); + $this->em->persist($post2); + $this->em->flush(); + + $result = $postRepository->createQueryByFilter([ + 'countOfViews' => Range::class, + ], new Request([ + HttpFilter::REQUEST_FILTER_KEY => [ + 'countOfViews' => [ + 'max' => 40, + ], + ], + ])) + ->getQuery() + ->getResult(); + + $this->assertNotEmpty($result); + $this->assertCount(2, $result); + } }