diff --git a/.travis.yml b/.travis.yml index a86294bd2..5315ad81b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.3 - - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/composer.json b/composer.json index bfcd27f24..093971c90 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ {"name": "Johannes Schmitt", "email": "schmittjoh@gmail.com"} ], "require": { - "php": "^5.3.2 || ^7.0" + "php": "^5.5 || ^7.0" }, "require-dev": { "phpunit/phpunit": "~4.0" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3a93ce6a9..684196d96 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,7 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" + bootstrap="vendor/autoload.php" > diff --git a/tests/Doctrine/Tests/Common/Collections/AbstractLazyArrayCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/AbstractLazyArrayCollectionTest.php new file mode 100644 index 000000000..397771709 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Collections/AbstractLazyArrayCollectionTest.php @@ -0,0 +1,44 @@ +. + */ + +namespace Doctrine\Tests\Common\Collections; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Tests\LazyArrayCollection; + +/** + * Tests for {@see \Doctrine\Common\Collections\AbstractLazyCollection}. + * + * @covers \Doctrine\Common\Collections\AbstractLazyCollection + */ +class AbstractLazyCollectionTest extends BaseArrayCollectionTest +{ + protected function buildCollection(array $elements = []) + { + return new LazyArrayCollection(new ArrayCollection($elements)); + } + + public function testLazyCollection() + { + $collection = $this->buildCollection(['a', 'b', 'c']); + + $this->assertFalse($collection->isInitialized()); + $this->assertCount(3, $collection); + } +} diff --git a/tests/Doctrine/Tests/Common/Collections/AbstractLazyCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/AbstractLazyCollectionTest.php index 4de82cc20..68d425437 100644 --- a/tests/Doctrine/Tests/Common/Collections/AbstractLazyCollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/AbstractLazyCollectionTest.php @@ -2,19 +2,18 @@ namespace Doctrine\Tests\Common\Collections; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Tests\LazyArrayCollection; -class AbstractLazyCollectionTest extends \PHPUnit_Framework_TestCase +/** + * Tests for {@see \Doctrine\Common\Collections\AbstractLazyCollection}. + * + * @covers \Doctrine\Common\Collections\AbstractLazyCollection + */ +class LazyCollectionTest extends BaseCollectionTest { - public function testLazyCollection() + protected function setUp() { - $collection = new LazyArrayCollection(); - - $this->assertFalse($collection->isInitialized()); - $this->assertCount(3, $collection); - - $collection->add('bar'); - $this->assertTrue($collection->isInitialized()); - $this->assertCount(4, $collection); + $this->collection = new LazyArrayCollection(new ArrayCollection()); } } diff --git a/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php index c8b9f63e4..75d479eee 100644 --- a/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php @@ -20,317 +20,16 @@ namespace Doctrine\Tests\Common\Collections; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Criteria; -use Doctrine\Tests\DerivedArrayCollection; /** - * Tests for {@see \Doctrine\Common\Collections\ArrayCollection} + * Tests for {@see \Doctrine\Common\Collections\ArrayCollection}. * * @covers \Doctrine\Common\Collections\ArrayCollection */ -class ArrayCollectionTest extends \PHPUnit_Framework_TestCase +class ArrayCollectionTest extends BaseArrayCollectionTest { - /** - * @dataProvider provideDifferentElements - */ - public function testToArray($elements) + protected function buildCollection(array $elements = []) { - $collection = new ArrayCollection($elements); - - $this->assertSame($elements, $collection->toArray()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testFirst($elements) - { - $collection = new ArrayCollection($elements); - $this->assertSame(reset($elements), $collection->first()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testLast($elements) - { - $collection = new ArrayCollection($elements); - $this->assertSame(end($elements), $collection->last()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testKey($elements) - { - $collection = new ArrayCollection($elements); - - $this->assertSame(key($elements), $collection->key()); - - next($elements); - $collection->next(); - - $this->assertSame(key($elements), $collection->key()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testNext($elements) - { - $collection = new ArrayCollection($elements); - - while (true) { - $collectionNext = $collection->next(); - $arrayNext = next($elements); - - if(!$collectionNext || !$arrayNext) { - break; - } - - $this->assertSame($arrayNext, $collectionNext, "Returned value of ArrayCollection::next() and next() not match"); - $this->assertSame(key($elements), $collection->key(), "Keys not match"); - $this->assertSame(current($elements), $collection->current(), "Current values not match"); - } - } - - /** - * @dataProvider provideDifferentElements - */ - public function testCurrent($elements) - { - $collection = new ArrayCollection($elements); - - $this->assertSame(current($elements), $collection->current()); - - next($elements); - $collection->next(); - - $this->assertSame(current($elements), $collection->current()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testGetKeys($elements) - { - $collection = new ArrayCollection($elements); - - $this->assertSame(array_keys($elements), $collection->getKeys()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testGetValues($elements) - { - $collection = new ArrayCollection($elements); - - $this->assertSame(array_values($elements), $collection->getValues()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testCount($elements) - { - $collection = new ArrayCollection($elements); - - $this->assertSame(count($elements), $collection->count()); - } - - /** - * @dataProvider provideDifferentElements - */ - public function testIterator($elements) - { - $collection = new ArrayCollection($elements); - - $iterations = 0; - foreach($collection->getIterator() as $key => $item) { - $this->assertSame($elements[$key], $item, "Item {$key} not match"); - $iterations++; - } - - $this->assertEquals(count($elements), $iterations, "Number of iterations not match"); - } - - /** - * @return array - */ - public function provideDifferentElements() - { - return array( - 'indexed' => array(array(1, 2, 3, 4, 5)), - 'associative' => array(array('A' => 'a', 'B' => 'b', 'C' => 'c')), - 'mixed' => array(array('A' => 'a', 1, 'B' => 'b', 2, 3)), - ); - } - - public function testRemove() - { - $elements = array(1, 'A' => 'a', 2, 'B' => 'b', 3); - $collection = new ArrayCollection($elements); - - $this->assertEquals(1, $collection->remove(0)); - unset($elements[0]); - - $this->assertEquals(null, $collection->remove('non-existent')); - unset($elements['non-existent']); - - $this->assertEquals(2, $collection->remove(1)); - unset($elements[1]); - - $this->assertEquals('a', $collection->remove('A')); - unset($elements['A']); - - $this->assertEquals($elements, $collection->toArray()); - } - - public function testRemoveElement() - { - $elements = array(1, 'A' => 'a', 2, 'B' => 'b', 3, 'A2' => 'a', 'B2' => 'b'); - $collection = new ArrayCollection($elements); - - $this->assertTrue($collection->removeElement(1)); - unset($elements[0]); - - $this->assertFalse($collection->removeElement('non-existent')); - - $this->assertTrue($collection->removeElement('a')); - unset($elements['A']); - - $this->assertTrue($collection->removeElement('a')); - unset($elements['A2']); - - $this->assertEquals($elements, $collection->toArray()); - } - - public function testContainsKey() - { - $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'B2' => 'b'); - $collection = new ArrayCollection($elements); - - $this->assertTrue($collection->containsKey(0), "Contains index 0"); - $this->assertTrue($collection->containsKey('A'), "Contains key \"A\""); - $this->assertTrue($collection->containsKey('null'), "Contains key \"null\", with value null"); - $this->assertFalse($collection->containsKey('non-existent'), "Doesn't contain key"); - } - - public function testEmpty() - { - $collection = new ArrayCollection(); - $this->assertTrue($collection->isEmpty(), "Empty collection"); - - $collection->add(1); - $this->assertFalse($collection->isEmpty(), "Not empty collection"); - } - - public function testContains() - { - $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); - $collection = new ArrayCollection($elements); - - $this->assertTrue($collection->contains(0), "Contains Zero"); - $this->assertTrue($collection->contains('a'), "Contains \"a\""); - $this->assertTrue($collection->contains(null), "Contains Null"); - $this->assertFalse($collection->contains('non-existent'), "Doesn't contain an element"); - } - - public function testExists() - { - $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); - $collection = new ArrayCollection($elements); - - $this->assertTrue($collection->exists(function($key, $element) { - return $key == 'A' && $element == 'a'; - }), "Element exists"); - - $this->assertFalse($collection->exists(function($key, $element) { - return $key == 'non-existent' && $element == 'non-existent'; - }), "Element not exists"); - } - - public function testIndexOf() - { - $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); - $collection = new ArrayCollection($elements); - - $this->assertSame(array_search(2, $elements, true), $collection->indexOf(2), 'Index of 2'); - $this->assertSame(array_search(null, $elements, true), $collection->indexOf(null), 'Index of null'); - $this->assertSame(array_search('non-existent', $elements, true), $collection->indexOf('non-existent'), 'Index of non existent'); - } - - public function testGet() - { - $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); - $collection = new ArrayCollection($elements); - - $this->assertSame(2, $collection->get(1), 'Get element by index'); - $this->assertSame('a', $collection->get('A'), 'Get element by name'); - $this->assertSame(null, $collection->get('non-existent'), 'Get non existent element'); - } - - public function testMatchingWithSortingPreservesyKeys() - { - $object1 = new \stdClass(); - $object2 = new \stdClass(); - - $object1->sortField = 2; - $object2->sortField = 1; - - $collection = new ArrayCollection(array( - 'object1' => $object1, - 'object2' => $object2, - )); - - $this->assertSame( - array( - 'object2' => $object2, - 'object1' => $object1, - ), - $collection - ->matching(new Criteria(null, array('sortField' => Criteria::ASC))) - ->toArray() - ); - } - - public function testMultiColumnSortAppliesAllSorts() - { - $collection = new ArrayCollection(array( - array('foo' => 1, 'bar' => 2), - array('foo' => 2, 'bar' => 4), - array('foo' => 2, 'bar' => 3) - )); - - $expected = array( - 1 => array('foo' => 2, 'bar' => 4), - 2 => array('foo' => 2, 'bar' => 3), - 0 => array('foo' => 1, 'bar' => 2) - ); - - $this->assertSame( - $expected, - $collection - ->matching(new Criteria(null, array('foo' => Criteria::DESC, 'bar' => Criteria::DESC))) - ->toArray() - ); - } - - /** - * Tests that methods that create a new instance can be called in a derived - * class that implements different constructor semantics. - */ - public function testDerivedClassCreation() - { - $collection = new DerivedArrayCollection(new \stdClass); - $closure = function () { - // Intentionally empty. - }; - - self::assertInstanceOf('Doctrine\Tests\DerivedArrayCollection', $collection->map($closure)); - self::assertInstanceOf('Doctrine\Tests\DerivedArrayCollection', $collection->filter($closure)); - self::assertContainsOnlyInstancesOf('Doctrine\Tests\DerivedArrayCollection', $collection->partition($closure)); - self::assertInstanceOf('Doctrine\Tests\DerivedArrayCollection', $collection->matching(new Criteria)); + return new ArrayCollection($elements); } } diff --git a/tests/Doctrine/Tests/Common/Collections/BaseArrayCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/BaseArrayCollectionTest.php new file mode 100644 index 000000000..fb98155c6 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Collections/BaseArrayCollectionTest.php @@ -0,0 +1,328 @@ +. + */ + +namespace Doctrine\Tests\Common\Collections; + +use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; + +abstract class BaseArrayCollectionTest extends \PHPUnit_Framework_TestCase +{ + abstract protected function buildCollection(array $elements = []); + + protected function isSelectable($obj) + { + return $obj instanceof Selectable; + } + + /** + * @dataProvider provideDifferentElements + */ + public function testToArray($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame($elements, $collection->toArray()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testFirst($elements) + { + $collection = $this->buildCollection($elements); + $this->assertSame(reset($elements), $collection->first()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testLast($elements) + { + $collection = $this->buildCollection($elements); + $this->assertSame(end($elements), $collection->last()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testKey($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame(key($elements), $collection->key()); + + next($elements); + $collection->next(); + + $this->assertSame(key($elements), $collection->key()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testNext($elements) + { + $collection = $this->buildCollection($elements); + + while (true) { + $collectionNext = $collection->next(); + $arrayNext = next($elements); + + if (!$collectionNext || !$arrayNext) { + break; + } + + $this->assertSame($arrayNext, $collectionNext, 'Returned value of ArrayCollection::next() and next() not match'); + $this->assertSame(key($elements), $collection->key(), 'Keys not match'); + $this->assertSame(current($elements), $collection->current(), 'Current values not match'); + } + } + + /** + * @dataProvider provideDifferentElements + */ + public function testCurrent($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame(current($elements), $collection->current()); + + next($elements); + $collection->next(); + + $this->assertSame(current($elements), $collection->current()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testGetKeys($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame(array_keys($elements), $collection->getKeys()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testGetValues($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame(array_values($elements), $collection->getValues()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testCount($elements) + { + $collection = $this->buildCollection($elements); + + $this->assertSame(count($elements), $collection->count()); + } + + /** + * @dataProvider provideDifferentElements + */ + public function testIterator($elements) + { + $collection = $this->buildCollection($elements); + + $iterations = 0; + foreach ($collection->getIterator() as $key => $item) { + $this->assertSame($elements[$key], $item, "Item {$key} not match"); + ++$iterations; + } + + $this->assertEquals(count($elements), $iterations, 'Number of iterations not match'); + } + + /** + * @return array + */ + public function provideDifferentElements() + { + return [ + 'indexed' => [[1, 2, 3, 4, 5]], + 'associative' => [['A' => 'a', 'B' => 'b', 'C' => 'c']], + 'mixed' => [['A' => 'a', 1, 'B' => 'b', 2, 3]], + ]; + } + + public function testRemove() + { + $elements = [1, 'A' => 'a', 2, 'B' => 'b', 3]; + $collection = $this->buildCollection($elements); + + $this->assertEquals(1, $collection->remove(0)); + unset($elements[0]); + + $this->assertEquals(null, $collection->remove('non-existent')); + unset($elements['non-existent']); + + $this->assertEquals(2, $collection->remove(1)); + unset($elements[1]); + + $this->assertEquals('a', $collection->remove('A')); + unset($elements['A']); + + $this->assertEquals($elements, $collection->toArray()); + } + + public function testRemoveElement() + { + $elements = [1, 'A' => 'a', 2, 'B' => 'b', 3, 'A2' => 'a', 'B2' => 'b']; + $collection = $this->buildCollection($elements); + + $this->assertTrue($collection->removeElement(1)); + unset($elements[0]); + + $this->assertFalse($collection->removeElement('non-existent')); + + $this->assertTrue($collection->removeElement('a')); + unset($elements['A']); + + $this->assertTrue($collection->removeElement('a')); + unset($elements['A2']); + + $this->assertEquals($elements, $collection->toArray()); + } + + public function testContainsKey() + { + $elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'B2' => 'b']; + $collection = $this->buildCollection($elements); + + $this->assertTrue($collection->containsKey(0), 'Contains index 0'); + $this->assertTrue($collection->containsKey('A'), 'Contains key "A"'); + $this->assertTrue($collection->containsKey('null'), 'Contains key "null", with value null'); + $this->assertFalse($collection->containsKey('non-existent'), "Doesn't contain key"); + } + + public function testEmpty() + { + $collection = $this->buildCollection(); + $this->assertTrue($collection->isEmpty(), 'Empty collection'); + + $collection->add(1); + $this->assertFalse($collection->isEmpty(), 'Not empty collection'); + } + + public function testContains() + { + $elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0]; + $collection = $this->buildCollection($elements); + + $this->assertTrue($collection->contains(0), 'Contains Zero'); + $this->assertTrue($collection->contains('a'), 'Contains "a"'); + $this->assertTrue($collection->contains(null), 'Contains Null'); + $this->assertFalse($collection->contains('non-existent'), "Doesn't contain an element"); + } + + public function testExists() + { + $elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0]; + $collection = $this->buildCollection($elements); + + $this->assertTrue($collection->exists(function ($key, $element) { + return $key == 'A' && $element == 'a'; + }), 'Element exists'); + + $this->assertFalse($collection->exists(function ($key, $element) { + return $key == 'non-existent' && $element == 'non-existent'; + }), 'Element not exists'); + } + + public function testIndexOf() + { + $elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0]; + $collection = $this->buildCollection($elements); + + $this->assertSame(array_search(2, $elements, true), $collection->indexOf(2), 'Index of 2'); + $this->assertSame(array_search(null, $elements, true), $collection->indexOf(null), 'Index of null'); + $this->assertSame(array_search('non-existent', $elements, true), $collection->indexOf('non-existent'), 'Index of non existent'); + } + + public function testGet() + { + $elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0]; + $collection = $this->buildCollection($elements); + + $this->assertSame(2, $collection->get(1), 'Get element by index'); + $this->assertSame('a', $collection->get('A'), 'Get element by name'); + $this->assertSame(null, $collection->get('non-existent'), 'Get non existent element'); + } + + public function testMatchingWithSortingPreservesyKeys() + { + $object1 = new \stdClass(); + $object2 = new \stdClass(); + + $object1->sortField = 2; + $object2->sortField = 1; + + $collection = $this->buildCollection([ + 'object1' => $object1, + 'object2' => $object2, + ]); + + if (!$this->isSelectable($collection)) { + $this->markTestSkipped('Collection does not support Selectable interface'); + } + + $this->assertSame( + [ + 'object2' => $object2, + 'object1' => $object1, + ], + $collection + ->matching(new Criteria(null, ['sortField' => Criteria::ASC])) + ->toArray() + ); + } + + public function testMultiColumnSortAppliesAllSorts() + { + $collection = $this->buildCollection([ + ['foo' => 1, 'bar' => 2], + ['foo' => 2, 'bar' => 4], + ['foo' => 2, 'bar' => 3] + ]); + + $expected = [ + 1 => ['foo' => 2, 'bar' => 4], + 2 => ['foo' => 2, 'bar' => 3], + 0 => ['foo' => 1, 'bar' => 2] + ]; + + if (!$this->isSelectable($collection)) { + $this->markTestSkipped('Collection does not support Selectable interface'); + } + + $this->assertSame( + $expected, + $collection + ->matching(new Criteria(null, ['foo' => Criteria::DESC, 'bar' => Criteria::DESC])) + ->toArray() + ); + } +} diff --git a/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php new file mode 100644 index 000000000..527e314e7 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php @@ -0,0 +1,222 @@ +assertFalse(isset($this->collection[0])); + $this->collection->add('testing'); + $this->assertTrue(isset($this->collection[0])); + unset($this->collection[0]); + $this->assertFalse(isset($this->collection[0])); + } + + public function testRemovingNonExistentEntryReturnsNull() + { + $this->assertEquals(null, $this->collection->remove('testing_does_not_exist')); + } + + public function testExists() + { + $this->collection->add('one'); + $this->collection->add('two'); + $exists = $this->collection->exists(function ($k, $e) { + return $e == 'one'; + }); + $this->assertTrue($exists); + $exists = $this->collection->exists(function ($k, $e) { + return $e == 'other'; + }); + $this->assertFalse($exists); + } + + public function testMap() + { + $this->collection->add(1); + $this->collection->add(2); + $res = $this->collection->map(function ($e) { + return $e * 2; + }); + $this->assertEquals([2, 4], $res->toArray()); + } + + public function testFilter() + { + $this->collection->add(1); + $this->collection->add('foo'); + $this->collection->add(3); + $res = $this->collection->filter(function ($e) { + return is_numeric($e); + }); + $this->assertEquals([0 => 1, 2 => 3], $res->toArray()); + } + + public function testFirstAndLast() + { + $this->collection->add('one'); + $this->collection->add('two'); + + $this->assertEquals($this->collection->first(), 'one'); + $this->assertEquals($this->collection->last(), 'two'); + } + + public function testArrayAccess() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + + $this->assertEquals($this->collection[0], 'one'); + $this->assertEquals($this->collection[1], 'two'); + + unset($this->collection[0]); + $this->assertEquals($this->collection->count(), 1); + } + + public function testContainsKey() + { + $this->collection[5] = 'five'; + $this->assertTrue($this->collection->containsKey(5)); + } + + public function testContains() + { + $this->collection[0] = 'test'; + $this->assertTrue($this->collection->contains('test')); + } + + public function testSearch() + { + $this->collection[0] = 'test'; + $this->assertEquals(0, $this->collection->indexOf('test')); + } + + public function testGet() + { + $this->collection[0] = 'test'; + $this->assertEquals('test', $this->collection->get(0)); + } + + public function testGetKeys() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->assertEquals([0, 1], $this->collection->getKeys()); + } + + public function testGetValues() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->assertEquals(['one', 'two'], $this->collection->getValues()); + } + + public function testCount() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->assertEquals($this->collection->count(), 2); + $this->assertEquals(count($this->collection), 2); + } + + public function testForAll() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->assertEquals($this->collection->forAll(function ($k, $e) { + return is_string($e); + }), true); + $this->assertEquals($this->collection->forAll(function ($k, $e) { + return is_array($e); + }), false); + } + + public function testPartition() + { + $this->collection[] = true; + $this->collection[] = false; + $partition = $this->collection->partition(function ($k, $e) { + return $e == true; + }); + $this->assertEquals($partition[0][0], true); + $this->assertEquals($partition[1][0], false); + } + + public function testClear() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->collection->clear(); + $this->assertEquals($this->collection->isEmpty(), true); + } + + public function testRemove() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $el = $this->collection->remove(0); + + $this->assertEquals('one', $el); + $this->assertEquals($this->collection->contains('one'), false); + $this->assertNull($this->collection->remove(0)); + } + + public function testRemoveElement() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + + $this->assertTrue($this->collection->removeElement('two')); + $this->assertFalse($this->collection->contains('two')); + $this->assertFalse($this->collection->removeElement('two')); + } + + public function testSlice() + { + $this->collection[] = 'one'; + $this->collection[] = 'two'; + $this->collection[] = 'three'; + + $slice = $this->collection->slice(0, 1); + $this->assertInternalType('array', $slice); + $this->assertEquals(['one'], $slice); + + $slice = $this->collection->slice(1); + $this->assertEquals([1 => 'two', 2 => 'three'], $slice); + + $slice = $this->collection->slice(1, 1); + $this->assertEquals([1 => 'two'], $slice); + } + + public function fillMatchingFixture() + { + $std1 = new \stdClass(); + $std1->foo = 'bar'; + $this->collection[] = $std1; + + $std2 = new \stdClass(); + $std2->foo = 'baz'; + $this->collection[] = $std2; + } + + public function testCanRemoveNullValuesByKey() + { + $this->collection->add(null); + $this->collection->remove(0); + $this->assertTrue($this->collection->isEmpty()); + } + + public function testCanVerifyExistingKeysWithNullValues() + { + $this->collection->set('key', null); + $this->assertTrue($this->collection->containsKey('key')); + } +} diff --git a/tests/Doctrine/Tests/Common/Collections/CollectionTest.php b/tests/Doctrine/Tests/Common/Collections/CollectionTest.php index f3f91ad3f..65e17d960 100644 --- a/tests/Doctrine/Tests/Common/Collections/CollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/CollectionTest.php @@ -6,206 +6,19 @@ use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; -class CollectionTest extends \PHPUnit_Framework_TestCase +class CollectionTest extends BaseCollectionTest { - /** - * @var Collection - */ - private $collection; - protected function setUp() { $this->collection = new ArrayCollection(); } - public function testIssetAndUnset() - { - $this->assertFalse(isset($this->collection[0])); - $this->collection->add('testing'); - $this->assertTrue(isset($this->collection[0])); - unset($this->collection[0]); - $this->assertFalse(isset($this->collection[0])); - } - public function testToString() { $this->collection->add('testing'); $this->assertTrue(is_string((string) $this->collection)); } - public function testRemovingNonExistentEntryReturnsNull() - { - $this->assertEquals(null, $this->collection->remove('testing_does_not_exist')); - } - - public function testExists() - { - $this->collection->add("one"); - $this->collection->add("two"); - $exists = $this->collection->exists(function($k, $e) { return $e == "one"; }); - $this->assertTrue($exists); - $exists = $this->collection->exists(function($k, $e) { return $e == "other"; }); - $this->assertFalse($exists); - } - - public function testMap() - { - $this->collection->add(1); - $this->collection->add(2); - $res = $this->collection->map(function($e) { return $e * 2; }); - $this->assertEquals(array(2, 4), $res->toArray()); - } - - public function testFilter() - { - $this->collection->add(1); - $this->collection->add("foo"); - $this->collection->add(3); - $res = $this->collection->filter(function($e) { return is_numeric($e); }); - $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray()); - } - - public function testFirstAndLast() - { - $this->collection->add('one'); - $this->collection->add('two'); - - $this->assertEquals($this->collection->first(), 'one'); - $this->assertEquals($this->collection->last(), 'two'); - } - - public function testArrayAccess() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - - $this->assertEquals($this->collection[0], 'one'); - $this->assertEquals($this->collection[1], 'two'); - - unset($this->collection[0]); - $this->assertEquals($this->collection->count(), 1); - } - - public function testContainsKey() - { - $this->collection[5] = 'five'; - $this->assertTrue($this->collection->containsKey(5)); - } - - public function testContains() - { - $this->collection[0] = 'test'; - $this->assertTrue($this->collection->contains('test')); - } - - public function testSearch() - { - $this->collection[0] = 'test'; - $this->assertEquals(0, $this->collection->indexOf('test')); - } - - public function testGet() - { - $this->collection[0] = 'test'; - $this->assertEquals('test', $this->collection->get(0)); - } - - public function testGetKeys() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->assertEquals(array(0, 1), $this->collection->getKeys()); - } - - public function testGetValues() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->assertEquals(array('one', 'two'), $this->collection->getValues()); - } - - public function testCount() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->assertEquals($this->collection->count(), 2); - $this->assertEquals(count($this->collection), 2); - } - - public function testForAll() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->assertEquals($this->collection->forAll(function($k, $e) { return is_string($e); }), true); - $this->assertEquals($this->collection->forAll(function($k, $e) { return is_array($e); }), false); - } - - public function testPartition() - { - $this->collection[] = true; - $this->collection[] = false; - $partition = $this->collection->partition(function($k, $e) { return $e == true; }); - $this->assertEquals($partition[0][0], true); - $this->assertEquals($partition[1][0], false); - } - - public function testClear() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->collection->clear(); - $this->assertEquals($this->collection->isEmpty(), true); - } - - public function testRemove() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $el = $this->collection->remove(0); - - $this->assertEquals('one', $el); - $this->assertEquals($this->collection->contains('one'), false); - $this->assertNull($this->collection->remove(0)); - } - - public function testRemoveElement() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - - $this->assertTrue($this->collection->removeElement('two')); - $this->assertFalse($this->collection->contains('two')); - $this->assertFalse($this->collection->removeElement('two')); - } - - public function testSlice() - { - $this->collection[] = 'one'; - $this->collection[] = 'two'; - $this->collection[] = 'three'; - - $slice = $this->collection->slice(0, 1); - $this->assertInternalType('array', $slice); - $this->assertEquals(array('one'), $slice); - - $slice = $this->collection->slice(1); - $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice); - - $slice = $this->collection->slice(1, 1); - $this->assertEquals(array(1 => 'two'), $slice); - } - - public function fillMatchingFixture() - { - $std1 = new \stdClass(); - $std1->foo = "bar"; - $this->collection[] = $std1; - - $std2 = new \stdClass(); - $std2->foo = "baz"; - $this->collection[] = $std2; - } - /** * @group DDC-1637 */ @@ -213,8 +26,8 @@ public function testMatching() { $this->fillMatchingFixture(); - $col = $this->collection->matching(new Criteria(Criteria::expr()->eq("foo", "bar"))); - $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); + $col = $this->collection->matching(new Criteria(Criteria::expr()->eq('foo', 'bar'))); + $this->assertInstanceOf(Collection::class, $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(1, count($col)); } @@ -226,9 +39,9 @@ public function testMatchingOrdering() { $this->fillMatchingFixture(); - $col = $this->collection->matching(new Criteria(null, array('foo' => 'DESC'))); + $col = $this->collection->matching(new Criteria(null, ['foo' => 'DESC'])); - $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); + $this->assertInstanceOf(Collection::class, $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(2, count($col)); $this->assertEquals('baz', $col->first()->foo); @@ -244,22 +57,9 @@ public function testMatchingSlice() $col = $this->collection->matching(new Criteria(null, null, 1, 1)); - $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); + $this->assertInstanceOf(Collection::class, $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(1, count($col)); $this->assertEquals('baz', $col[0]->foo); } - - public function testCanRemoveNullValuesByKey() - { - $this->collection->add(null); - $this->collection->remove(0); - $this->assertTrue($this->collection->isEmpty()); - } - - public function testCanVerifyExistingKeysWithNullValues() - { - $this->collection->set('key', null); - $this->assertTrue($this->collection->containsKey('key')); - } } diff --git a/tests/Doctrine/Tests/Common/Collections/DerivedCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/DerivedCollectionTest.php new file mode 100644 index 000000000..e4054276e --- /dev/null +++ b/tests/Doctrine/Tests/Common/Collections/DerivedCollectionTest.php @@ -0,0 +1,45 @@ +. + */ + +namespace Doctrine\Tests\Common\Collections; + +use Doctrine\Tests\DerivedArrayCollection; + +/** + * @author Alexander Golovnya + */ +class DerivedCollectionTest +{ + /** + * Tests that methods that create a new instance can be called in a derived + * class that implements different constructor semantics. + */ + public function testDerivedClassCreation() + { + $collection = new DerivedArrayCollection(new \stdClass()); + $closure = function () { + return $allMatches = false; + }; + + self::assertInstanceOf(DerivedArrayCollection::class, $collection->map($closure)); + self::assertInstanceOf(DerivedArrayCollection::class, $collection->filter($closure)); + self::assertContainsOnlyInstancesOf(DerivedArrayCollection::class, $collection->partition($closure)); + self::assertInstanceOf(DerivedArrayCollection::class, $collection->matching(new Criteria())); + } +} diff --git a/tests/Doctrine/Tests/DerivedArrayCollection.php b/tests/Doctrine/Tests/DerivedArrayCollection.php index f799ce4fa..007419174 100644 --- a/tests/Doctrine/Tests/DerivedArrayCollection.php +++ b/tests/Doctrine/Tests/DerivedArrayCollection.php @@ -1,8 +1,12 @@ collectionOnInitialization = $collection; + } + /** - * Do the initialization logic - * - * @return void + * Do the initialization logic. */ protected function doInitialize() { - $this->collection = new ArrayCollection(array('a', 'b', 'c')); + $this->collection = $this->collectionOnInitialization; } }