1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] Testing Collection base class and removing some code

This commit is contained in:
jwage 2009-02-17 21:18:13 +00:00
parent 15beb5e43b
commit 554adc32a4
2 changed files with 131 additions and 36 deletions

View File

@ -31,7 +31,7 @@ use \ArrayIterator;
* A Collection is a thin wrapper around a php array. Think of it as an OO version
* of a plain array.
*
* @author robo
* @author Roman S. Borschel
* @since 2.0
*/
class Collection implements Countable, IteratorAggregate, ArrayAccess
@ -45,8 +45,9 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
protected $_elements = array();
/**
* Constructor accepts an array of $elements
*
* @param <type> $elements
* @param array $elements
*/
public function __construct(array $elements = array())
{
@ -210,23 +211,13 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* @param Closure $p The predicate.
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
*/
public function exists(Closure $p) {
public function exists(Closure $p)
{
foreach ($this->_elements as $key => $element)
if ($p($key, $element)) return true;
return false;
}
/**
* TODO
*
* @param unknown_type $otherColl
* @todo Impl
*/
public function containsAll($otherColl)
{
throw new DoctrineException("Not yet implemented.");
}
/**
* Searches for a given element and, if found, returns the corresponding key/index
* of that element. The comparison of two elements is strict, that means not
@ -313,16 +304,6 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
return true;
}
/**
* Adds all entities of the other collection to this collection.
*
* @param unknown_type $otherCollection
* @todo Impl
*/
public function addAll($otherCollection)
{
}
/**
* Checks whether the collection is empty.
* Note: This is preferrable over count() == 0.
@ -332,7 +313,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
public function isEmpty()
{
// Note: Little "trick". Empty arrays evaluate to FALSE. No need to count().
return ! (bool)$this->_elements;
return ! (bool) $this->_elements;
}
/**
@ -375,10 +356,13 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* @param Closure $p The predicate.
* @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
*/
public function forall(Closure $p)
public function forAll(Closure $p)
{
foreach ($this->_elements as $key => $element)
if ( ! $p($key, $element)) return false;
foreach ($this->_elements as $key => $element) {
if ( ! $p($key, $element)) {
return false;
}
}
return true;
}
@ -419,5 +403,4 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
{
$this->_elements = array();
}
}
}

View File

@ -12,14 +12,17 @@ require_once __DIR__ . '/../../TestInit.php';
* @author robo
* @since 2.0
*/
class CollectionTest extends \Doctrine\Tests\DoctrineTestCase {
class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
{
private $_coll;
protected function setUp() {
protected function setUp()
{
$this->_coll = new \Doctrine\Common\Collections\Collection;
}
public function testExists() {
public function testExists()
{
$this->_coll->add("one");
$this->_coll->add("two");
$exists = $this->_coll->exists(function($key, $element) { return $element == "one"; });
@ -28,19 +31,128 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase {
$this->assertFalse($exists);
}
public function testMap() {
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->unwrap());
}
public function testFilter() {
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->unwrap());
}
}
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->assertEquals($this->_coll->containsKey(5), true);
}
public function testContains()
{
$this->_coll[0] = 'test';
$this->assertEquals($this->_coll->contains('test'), true);
}
public function testSearch()
{
$this->_coll[0] = 'test';
$this->assertEquals($this->_coll->search('test'), 0);
}
public function testGet()
{
$this->_coll[0] = 'test';
$this->assertEquals($this->_coll->get(0), 'test');
}
public function testGetKeys()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals($this->_coll->getKeys(), array(0, 1));
}
public function testGetElements()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->assertEquals($this->_coll->getElements(), array('one', 'two'));
}
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($key, $element) { return is_string($element); }), true);
$this->assertEquals($this->_coll->forAll(function($key, $element) { return is_array($element); }), false);
}
public function testPartition()
{
$this->_coll[] = true;
$this->_coll[] = false;
$partition = $this->_coll->partition(function($key, $element) { return $element == 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';
$this->_coll->remove(0);
$this->assertEquals($this->_coll->contains('one'), false);
}
public function testRemoveElement()
{
$this->_coll[] = 'one';
$this->_coll[] = 'two';
$this->_coll->removeElement('two');
$this->assertEquals($this->_coll->contains('two'), false);
}
}