Initial import.

This commit is contained in:
ekes 2015-02-24 16:25:12 +01:00
commit 86383280c9
428 changed files with 68738 additions and 0 deletions

View file

@ -0,0 +1,198 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\ExpressionBuilder;
/**
* @group DDC-1637
*/
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;
}
}

View file

@ -0,0 +1,251 @@
<?php
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Tests;
use Doctrine\Common\Collections\Criteria;
class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
{
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $_coll;
protected function setUp()
{
$this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
}
public function testIssetAndUnset()
{
$this->assertFalse(isset($this->_coll[0]));
$this->_coll->add('testing');
$this->assertTrue(isset($this->_coll[0]));
unset($this->_coll[0]);
$this->assertFalse(isset($this->_coll[0]));
}
public function testToString()
{
$this->_coll->add('testing');
$this->assertTrue(is_string((string) $this->_coll));
}
public function testRemovingNonExistentEntryReturnsNull()
{
$this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
}
public function testExists()
{
$this->_coll->add("one");
$this->_coll->add("two");
$exists = $this->_coll->exists(function($k, $e) { return $e == "one"; });
$this->assertTrue($exists);
$exists = $this->_coll->exists(function($k, $e) { return $e == "other"; });
$this->assertFalse($exists);
}
public function testMap()
{
$this->_coll->add(1);
$this->_coll->add(2);
$res = $this->_coll->map(function($e) { return $e * 2; });
$this->assertEquals(array(2, 4), $res->toArray());
}
public function testFilter()
{
$this->_coll->add(1);
$this->_coll->add("foo");
$this->_coll->add(3);
$res = $this->_coll->filter(function($e) { return is_numeric($e); });
$this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
}
public function testFirstAndLast()
{
$this->_coll->add('one');
$this->_coll->add('two');
$this->assertEquals($this->_coll->first(), 'one');
$this->assertEquals($this->_coll->last(), 'two');
}
public function testArrayAccess()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals($this->_coll[0], 'one');
$this->assertEquals($this->_coll[1], 'two');
unset($this->_coll[0]);
$this->assertEquals($this->_coll->count(), 1);
}
public function testContainsKey()
{
$this->_coll[5] = 'five';
$this->assertTrue($this->_coll->containsKey(5));
}
public function testContains()
{
$this->_coll[0] = 'test';
$this->assertTrue($this->_coll->contains('test'));
}
public function testSearch()
{
$this->_coll[0] = 'test';
$this->assertEquals(0, $this->_coll->indexOf('test'));
}
public function testGet()
{
$this->_coll[0] = 'test';
$this->assertEquals('test', $this->_coll->get(0));
}
public function testGetKeys()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals(array(0, 1), $this->_coll->getKeys());
}
public function testGetValues()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals(array('one', 'two'), $this->_coll->getValues());
}
public function testCount()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals($this->_coll->count(), 2);
$this->assertEquals(count($this->_coll), 2);
}
public function testForAll()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals($this->_coll->forAll(function($k, $e) { return is_string($e); }), true);
$this->assertEquals($this->_coll->forAll(function($k, $e) { return is_array($e); }), false);
}
public function testPartition()
{
$this->_coll[] = true;
$this->_coll[] = false;
$partition = $this->_coll->partition(function($k, $e) { return $e == true; });
$this->assertEquals($partition[0][0], true);
$this->assertEquals($partition[1][0], false);
}
public function testClear()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->_coll->clear();
$this->assertEquals($this->_coll->isEmpty(), true);
}
public function testRemove()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$el = $this->_coll->remove(0);
$this->assertEquals('one', $el);
$this->assertEquals($this->_coll->contains('one'), false);
$this->assertNull($this->_coll->remove(0));
}
public function testRemoveElement()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertTrue($this->_coll->removeElement('two'));
$this->assertFalse($this->_coll->contains('two'));
$this->assertFalse($this->_coll->removeElement('two'));
}
public function testSlice()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->_coll[] = 'three';
$slice = $this->_coll->slice(0, 1);
$this->assertInternalType('array', $slice);
$this->assertEquals(array('one'), $slice);
$slice = $this->_coll->slice(1);
$this->assertEquals(array(1 => 'two', 2 => 'three'), $slice);
$slice = $this->_coll->slice(1, 1);
$this->assertEquals(array(1 => 'two'), $slice);
}
public function fillMatchingFixture()
{
$std1 = new \stdClass();
$std1->foo = "bar";
$this->_coll[] = $std1;
$std2 = new \stdClass();
$std2->foo = "baz";
$this->_coll[] = $std2;
}
/**
* @group DDC-1637
*/
public function testMatching()
{
$this->fillMatchingFixture();
$col = $this->_coll->matching(new Criteria(Criteria::expr()->eq("foo", "bar")));
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
$this->assertNotSame($col, $this->_coll);
$this->assertEquals(1, count($col));
}
/**
* @group DDC-1637
*/
public function testMatchingOrdering()
{
$this->fillMatchingFixture();
$col = $this->_coll->matching(new Criteria(null, array('foo' => 'DESC')));
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
$this->assertNotSame($col, $this->_coll);
$this->assertEquals(2, count($col));
$this->assertEquals('baz', $col[0]->foo);
$this->assertEquals('bar', $col[1]->foo);
}
/**
* @group DDC-1637
*/
public function testMatchingSlice()
{
$this->fillMatchingFixture();
$col = $this->_coll->matching(new Criteria(null, null, 1, 1));
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
$this->assertNotSame($col, $this->_coll);
$this->assertEquals(1, count($col));
$this->assertEquals('baz', $col[0]->foo);
}
}

