2012-08-19 21:58:04 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Query;
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
use Doctrine\Common\Collections\Expr\Comparison as CriteriaComparison;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Common\Collections\Expr\Value;
|
2012-08-19 21:58:04 +04:00
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder as CriteriaBuilder;
|
|
|
|
use Doctrine\ORM\Query\Expr as QueryBuilder;
|
|
|
|
use Doctrine\ORM\Query\Parameter;
|
|
|
|
use Doctrine\ORM\Query\QueryExpressionVisitor;
|
2017-03-31 17:20:10 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2012-08-19 21:58:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for QueryExpressionVisitor
|
|
|
|
*
|
|
|
|
* @author Kirill chEbba Chebunin <iam@chebba.org>
|
|
|
|
*/
|
2017-03-31 17:20:10 +01:00
|
|
|
class QueryExpressionVisitorTest extends TestCase
|
2012-08-19 21:58:04 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var QueryExpressionVisitor
|
|
|
|
*/
|
|
|
|
private $visitor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->visitor = new QueryExpressionVisitor(['o','p']);
|
2012-08-19 21:58:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CriteriaComparison $criteriaExpr
|
2016-12-08 18:01:04 +01:00
|
|
|
* @param QueryBuilder\Comparison|string $queryExpr
|
2012-08-19 21:58:04 +04:00
|
|
|
* @param Parameter $parameter
|
|
|
|
*
|
|
|
|
* @dataProvider comparisonData
|
|
|
|
*/
|
|
|
|
public function testWalkComparison(CriteriaComparison $criteriaExpr, $queryExpr, Parameter $parameter = null)
|
|
|
|
{
|
|
|
|
$this->assertEquals($queryExpr, $this->visitor->walkComparison($criteriaExpr));
|
|
|
|
if ($parameter) {
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(new ArrayCollection([$parameter]), $this->visitor->getParameters());
|
2012-08-19 21:58:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function comparisonData()
|
|
|
|
{
|
2012-08-29 15:48:04 +04:00
|
|
|
$cb = new CriteriaBuilder();
|
|
|
|
$qb = new QueryBuilder();
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
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')],
|
2012-08-19 21:58:04 +04:00
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
[$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')],
|
2012-08-19 21:58:04 +04:00
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
[$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'])],
|
2012-08-19 21:58:04 +04:00
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
[$cb->contains('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value%')],
|
2013-09-16 14:56:04 +02:00
|
|
|
|
2017-04-13 16:25:44 +03:00
|
|
|
[$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')],
|
|
|
|
|
2012-08-19 21:58:04 +04:00
|
|
|
// Test parameter conversion
|
2016-12-07 23:33:41 +01:00
|
|
|
[$cb->eq('object.field', 'value'), $qb->eq('o.object.field', ':object_field'), new Parameter('object_field', 'value')],
|
2014-12-06 14:40:03 +00:00
|
|
|
|
|
|
|
// Test alternative rootAlias
|
2016-12-07 23:33:41 +01:00
|
|
|
[$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')],
|
|
|
|
];
|
2012-08-19 21:58:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWalkAndCompositeExpression()
|
|
|
|
{
|
2012-08-29 15:48:04 +04:00
|
|
|
$cb = new CriteriaBuilder();
|
2012-08-19 21:58:04 +04:00
|
|
|
$expr = $this->visitor->walkCompositeExpression(
|
2012-08-29 15:48:04 +04:00
|
|
|
$cb->andX(
|
|
|
|
$cb->eq("foo", 1),
|
|
|
|
$cb->eq("bar", 1)
|
2012-08-19 21:58:04 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(QueryBuilder\Andx::class, $expr);
|
2012-08-19 21:58:04 +04:00
|
|
|
$this->assertCount(2, $expr->getParts());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWalkOrCompositeExpression()
|
|
|
|
{
|
2012-08-29 15:48:04 +04:00
|
|
|
$cb = new CriteriaBuilder();
|
2012-08-19 21:58:04 +04:00
|
|
|
$expr = $this->visitor->walkCompositeExpression(
|
2012-08-29 15:48:04 +04:00
|
|
|
$cb->orX(
|
|
|
|
$cb->eq("foo", 1),
|
|
|
|
$cb->eq("bar", 1)
|
2012-08-19 21:58:04 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(QueryBuilder\Orx::class, $expr);
|
2012-08-19 21:58:04 +04:00
|
|
|
$this->assertCount(2, $expr->getParts());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWalkValue()
|
|
|
|
{
|
|
|
|
$this->assertEquals('value', $this->visitor->walkValue(new Value('value')));
|
|
|
|
}
|
2012-08-20 19:54:11 +04:00
|
|
|
|
|
|
|
public function testClearParameters()
|
|
|
|
{
|
|
|
|
$this->visitor->getParameters()->add(new Parameter('field', 'value'));
|
|
|
|
|
|
|
|
$this->visitor->clearParameters();
|
|
|
|
|
|
|
|
$this->assertCount(0, $this->visitor->getParameters());
|
|
|
|
}
|
2012-08-19 21:58:04 +04:00
|
|
|
}
|