Commit 051dbba7 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

tests: Добавлены фильтры для DateRange

parent b7b1400b
Loading
Loading
Loading
Loading
+159 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ namespace IQDEV\Tests\Packages\DoctrineHttpFilter;

use DateInterval;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\DateRange;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
@@ -175,6 +176,7 @@ class FilterByDateRangeTest extends TestCase
            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')
                ],
            ],
        ]))
@@ -183,4 +185,161 @@ class FilterByDateRangeTest extends TestCase

        $this->assertEmpty($result);
    }

    public function testFilterDateRangeWithoutParameterValue(): void
    {
        /** @var PostRepository $postRepository */
        $postRepository = $this->em->getRepository(Post::class);

        $this->expectException(FilterParameterValueIsNullException::class);

        $postRepository->createQueryByFilter([
            'createdAt' => DateRange::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [],
        ]))
            ->getQuery()
            ->getResult();
    }

    public function testFilterDateRangeWithNotDateParameterValue(): void
    {
        /** @var PostRepository $postRepository */
        $postRepository = $this->em->getRepository(Post::class);

        $this->expectException(FilterParameterValueIsNullException::class);

        $postRepository->createQueryByFilter([
            'createdAt' => DateRange::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'createdAt' => 'not date',
            ],
        ]))
            ->getQuery()
            ->getResult();
    }

    public function testFilterDateRangeWithNotDateParameterValues(): void
    {
        /** @var PostRepository $postRepository */
        $postRepository = $this->em->getRepository(Post::class);

        $result = $postRepository->createQueryByFilter([
            'createdAt' => DateRange::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'createdAt' => [
                    'from' => 'not date',
                    'to' => 'not date'
                ],
            ],
        ]))
            ->getQuery()
            ->getResult();

        $this->assertEmpty($result);
    }

    public function testFilterDateRangeWithDifferentKey(): void
    {
        /** @var PostRepository $postRepository */
        $postRepository = $this->em->getRepository(Post::class);

        $this->expectException(FilterParameterValueIsNullException::class);

        $postRepository->createQueryByFilter([
            'createdAt' => DateRange::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'notCreatedAt' => 'Not createdAt value'
            ],
        ]))
            ->getQuery()
            ->getResult();
    }

    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);
    }
}