Commit 8eb2c1f1 authored by Grzegorz Kielak's avatar Grzegorz Kielak

Fix CompositeExpression add method optimization

parent 5c5966aa
...@@ -91,10 +91,16 @@ class CompositeExpression implements \Countable ...@@ -91,10 +91,16 @@ class CompositeExpression implements \Countable
*/ */
public function add($part) public function add($part)
{ {
if ( ! empty($part) || ($part instanceof self && $part->count() > 0)) { if (empty($part)) {
$this->parts[] = $part; return $this;
} }
if ($part instanceof self && 0 === count($part)) {
return $this;
}
$this->parts[] = $part;
return $this; return $this;
} }
......
...@@ -20,6 +20,29 @@ class CompositeExpressionTest extends \Doctrine\Tests\DbalTestCase ...@@ -20,6 +20,29 @@ class CompositeExpressionTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals(2, count($expr)); $this->assertEquals(2, count($expr));
} }
public function testAdd()
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, array('u.group_id = 1'));
$this->assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_AND, array()));
$this->assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, array('u.user_id = 1')));
$this->assertCount(2, $expr);
$expr->add(null);
$this->assertCount(2, $expr);
$expr->add('u.user_id = 1');
$this->assertCount(3, $expr);
}
/** /**
* @dataProvider provideDataForConvertToString * @dataProvider provideDataForConvertToString
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment