3 namespace Doctrine\Tests\Common\Collections;
6 use Doctrine\Common\Collections\Criteria;
8 class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
11 * @var \Doctrine\Common\Collections\Collection
15 protected function setUp()
17 $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
20 public function testIssetAndUnset()
22 $this->assertFalse(isset($this->_coll[0]));
23 $this->_coll->add('testing');
24 $this->assertTrue(isset($this->_coll[0]));
25 unset($this->_coll[0]);
26 $this->assertFalse(isset($this->_coll[0]));
29 public function testToString()
31 $this->_coll->add('testing');
32 $this->assertTrue(is_string((string) $this->_coll));
35 public function testRemovingNonExistentEntryReturnsNull()
37 $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
40 public function testExists()
42 $this->_coll->add("one");
43 $this->_coll->add("two");
44 $exists = $this->_coll->exists(function($k, $e) { return $e == "one"; });
45 $this->assertTrue($exists);
46 $exists = $this->_coll->exists(function($k, $e) { return $e == "other"; });
47 $this->assertFalse($exists);
50 public function testMap()
54 $res = $this->_coll->map(function($e) { return $e * 2; });
55 $this->assertEquals(array(2, 4), $res->toArray());
58 public function testFilter()
61 $this->_coll->add("foo");
63 $res = $this->_coll->filter(function($e) { return is_numeric($e); });
64 $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
67 public function testFirstAndLast()
69 $this->_coll->add('one');
70 $this->_coll->add('two');
72 $this->assertEquals($this->_coll->first(), 'one');
73 $this->assertEquals($this->_coll->last(), 'two');
76 public function testArrayAccess()
78 $this->_coll[] = 'one';
79 $this->_coll[] = 'two';
81 $this->assertEquals($this->_coll[0], 'one');
82 $this->assertEquals($this->_coll[1], 'two');
84 unset($this->_coll[0]);
85 $this->assertEquals($this->_coll->count(), 1);
88 public function testContainsKey()
90 $this->_coll[5] = 'five';
91 $this->assertTrue($this->_coll->containsKey(5));
94 public function testContains()
96 $this->_coll[0] = 'test';
97 $this->assertTrue($this->_coll->contains('test'));
100 public function testSearch()
102 $this->_coll[0] = 'test';
103 $this->assertEquals(0, $this->_coll->indexOf('test'));
106 public function testGet()
108 $this->_coll[0] = 'test';
109 $this->assertEquals('test', $this->_coll->get(0));
112 public function testGetKeys()
114 $this->_coll[] = 'one';
115 $this->_coll[] = 'two';
116 $this->assertEquals(array(0, 1), $this->_coll->getKeys());
119 public function testGetValues()
121 $this->_coll[] = 'one';
122 $this->_coll[] = 'two';
123 $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
126 public function testCount()
128 $this->_coll[] = 'one';
129 $this->_coll[] = 'two';
130 $this->assertEquals($this->_coll->count(), 2);
131 $this->assertEquals(count($this->_coll), 2);
134 public function testForAll()
136 $this->_coll[] = 'one';
137 $this->_coll[] = 'two';
138 $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_string($e); }), true);
139 $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_array($e); }), false);
142 public function testPartition()
144 $this->_coll[] = true;
145 $this->_coll[] = false;
146 $partition = $this->_coll->partition(function($k, $e) { return $e == true; });
147 $this->assertEquals($partition[0][0], true);
148 $this->assertEquals($partition[1][0], false);
151 public function testClear()
153 $this->_coll[] = 'one';
154 $this->_coll[] = 'two';
155 $this->_coll->clear();
156 $this->assertEquals($this->_coll->isEmpty(), true);
159 public function testRemove()
161 $this->_coll[] = 'one';
162 $this->_coll[] = 'two';
163 $el = $this->_coll->remove(0);
165 $this->assertEquals('one', $el);
166 $this->assertEquals($this->_coll->contains('one'), false);
167 $this->assertNull($this->_coll->remove(0));
170 public function testRemoveElement()
172 $this->_coll[] = 'one';
173 $this->_coll[] = 'two';
175 $this->assertTrue($this->_coll->removeElement('two'));
176 $this->assertFalse($this->_coll->contains('two'));
177 $this->assertFalse($this->_coll->removeElement('two'));
180 public function testSlice()
182 $this->_coll[] = 'one';
183 $this->_coll[] = 'two';
184 $this->_coll[] = 'three';
186 $slice = $this->_coll->slice(0, 1);
187 $this->assertInternalType('array', $slice);
188 $this->assertEquals(array('one'), $slice);
190 $slice = $this->_coll->slice(1);
191 $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice);
193 $slice = $this->_coll->slice(1, 1);
194 $this->assertEquals(array(1 => 'two'), $slice);
197 public function fillMatchingFixture()
199 $std1 = new \stdClass();
201 $this->_coll[] = $std1;
203 $std2 = new \stdClass();
205 $this->_coll[] = $std2;
211 public function testMatching()
213 $this->fillMatchingFixture();
215 $col = $this->_coll->matching(new Criteria(Criteria::expr()->eq("foo", "bar")));
216 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
217 $this->assertNotSame($col, $this->_coll);
218 $this->assertEquals(1, count($col));
224 public function testMatchingOrdering()
226 $this->fillMatchingFixture();
228 $col = $this->_coll->matching(new Criteria(null, array('foo' => 'DESC')));
230 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
231 $this->assertNotSame($col, $this->_coll);
232 $this->assertEquals(2, count($col));
233 $this->assertEquals('baz', $col[0]->foo);
234 $this->assertEquals('bar', $col[1]->foo);
240 public function testMatchingSlice()
242 $this->fillMatchingFixture();
244 $col = $this->_coll->matching(new Criteria(null, null, 1, 1));
246 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col);
247 $this->assertNotSame($col, $this->_coll);
248 $this->assertEquals(1, count($col));
249 $this->assertEquals('baz', $col[0]->foo);