3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Tests\Common\Collections;
22 use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
23 use Doctrine\Common\Collections\ExpressionBuilder;
28 class ClosureExpressionVisitorTest extends \PHPUnit_Framework_TestCase
33 public function setUp()
35 $this->visitor = new ClosureExpressionVisitor();
36 $this->builder = new ExpressionBuilder();
39 public function testWalkEqualsComparison()
41 $closure = $this->visitor->walkComparison($this->builder->eq("foo", 1));
43 $this->assertTrue($closure(new TestObject(1)));
44 $this->assertFalse($closure(new TestObject(2)));
47 public function testWalkNotEqualsComparison()
49 $closure = $this->visitor->walkComparison($this->builder->neq("foo", 1));
51 $this->assertFalse($closure(new TestObject(1)));
52 $this->assertTrue($closure(new TestObject(2)));
55 public function testWalkLessThanComparison()
57 $closure = $this->visitor->walkComparison($this->builder->lt("foo", 1));
59 $this->assertFalse($closure(new TestObject(1)));
60 $this->assertTrue($closure(new TestObject(0)));
63 public function testWalkLessThanEqualsComparison()
65 $closure = $this->visitor->walkComparison($this->builder->lte("foo", 1));
67 $this->assertFalse($closure(new TestObject(2)));
68 $this->assertTrue($closure(new TestObject(1)));
69 $this->assertTrue($closure(new TestObject(0)));
72 public function testWalkGreaterThanEqualsComparison()
74 $closure = $this->visitor->walkComparison($this->builder->gte("foo", 1));
76 $this->assertTrue($closure(new TestObject(2)));
77 $this->assertTrue($closure(new TestObject(1)));
78 $this->assertFalse($closure(new TestObject(0)));
81 public function testWalkGreaterThanComparison()
83 $closure = $this->visitor->walkComparison($this->builder->gt("foo", 1));
85 $this->assertTrue($closure(new TestObject(2)));
86 $this->assertFalse($closure(new TestObject(1)));
87 $this->assertFalse($closure(new TestObject(0)));
90 public function testWalkInComparison()
92 $closure = $this->visitor->walkComparison($this->builder->in("foo", array(1, 2, 3)));
94 $this->assertTrue($closure(new TestObject(2)));
95 $this->assertTrue($closure(new TestObject(1)));
96 $this->assertFalse($closure(new TestObject(0)));
99 public function testWalkNotInComparison()
101 $closure = $this->visitor->walkComparison($this->builder->notIn("foo", array(1, 2, 3)));
103 $this->assertFalse($closure(new TestObject(1)));
104 $this->assertFalse($closure(new TestObject(2)));
105 $this->assertTrue($closure(new TestObject(0)));
106 $this->assertTrue($closure(new TestObject(4)));
109 public function testWalkAndCompositeExpression()
111 $closure = $this->visitor->walkCompositeExpression(
112 $this->builder->andX(
113 $this->builder->eq("foo", 1),
114 $this->builder->eq("bar", 1)
118 $this->assertTrue($closure(new TestObject(1, 1)));
119 $this->assertFalse($closure(new TestObject(1, 0)));
120 $this->assertFalse($closure(new TestObject(0, 1)));
121 $this->assertFalse($closure(new TestObject(0, 0)));
124 public function testWalkOrCompositeExpression()
126 $closure = $this->visitor->walkCompositeExpression(
128 $this->builder->eq("foo", 1),
129 $this->builder->eq("bar", 1)
133 $this->assertTrue($closure(new TestObject(1, 1)));
134 $this->assertTrue($closure(new TestObject(1, 0)));
135 $this->assertTrue($closure(new TestObject(0, 1)));
136 $this->assertFalse($closure(new TestObject(0, 0)));
139 public function testSortByFieldAscending()
141 $objects = array(new TestObject("b"), new TestObject("a"), new TestObject("c"));
142 $sort = ClosureExpressionVisitor::sortByField("foo");
144 usort($objects, $sort);
146 $this->assertEquals("a", $objects[0]->getFoo());
147 $this->assertEquals("b", $objects[1]->getFoo());
148 $this->assertEquals("c", $objects[2]->getFoo());
151 public function testSortByFieldDescending()
153 $objects = array(new TestObject("b"), new TestObject("a"), new TestObject("c"));
154 $sort = ClosureExpressionVisitor::sortByField("foo", -1);
156 usort($objects, $sort);
158 $this->assertEquals("c", $objects[0]->getFoo());
159 $this->assertEquals("b", $objects[1]->getFoo());
160 $this->assertEquals("a", $objects[2]->getFoo());
163 public function testSortDelegate()
165 $objects = array(new TestObject("a", "c"), new TestObject("a", "b"), new TestObject("a", "a"));
166 $sort = ClosureExpressionVisitor::sortByField("bar", 1);
167 $sort = ClosureExpressionVisitor::sortByField("foo", 1, $sort);
169 usort($objects, $sort);
171 $this->assertEquals("a", $objects[0]->getBar());
172 $this->assertEquals("b", $objects[1]->getBar());
173 $this->assertEquals("c", $objects[2]->getBar());
182 public function __construct($foo = null, $bar = null)
188 public function getFoo()
193 public function getBar()