View file

@ -0,0 +1,82 @@
<?php
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\CompositeExpression;
class CriteriaTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$criteria = Criteria::create();
$this->assertInstanceOf("Doctrine\Common\Collections\Criteria", $criteria);
}
public function testConstructor()
{
$expr = new Comparison("field", "=", "value");
$criteria = new Criteria($expr, array("foo" => "ASC"), 10, 20);
$this->assertSame($expr, $criteria->getWhereExpression());
$this->assertEquals(array("foo" => "ASC"), $criteria->getOrderings());
$this->assertEquals(10, $criteria->getFirstResult());
$this->assertEquals(20, $criteria->getMaxResults());
}
public function testWhere()
{
$expr = new Comparison("field", "=", "value");
$criteria = new Criteria();
$criteria->where($expr);
$this->assertSame($expr, $criteria->getWhereExpression());
}
public function testAndWhere()
{
$expr = new Comparison("field", "=", "value");
$criteria = new Criteria();
$criteria->where($expr);
$expr = $criteria->getWhereExpression();
$criteria->andWhere($expr);
$where = $criteria->getWhereExpression();
$this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $where);
$this->assertEquals(CompositeExpression::TYPE_AND, $where->getType());
$this->assertSame(array($expr, $expr), $where->getExpressionList());
}
public function testOrWhere()
{
$expr = new Comparison("field", "=", "value");
$criteria = new Criteria();
$criteria->where($expr);
$expr = $criteria->getWhereExpression();
$criteria->orWhere($expr);
$where = $criteria->getWhereExpression();
$this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $where);
$this->assertEquals(CompositeExpression::TYPE_OR, $where->getType());
$this->assertSame(array($expr, $expr), $where->getExpressionList());
}
public function testOrderings()
{
$criteria = Criteria::create()
->orderBy(array("foo" => "ASC"));
$this->assertEquals(array("foo" => "ASC"), $criteria->getOrderings());
}
public function testExpr()
{
$this->assertInstanceOf('Doctrine\Common\Collections\ExpressionBuilder', Criteria::expr());
}
}

View file

@ -0,0 +1,114 @@
<?php
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\CompositeExpression;
/**
* @group DDC-1637
*/
class ExpressionBuilderTest extends \PHPUnit_Framework_TestCase
{
private $builder;
public function setUp()
{
$this->builder = new ExpressionBuilder();
}
public function testAndX()
{
$expr = $this->builder->andX($this->builder->eq("a", "b"));
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\CompositeExpression", $expr);
$this->assertEquals(CompositeExpression::TYPE_AND, $expr->getType());
}
public function testOrX()
{
$expr = $this->builder->orX($this->builder->eq("a", "b"));
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\CompositeExpression", $expr);
$this->assertEquals(CompositeExpression::TYPE_OR, $expr->getType());
}
public function testInvalidAndXArgument()
{
$this->setExpectedException("RuntimeException");
$this->builder->andX("foo");
}
public function testEq()
{
$expr = $this->builder->eq("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::EQ, $expr->getOperator());
}
public function testNeq()
{
$expr = $this->builder->neq("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::NEQ, $expr->getOperator());
}
public function testLt()
{
$expr = $this->builder->lt("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::LT, $expr->getOperator());
}
public function testGt()
{
$expr = $this->builder->gt("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::GT, $expr->getOperator());
}
public function testGte()
{
$expr = $this->builder->gte("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::GTE, $expr->getOperator());
}
public function testLte()
{
$expr = $this->builder->lte("a", "b");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::LTE, $expr->getOperator());
}
public function testIn()
{
$expr = $this->builder->in("a", array("b"));
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::IN, $expr->getOperator());
}
public function testNotIn()
{
$expr = $this->builder->notIn("a", array("b"));
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::NIN, $expr->getOperator());
}
public function testIsNull()
{
$expr = $this->builder->isNull("a");
$this->assertInstanceOf("Doctrine\Common\Collections\Expr\Comparison", $expr);
$this->assertEquals(Comparison::IS, $expr->getOperator());
}
}