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

tests: Реализован тест для сортировки по смежным таблицам

parent 0f630d65
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
@@ -4,9 +4,11 @@ declare(strict_types=1);

namespace IQDEV\Tests\Packages\DoctrineHttpFilter;

use Doctrine\ORM\AbstractQuery;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Like;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Packages\DoctrineHttpFilter\HttpSort;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Comment;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Repository\PostRepository;
use Symfony\Component\HttpFoundation\Request;
@@ -165,4 +167,59 @@ class HttpSortTest extends TestCase
        $this->assertSame($date2, $result[0]->createdAt);
        $this->assertCount(2, $result);
    }

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

        $author1 = 'Александр';
        $author2 = 'Яков';

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

        $comment1 = new Comment(
            $author1,
            $this->faker->text(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
            $post
        );
        $comment2 = new Comment(
            $author1,
            $this->faker->text(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
            $post
        );
        $comment3 = new Comment(
            $author2,
            $this->faker->text(),
            \DateTimeImmutable::createFromInterface($this->faker->dateTime()),
            $post
        );

        $this->em->persist($post);
        $this->em->persist($comment1);
        $this->em->persist($comment2);
        $this->em->persist($comment3);
        $this->em->flush();

        $result = $postRepository->createQueryForSort(
            new Request([
                HttpSort::REQUEST_SORT_KEY => [
                    'c.author' => 'DESC',
                ],
            ])
        )
            ->leftJoin(Comment::class, 'c')
            ->select('p', 'c.author')
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_ARRAY);

        $this->assertSame($author2, $result[0]['author']);
    }
}