Skip to content
Snippets Groups Projects
FilterByInTest.php 3.87 KiB
Newer Older
<?php

declare(strict_types=1);

namespace IQDEV\Tests\Packages\DoctrineHttpFilter;

use IQDEV\Packages\DoctrineHttpFilter\Filter\In;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Repository\PostRepository;
use Symfony\Component\HttpFoundation\Request;

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

        $title = 'Спорт';
        $inValues = ['Спорт', 'Наука', 'Кулинария'];

        $post = new Post(
            $title,
            $this->faker->text(),
            $this->faker->boolean(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
        );

        $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([
            'title' => In::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'title' => $inValues,
            ],
        ]))
            ->getQuery()
            ->getResult();

        $this->assertNotEmpty($result);
        $this->assertCount(1, $result);
        $this->assertStringContainsString($inValues[0], current($result)->title);
    }

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

        $titleFirst = 'Спорт';
        $titleSecond = 'Кулинария';
        $inValues = ['Спорт', 'Наука', 'Кулинария'];

        $post = new Post(
            $titleFirst,
            $this->faker->text(),
            $this->faker->boolean(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
        );

        $post2 = new Post(
            $titleSecond,
            $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([
            'title' => In::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'title' => $inValues,
            ],
        ]))
            ->getQuery()
            ->getResult();

        $this->assertNotEmpty($result);
        $this->assertCount(2, $result);
        $this->assertStringContainsString($inValues[0], current($result)->title);
        $this->assertStringContainsString($inValues[2], next($result)->title);
    }

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

        $title = 'Бизнес';
        $inValues = ['Спорт', 'Наука', 'Кулинария'];

        $post = new Post(
            $title,
            $this->faker->text(),
            $this->faker->boolean(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
        );

        $this->em->persist($post);
        $this->em->flush();

        $result = $postRepository->createQueryByFilter([
            'title' => In::class,
        ], new Request([
            HttpFilter::REQUEST_FILTER_KEY => [
                'title' => $inValues,
            ],
        ]))
            ->getQuery()
            ->getResult();

        $this->assertEmpty($result);
    }
}