[FEATURE] Allow calling toArray() on ObjectStorage using name getArray()
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / ObjectStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class ObjectStorageTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @test
24 */
25 public function anObjectCanBeAttached()
26 {
27 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
28 $object1 = new \StdClass();
29 $object2 = new \StdClass();
30 $objectStorage->attach($object1);
31 $objectStorage->attach($object2, 'foo');
32 $this->assertEquals($objectStorage[$object1], null);
33 $this->assertEquals($objectStorage[$object2], 'foo');
34 }
35
36 /**
37 * @test
38 */
39 public function anObjectCanBeDetached()
40 {
41 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
42 $object1 = new \StdClass();
43 $object2 = new \StdClass();
44 $objectStorage->attach($object1);
45 $objectStorage->attach($object2, 'foo');
46 $this->assertEquals(count($objectStorage), 2);
47 $objectStorage->detach($object1);
48 $this->assertEquals(count($objectStorage), 1);
49 $objectStorage->detach($object2);
50 $this->assertEquals(count($objectStorage), 0);
51 }
52
53 /**
54 * @test
55 */
56 public function offsetSetAssociatesDataToAnObjectInTheStorage()
57 {
58 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
59 $object1 = new \StdClass();
60 $object2 = new \StdClass();
61 $objectStorage->offsetSet($object1, 'foo');
62 $this->assertEquals(count($objectStorage), 1);
63 $objectStorage[$object2] = 'bar';
64 $this->assertEquals(count($objectStorage), 2);
65 }
66
67 /**
68 * @test
69 */
70 public function offsetUnsetRemovesAnObjectFromTheStorage()
71 {
72 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
73 $object1 = new \StdClass();
74 $object2 = new \StdClass();
75 $objectStorage->attach($object1);
76 $objectStorage->attach($object2, 'foo');
77 $this->assertEquals(count($objectStorage), 2);
78 $objectStorage->offsetUnset($object2);
79 $this->assertEquals(count($objectStorage), 1);
80 $objectStorage->offsetUnset($object1);
81 $this->assertEquals(count($objectStorage), 0);
82 }
83
84 /**
85 * @test
86 */
87 public function offsetGetReturnsTheDataAssociatedWithAnObject()
88 {
89 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
90 $object1 = new \StdClass();
91 $object2 = new \StdClass();
92 $objectStorage[$object1] = 'foo';
93 $objectStorage->attach($object2);
94 $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
95 $this->assertEquals($objectStorage->offsetGet($object2), null);
96 }
97
98 /**
99 * @test
100 */
101 public function offsetExistsChecksWhetherAnObjectExistsInTheStorage()
102 {
103 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
104 $object1 = new \StdClass();
105 $object2 = new \StdClass();
106 $objectStorage->attach($object1);
107 $this->assertEquals($objectStorage->offsetExists($object1), true);
108 $this->assertEquals($objectStorage->offsetExists($object2), false);
109 }
110
111 /**
112 * @test
113 */
114 public function offsetExistsWorksWithEmptyStorageAndIntegerKey()
115 {
116 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
117 $this->assertEquals($objectStorage->offsetExists(0), false);
118 }
119
120 /**
121 * @test
122 */
123 public function offsetExistsWorksWithEmptyStorageAndStringKey()
124 {
125 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
126 $this->assertEquals($objectStorage->offsetExists('0'), false);
127 }
128
129 /**
130 * @test
131 */
132 public function getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry()
133 {
134 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
135 $object1 = new \StdClass();
136 $object2 = new \StdClass();
137 $object3 = new \StdClass();
138 $objectStorage->attach($object1, 42);
139 $objectStorage->attach($object2, 'foo');
140 $objectStorage->attach($object3, array('bar', 'baz'));
141 $objectStorage->rewind();
142 $this->assertEquals($objectStorage->getInfo(), 42);
143 $objectStorage->next();
144 $this->assertEquals($objectStorage->getInfo(), 'foo');
145 $objectStorage->next();
146 $this->assertEquals($objectStorage->getInfo(), array('bar', 'baz'));
147 }
148
149 /**
150 * @test
151 */
152 public function setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry()
153 {
154 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
155 $object1 = new \StdClass();
156 $object2 = new \StdClass();
157 $objectStorage->attach($object1);
158 $objectStorage->attach($object2, 'foo');
159 $objectStorage->rewind();
160 $objectStorage->setInfo(42);
161 $objectStorage->next();
162 $objectStorage->setInfo('bar');
163 $this->assertEquals($objectStorage[$object1], 42);
164 $this->assertEquals($objectStorage[$object2], 'bar');
165 }
166
167 /**
168 * @test
169 */
170 public function removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage()
171 {
172 $object1 = new \StdClass();
173 $object2 = new \StdClass();
174 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
175 $objectStorageA->attach($object1, 'foo');
176 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
177 $objectStorageB->attach($object1, 'bar');
178 $objectStorageB->attach($object2, 'baz');
179 $this->assertEquals(count($objectStorageB), 2);
180 $objectStorageB->removeAll($objectStorageA);
181 $this->assertEquals(count($objectStorageB), 1);
182 }
183
184 /**
185 * @test
186 */
187 public function addAllAddsAllObjectsFromAnotherStorage()
188 {
189 $object1 = new \StdClass();
190 $object2 = new \StdClass();
191 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
192 // It might be better to mock this
193 $objectStorageA->attach($object1, 'foo');
194 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
195 $objectStorageB->attach($object2, 'baz');
196 $this->assertEquals($objectStorageB->offsetExists($object1), false);
197 $objectStorageB->addAll($objectStorageA);
198 $this->assertEquals($objectStorageB[$object1], 'foo');
199 $this->assertEquals($objectStorageB[$object2], 'baz');
200 }
201
202 /**
203 * @test
204 */
205 public function theStorageCanBeRetrievedAsArray()
206 {
207 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
208 $object1 = new \StdClass();
209 $object2 = new \StdClass();
210 $objectStorage->attach($object1, 'foo');
211 $objectStorage->attach($object2, 'bar');
212 $this->assertEquals(array($object1, $object2), $objectStorage->toArray());
213 $this->assertEquals(array($object1, $object2), $objectStorage->getArray());
214 }
215
216 /**
217 * @test
218 */
219 public function allRelationsAreNotDirtyOnAttaching()
220 {
221 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
222 $object1 = new \StdClass();
223 $object2 = new \StdClass();
224 $object3 = new \StdClass();
225 $objectStorage->attach($object1);
226 $objectStorage->attach($object2);
227 $objectStorage->attach($object3);
228 $this->assertFalse($objectStorage->isRelationDirty($object1));
229 $this->assertFalse($objectStorage->isRelationDirty($object2));
230 $this->assertFalse($objectStorage->isRelationDirty($object3));
231 }
232
233 /**
234 * @test
235 */
236 public function allRelationsAreNotDirtyOnAttachingAndRemoving()
237 {
238 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
239 $object1 = new \StdClass;
240 $object2 = new \StdClass;
241 $object3 = new \StdClass;
242 $objectStorage->attach($object1);
243 $objectStorage->attach($object2);
244 $objectStorage->detach($object2);
245 $objectStorage->attach($object3);
246 $this->assertFalse($objectStorage->isRelationDirty($object1));
247 $this->assertFalse($objectStorage->isRelationDirty($object3));
248 }
249
250 /**
251 * @test
252 */
253 public function theRelationsAreNotDirtyOnReAddingAtSamePosition()
254 {
255 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
256 $object1 = new \StdClass;
257 $object2 = new \StdClass;
258 $objectStorage->attach($object1);
259 $objectStorage->attach($object2);
260 $clonedStorage = clone $objectStorage;
261 $objectStorage->removeAll($clonedStorage);
262 $objectStorage->attach($object1);
263 $objectStorage->attach($object2);
264 $this->assertFalse($objectStorage->isRelationDirty($object1));
265 $this->assertFalse($objectStorage->isRelationDirty($object2));
266 }
267
268 /**
269 * @test
270 */
271 public function theRelationsAreDirtyOnReAddingAtOtherPosition()
272 {
273 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
274 $object1 = new \StdClass;
275 $object2 = new \StdClass;
276 $objectStorage->attach($object1);
277 $objectStorage->attach($object2);
278 $clonedStorage = clone $objectStorage;
279 $objectStorage->removeAll($clonedStorage);
280 $objectStorage->attach($object2);
281 $objectStorage->attach($object1);
282 $this->assertTrue($objectStorage->isRelationDirty($object1));
283 $this->assertTrue($objectStorage->isRelationDirty($object2));
284 }
285 }