1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Query;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Expr\Comparison as CriteriaComparison;
use Doctrine\Common\Collections\Expr\Value;
use Doctrine\Common\Collections\ExpressionBuilder as CriteriaBuilder;
use Doctrine\ORM\Query\Expr as QueryBuilder;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\QueryExpressionVisitor;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Test for QueryExpressionVisitor
*/
class QueryExpressionVisitorTest extends TestCase
{
private QueryExpressionVisitor $visitor;
protected function setUp(): void
{
$this->visitor = new QueryExpressionVisitor(['o', 'p']);
}
#[DataProvider('comparisonData')]
public function testWalkComparison(CriteriaComparison $criteriaExpr, QueryBuilder\Comparison|QueryBuilder\Func|string $queryExpr, Parameter|null $parameter = null): void
{
self::assertEquals($queryExpr, $this->visitor->walkComparison($criteriaExpr));
if ($parameter) {
self::assertEquals(new ArrayCollection([$parameter]), $this->visitor->getParameters());
}
}
/**
* @phpstan-return list<array{
* 0: CriteriaComparison,
* 1: QueryBuilder\Comparison|QueryBuilder\Func|string,
* 2?: Parameter,
* }>
*/
public static function comparisonData(): array
{
$cb = new CriteriaBuilder();
$qb = new QueryBuilder();
return [
[$cb->eq('field', 'value'), $qb->eq('o.field', ':field'), new Parameter('field', 'value')],
[$cb->neq('field', 'value'), $qb->neq('o.field', ':field'), new Parameter('field', 'value')],
[$cb->eq('field', null), $qb->isNull('o.field')],
[$cb->neq('field', null), $qb->isNotNull('o.field')],
[$cb->isNull('field'), $qb->isNull('o.field')],
[$cb->gt('field', 'value'), $qb->gt('o.field', ':field'), new Parameter('field', 'value')],
[$cb->gte('field', 'value'), $qb->gte('o.field', ':field'), new Parameter('field', 'value')],
[$cb->lt('field', 'value'), $qb->lt('o.field', ':field'), new Parameter('field', 'value')],
[$cb->lte('field', 'value'), $qb->lte('o.field', ':field'), new Parameter('field', 'value')],
[$cb->in('field', ['value']), $qb->in('o.field', ':field'), new Parameter('field', ['value'])],
[$cb->notIn('field', ['value']), $qb->notIn('o.field', ':field'), new Parameter('field', ['value'])],
[$cb->contains('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value%')],
[$cb->memberOf(':field', 'o.field'), $qb->isMemberOf(':field', 'o.field')],
[$cb->startsWith('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', 'value%')],
[$cb->endsWith('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value')],
// Test parameter conversion
[$cb->eq('object.field', 'value'), $qb->eq('o.object.field', ':object_field'), new Parameter('object_field', 'value')],
// Test alternative rootAlias
[$cb->eq('p.field', 'value'), $qb->eq('p.field', ':p_field'), new Parameter('p_field', 'value')],
[$cb->eq('p.object.field', 'value'), $qb->eq('p.object.field', ':p_object_field'), new Parameter('p_object_field', 'value')],
];
}
public function testWalkAndCompositeExpression(): void
{
$cb = new CriteriaBuilder();
$expr = $this->visitor->walkCompositeExpression(
$cb->andX(
$cb->eq('foo', 1),
$cb->eq('bar', 1),
),
);
self::assertInstanceOf(QueryBuilder\Andx::class, $expr);
self::assertCount(2, $expr->getParts());
}
public function testWalkOrCompositeExpression(): void
{
$cb = new CriteriaBuilder();
$expr = $this->visitor->walkCompositeExpression(
$cb->orX(
$cb->eq('foo', 1),
$cb->eq('bar', 1),
),
);
self::assertInstanceOf(QueryBuilder\Orx::class, $expr);
self::assertCount(2, $expr->getParts());
}
public function testWalkNotCompositeExpression(): void
{
$qb = new QueryBuilder();
$cb = new CriteriaBuilder();
$expr = $this->visitor->walkCompositeExpression(
$cb->not(
$cb->eq('foo', 1),
),
);
self::assertInstanceOf(QueryBuilder\Func::class, $expr);
self::assertEquals('NOT', $expr->getName());
self::assertCount(1, $expr->getArguments());
self::assertEquals($qb->eq('o.foo', ':foo'), $expr->getArguments()[0]);
self::assertEquals(new ArrayCollection([new Parameter('foo', 1)]), $this->visitor->getParameters());
}
public function testWalkValue(): void
{
self::assertEquals('value', $this->visitor->walkValue(new Value('value')));
}
public function testClearParameters(): void
{
$this->visitor->getParameters()->add(new Parameter('field', 'value'));
$this->visitor->clearParameters();
self::assertCount(0, $this->visitor->getParameters());
}
}
|