<?php
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\ExpressionBuilder;
class ClosureExpressionVisitorTest extends \PHPUnit_Framework_TestCase {
private $visitor;
private $builder;
public function setUp() {
$this->visitor = new ClosureExpressionVisitor();
$this->builder = new ExpressionBuilder();
}
public function testWalkEqualsComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->eq("foo", 1));
$this
->assertTrue($closure(new TestObject(1)));
$this
->assertFalse($closure(new TestObject(2)));
}
public function testWalkNotEqualsComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->neq("foo", 1));
$this
->assertFalse($closure(new TestObject(1)));
$this
->assertTrue($closure(new TestObject(2)));
}
public function testWalkLessThanComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->lt("foo", 1));
$this
->assertFalse($closure(new TestObject(1)));
$this
->assertTrue($closure(new TestObject(0)));
}
public function testWalkLessThanEqualsComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->lte("foo", 1));
$this
->assertFalse($closure(new TestObject(2)));
$this
->assertTrue($closure(new TestObject(1)));
$this
->assertTrue($closure(new TestObject(0)));
}
public function testWalkGreaterThanEqualsComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->gte("foo", 1));
$this
->assertTrue($closure(new TestObject(2)));
$this
->assertTrue($closure(new TestObject(1)));
$this
->assertFalse($closure(new TestObject(0)));
}
public function testWalkGreaterThanComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->gt("foo", 1));
$this
->assertTrue($closure(new TestObject(2)));
$this
->assertFalse($closure(new TestObject(1)));
$this
->assertFalse($closure(new TestObject(0)));
}
public function testWalkInComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->in("foo", array(
1,
2,
3,
)));
$this
->assertTrue($closure(new TestObject(2)));
$this
->assertTrue($closure(new TestObject(1)));
$this
->assertFalse($closure(new TestObject(0)));
}
public function testWalkNotInComparison() {
$closure = $this->visitor
->walkComparison($this->builder
->notIn("foo", array(
1,
2,
3,
)));
$this
->assertFalse($closure(new TestObject(1)));
$this
->assertFalse($closure(new TestObject(2)));
$this
->assertTrue($closure(new TestObject(0)));
$this
->assertTrue($closure(new TestObject(4)));
}
public function testWalkAndCompositeExpression() {
$closure = $this->visitor
->walkCompositeExpression($this->builder
->andX($this->builder
->eq("foo", 1), $this->builder
->eq("bar", 1)));
$this
->assertTrue($closure(new TestObject(1, 1)));
$this
->assertFalse($closure(new TestObject(1, 0)));
$this
->assertFalse($closure(new TestObject(0, 1)));
$this
->assertFalse($closure(new TestObject(0, 0)));
}
public function testWalkOrCompositeExpression() {
$closure = $this->visitor
->walkCompositeExpression($this->builder
->orX($this->builder
->eq("foo", 1), $this->builder
->eq("bar", 1)));
$this
->assertTrue($closure(new TestObject(1, 1)));
$this
->assertTrue($closure(new TestObject(1, 0)));
$this
->assertTrue($closure(new TestObject(0, 1)));
$this
->assertFalse($closure(new TestObject(0, 0)));
}
public function testSortByFieldAscending() {
$objects = array(
new TestObject("b"),
new TestObject("a"),
new TestObject("c"),
);
$sort = ClosureExpressionVisitor::sortByField("foo");
usort($objects, $sort);
$this
->assertEquals("a", $objects[0]
->getFoo());
$this
->assertEquals("b", $objects[1]
->getFoo());
$this
->assertEquals("c", $objects[2]
->getFoo());
}
public function testSortByFieldDescending() {
$objects = array(
new TestObject("b"),
new TestObject("a"),
new TestObject("c"),
);
$sort = ClosureExpressionVisitor::sortByField("foo", -1);
usort($objects, $sort);
$this
->assertEquals("c", $objects[0]
->getFoo());
$this
->assertEquals("b", $objects[1]
->getFoo());
$this
->assertEquals("a", $objects[2]
->getFoo());
}
public function testSortDelegate() {
$objects = array(
new TestObject("a", "c"),
new TestObject("a", "b"),
new TestObject("a", "a"),
);
$sort = ClosureExpressionVisitor::sortByField("bar", 1);
$sort = ClosureExpressionVisitor::sortByField("foo", 1, $sort);
usort($objects, $sort);
$this
->assertEquals("a", $objects[0]
->getBar());
$this
->assertEquals("b", $objects[1]
->getBar());
$this
->assertEquals("c", $objects[2]
->getBar());
}
}
class TestObject {
private $foo;
private $bar;
public function __construct($foo = null, $bar = null) {
$this->foo = $foo;
$this->bar = $bar;
}
public function getFoo() {
return $this->foo;
}
public function getBar() {
return $this->bar;
}
}