Skip to content

Commit 33836c3

Browse files
committed
test: cover ServerTypeFilter nested-property branch
Codecov flagged 0% patch coverage on the new dotted-path handling. Three KernelTest cases cover: * flat property → no JOIN, compares on root alias * nested property → auto-LEFT-JOINs the relation under the plain alias * nested property when the alias already exists → no duplicate JOIN
1 parent c303c0d commit 33836c3

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Form\Type\Admin;
6+
7+
use App\Entity\Installation;
8+
use App\Form\Type\Admin\ServerTypeFilter;
9+
use App\Form\Type\Admin\ServerTypeFilterType;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
use Doctrine\ORM\QueryBuilder;
12+
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
13+
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
14+
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto;
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
17+
class ServerTypeFilterTest extends KernelTestCase
18+
{
19+
public function testApplyOnFlatPropertyComparesAgainstRootAlias(): void
20+
{
21+
$qb = $this->makeInstallationQueryBuilder();
22+
23+
$this->apply($qb, 'type', 'prod');
24+
25+
$dql = $qb->getDQL();
26+
self::assertStringContainsString('entity.type = :type_0', $dql);
27+
self::assertStringNotContainsString('JOIN', $dql, 'A flat property must not trigger any JOIN');
28+
self::assertSame('prod', $qb->getParameter('type_0')?->getValue());
29+
}
30+
31+
public function testApplyOnNestedPropertyJoinsTheRelation(): void
32+
{
33+
$qb = $this->makeInstallationQueryBuilder();
34+
35+
$this->apply($qb, 'server.type', 'stg');
36+
37+
$dql = $qb->getDQL();
38+
self::assertStringContainsString('LEFT JOIN entity.server server', $dql, 'Nested filter must auto-join the relation');
39+
self::assertStringContainsString('server.type = :server_type_0', $dql);
40+
self::assertSame('stg', $qb->getParameter('server_type_0')?->getValue());
41+
}
42+
43+
public function testApplyReusesExistingJoinAliasInsteadOfDuplicating(): void
44+
{
45+
$qb = $this->makeInstallationQueryBuilder()
46+
->leftJoin('entity.server', 'server');
47+
48+
$this->apply($qb, 'server.type', 'devops');
49+
50+
$dql = $qb->getDQL();
51+
self::assertSame(1, substr_count($dql, 'LEFT JOIN entity.server'), 'No duplicate join when alias already exists');
52+
self::assertStringContainsString('server.type = :server_type_0', $dql);
53+
}
54+
55+
private function apply(QueryBuilder $qb, string $property, string $value): void
56+
{
57+
$filterDto = new FilterDto();
58+
$filterDto->setProperty($property);
59+
$filterDto->setFormType(ServerTypeFilterType::class);
60+
61+
ServerTypeFilter::new($property)->apply(
62+
$qb,
63+
FilterDataDto::new(0, $filterDto, 'entity', [
64+
'comparison' => '=',
65+
'value' => $value,
66+
]),
67+
null,
68+
new EntityDto(Installation::class, $this->getEntityManager()->getClassMetadata(Installation::class)),
69+
);
70+
}
71+
72+
private function makeInstallationQueryBuilder(): QueryBuilder
73+
{
74+
return $this->getEntityManager()->getRepository(Installation::class)->createQueryBuilder('entity');
75+
}
76+
77+
private function getEntityManager(): EntityManagerInterface
78+
{
79+
return static::getContainer()->get('doctrine')->getManager();
80+
}
81+
}

0 commit comments

Comments
 (0)