Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Collections / CollectionTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Collections;
4
5 use Doctrine\Tests;
6 use Doctrine\Common\Collections\Criteria;
7
8 class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
9 {
10     /**
11      * @var \Doctrine\Common\Collections\Collection
12      */
13     private $_coll;
14
15     protected function setUp()
16     {
17         $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
18     }
19
20     public function testIssetAndUnset()
21     {
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]));
27     }
28
29     public function testToString()
30     {
31         $this->_coll->add('testing');
32         $this->assertTrue(is_string((string) $this->_coll));
33     }
34
35     public function testRemovingNonExistentEntryReturnsNull()
36     {
37         $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
38     }
39
40     public function testExists()
41     {
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);
48     }
49
50     public function testMap()
51     {
52         $this->_coll->add(1);
53         $this->_coll->add(2);
54         $res = $this->_coll->map(function($e) { return $e * 2; });
55         $this->assertEquals(array(2, 4), $res->toArray());
56     }
57
58     public function testFilter()
59     {
60         $this->_coll->add(1);
61         $this->_coll->add("foo");
62         $this->_coll->add(3);
63         $res = $this->_coll->filter(function($e) { return is_numeric($e); });
64         $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
65     }
66
67     public function testFirstAndLast()
68     {
69         $this->_coll->add('one');
70         $this->_coll->add('two');
71
72         $this->assertEquals($this->_coll->first(), 'one');
73         $this->assertEquals($this->_coll->last(), 'two');
74     }
75
76     public function testArrayAccess()
77     {
78         $this->_coll[] = 'one';
79         $this->_coll[] = 'two';
80
81         $this->assertEquals($this->_coll[0], 'one');
82         $this->assertEquals($this->_coll[1], 'two');
83
84         unset($this->_coll[0]);
85         $this->assertEquals($this->_coll->count(), 1);
86     }
87
88     public function testContainsKey()
89     {
90         $this->_coll[5] = 'five';
91         $this->assertTrue($this->_coll->containsKey(5));
92     }
93
94     public function testContains()
95     {
96         $this->_coll[0] = 'test';
97         $this->assertTrue($this->_coll->contains('test'));
98     }
99
100     public function testSearch()
101     {
102         $this->_coll[0] = 'test';
103         $this->assertEquals(0, $this->_coll->indexOf('test'));
104     }
105
106     public function testGet()
107     {
108         $this->_coll[0] = 'test';
109         $this->assertEquals('test', $this->_coll->get(0));
110     }
111
112     public function testGetKeys()
113     {
114         $this->_coll[] = 'one';
115         $this->_coll[] = 'two';
116         $this->assertEquals(array(0, 1), $this->_coll->getKeys());
117     }
118
119     public function testGetValues()
120     {
121         $this->_coll[] = 'one';
122         $this->_coll[] = 'two';
123         $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
124     }
125
126     public function testCount()
127     {
128         $this->_coll[] = 'one';
129         $this->_coll[] = 'two';
130         $this->assertEquals($this->_coll->count(), 2);
131         $this->assertEquals(count($this->_coll), 2);
132     }
133
134     public function testForAll()
135     {
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);
140     }
141
142     public function testPartition()
143     {
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);
149     }
150
151     public function testClear()
152     {
153         $this->_coll[] = 'one';
154         $this->_coll[] = 'two';
155         $this->_coll->clear();
156         $this->assertEquals($this->_coll->isEmpty(), true);
157     }
158
159     public function testRemove()
160     {
161         $this->_coll[] = 'one';
162         $this->_coll[] = 'two';
163         $el = $this->_coll->remove(0);
164
165         $this->assertEquals('one', $el);
166         $this->assertEquals($this->_coll->contains('one'), false);
167         $this->assertNull($this->_coll->remove(0));
168     }
169
170     public function testRemoveElement()
171     {
172         $this->_coll[] = 'one';
173         $this->_coll[] = 'two';
174
175         $this->assertTrue($this->_coll->removeElement('two'));
176         $this->assertFalse($this->_coll->contains('two'));
177         $this->assertFalse($this->_coll->removeElement('two'));
178     }
179
180     public function testSlice()
181     {
182         $this->_coll[] = 'one';
183         $this->_coll[] = 'two';
184         $this->_coll[] = 'three';
185
186         $slice = $this->_coll->slice(0, 1);
187         $this->assertInternalType('array', $slice);
188         $this->assertEquals(array('one'), $slice);
189
190         $slice = $this->_coll->slice(1);
191         $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice);
192
193         $slice = $this->_coll->slice(1, 1);
194         $this->assertEquals(array(1 => 'two'), $slice);
195     }
196
197     public function fillMatchingFixture()
198     {
199         $std1 = new \stdClass();
200         $std1->foo = "bar";
201         $this->_coll[] = $std1;
202
203         $std2 = new \stdClass();
204         $std2->foo = "baz";
205         $this->_coll[] = $std2;
206     }
207
208     /**
209      * @group DDC-1637
210      */
211     public function testMatching()
212     {
213         $this->fillMatchingFixture();
214
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));
219     }
220
221     /**
222      * @group DDC-1637
223      */
224     public function testMatchingOrdering()
225     {
226         $this->fillMatchingFixture();
227
228         $col = $this->_coll->matching(new Criteria(null, array('foo' => 'DESC')));
229
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);
235     }
236
237     /**
238      * @group DDC-1637
239      */
240     public function testMatchingSlice()
241     {
242         $this->fillMatchingFixture();
243
244         $col = $this->_coll->matching(new Criteria(null, null, 1, 1));
245
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);
250     }
251 }