[BUGFIX] Fix access to empty ObjectStorage with numeric key
[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 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
27 $object1 = new \StdClass();
28 $object2 = new \StdClass();
29 $objectStorage->attach($object1);
30 $objectStorage->attach($object2, 'foo');
31 $this->assertEquals($objectStorage[$object1], NULL);
32 $this->assertEquals($objectStorage[$object2], 'foo');
33 }
34
35 /**
36 * @test
37 */
38 public function anObjectCanBeDetached() {
39 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
40 $object1 = new \StdClass();
41 $object2 = new \StdClass();
42 $objectStorage->attach($object1);
43 $objectStorage->attach($object2, 'foo');
44 $this->assertEquals(count($objectStorage), 2);
45 $objectStorage->detach($object1);
46 $this->assertEquals(count($objectStorage), 1);
47 $objectStorage->detach($object2);
48 $this->assertEquals(count($objectStorage), 0);
49 }
50
51 /**
52 * @test
53 */
54 public function offsetSetAssociatesDataToAnObjectInTheStorage() {
55 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
56 $object1 = new \StdClass();
57 $object2 = new \StdClass();
58 $objectStorage->offsetSet($object1, 'foo');
59 $this->assertEquals(count($objectStorage), 1);
60 $objectStorage[$object2] = 'bar';
61 $this->assertEquals(count($objectStorage), 2);
62 }
63
64 /**
65 * @test
66 */
67 public function offsetUnsetRemovesAnObjectFromTheStorage() {
68 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
69 $object1 = new \StdClass();
70 $object2 = new \StdClass();
71 $objectStorage->attach($object1);
72 $objectStorage->attach($object2, 'foo');
73 $this->assertEquals(count($objectStorage), 2);
74 $objectStorage->offsetUnset($object2);
75 $this->assertEquals(count($objectStorage), 1);
76 $objectStorage->offsetUnset($object1);
77 $this->assertEquals(count($objectStorage), 0);
78 }
79
80 /**
81 * @test
82 */
83 public function offsetGetReturnsTheDataAssociatedWithAnObject() {
84 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
85 $object1 = new \StdClass();
86 $object2 = new \StdClass();
87 $objectStorage[$object1] = 'foo';
88 $objectStorage->attach($object2);
89 $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
90 $this->assertEquals($objectStorage->offsetGet($object2), NULL);
91 }
92
93 /**
94 * @test
95 */
96 public function offsetExistsChecksWhetherAnObjectExistsInTheStorage() {
97 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
98 $object1 = new \StdClass();
99 $object2 = new \StdClass();
100 $objectStorage->attach($object1);
101 $this->assertEquals($objectStorage->offsetExists($object1), TRUE);
102 $this->assertEquals($objectStorage->offsetExists($object2), FALSE);
103 }
104
105 /**
106 * @test
107 */
108 public function offsetExistsWorksWithEmptyStorageAndIntegerKey() {
109 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
110 $this->assertEquals($objectStorage->offsetExists(0), FALSE);
111 }
112
113 /**
114 * @test
115 */
116 public function offsetExistsWorksWithEmptyStorageAndStringKey() {
117 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
118 $this->assertEquals($objectStorage->offsetExists('0'), FALSE);
119 }
120
121 /**
122 * @test
123 */
124 public function getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry() {
125 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
126 $object1 = new \StdClass();
127 $object2 = new \StdClass();
128 $object3 = new \StdClass();
129 $objectStorage->attach($object1, 42);
130 $objectStorage->attach($object2, 'foo');
131 $objectStorage->attach($object3, array('bar', 'baz'));
132 $objectStorage->rewind();
133 $this->assertEquals($objectStorage->getInfo(), 42);
134 $objectStorage->next();
135 $this->assertEquals($objectStorage->getInfo(), 'foo');
136 $objectStorage->next();
137 $this->assertEquals($objectStorage->getInfo(), array('bar', 'baz'));
138 }
139
140 /**
141 * @test
142 */
143 public function setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry() {
144 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
145 $object1 = new \StdClass();
146 $object2 = new \StdClass();
147 $objectStorage->attach($object1);
148 $objectStorage->attach($object2, 'foo');
149 $objectStorage->rewind();
150 $objectStorage->setInfo(42);
151 $objectStorage->next();
152 $objectStorage->setInfo('bar');
153 $this->assertEquals($objectStorage[$object1], 42);
154 $this->assertEquals($objectStorage[$object2], 'bar');
155 }
156
157 /**
158 * @test
159 */
160 public function removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage() {
161 $object1 = new \StdClass();
162 $object2 = new \StdClass();
163 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
164 $objectStorageA->attach($object1, 'foo');
165 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
166 $objectStorageB->attach($object1, 'bar');
167 $objectStorageB->attach($object2, 'baz');
168 $this->assertEquals(count($objectStorageB), 2);
169 $objectStorageB->removeAll($objectStorageA);
170 $this->assertEquals(count($objectStorageB), 1);
171 }
172
173 /**
174 * @test
175 */
176 public function addAllAddsAllObjectsFromAnotherStorage() {
177 $object1 = new \StdClass();
178 $object2 = new \StdClass();
179 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
180 // It might be better to mock this
181 $objectStorageA->attach($object1, 'foo');
182 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
183 $objectStorageB->attach($object2, 'baz');
184 $this->assertEquals($objectStorageB->offsetExists($object1), FALSE);
185 $objectStorageB->addAll($objectStorageA);
186 $this->assertEquals($objectStorageB[$object1], 'foo');
187 $this->assertEquals($objectStorageB[$object2], 'baz');
188 }
189
190 /**
191 * @test
192 */
193 public function theStorageCanBeRetrievedAsArray() {
194 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
195 $object1 = new \StdClass();
196 $object2 = new \StdClass();
197 $objectStorage->attach($object1, 'foo');
198 $objectStorage->attach($object2, 'bar');
199 $this->assertEquals($objectStorage->toArray(), array($object1, $object2));
200 }
201
202 /**
203 * @test
204 */
205 public function allRelationsAreNotDirtyOnAttaching() {
206 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
207 $object1 = new \StdClass();
208 $object2 = new \StdClass();
209 $object3 = new \StdClass();
210 $objectStorage->attach($object1);
211 $objectStorage->attach($object2);
212 $objectStorage->attach($object3);
213 $this->assertFalse($objectStorage->isRelationDirty($object1));
214 $this->assertFalse($objectStorage->isRelationDirty($object2));
215 $this->assertFalse($objectStorage->isRelationDirty($object3));
216 }
217
218 /**
219 * @test
220 */
221 public function allRelationsAreNotDirtyOnAttachingAndRemoving() {
222 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
223 $object1 = new \StdClass;
224 $object2 = new \StdClass;
225 $object3 = new \StdClass;
226 $objectStorage->attach($object1);
227 $objectStorage->attach($object2);
228 $objectStorage->detach($object2);
229 $objectStorage->attach($object3);
230 $this->assertFalse($objectStorage->isRelationDirty($object1));
231 $this->assertFalse($objectStorage->isRelationDirty($object3));
232 }
233
234 /**
235 * @test
236 */
237 public function theRelationsAreNotDirtyOnReAddingAtSamePosition() {
238 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
239 $object1 = new \StdClass;
240 $object2 = new \StdClass;
241 $objectStorage->attach($object1);
242 $objectStorage->attach($object2);
243 $clonedStorage = clone $objectStorage;
244 $objectStorage->removeAll($clonedStorage);
245 $objectStorage->attach($object1);
246 $objectStorage->attach($object2);
247 $this->assertFalse($objectStorage->isRelationDirty($object1));
248 $this->assertFalse($objectStorage->isRelationDirty($object2));
249 }
250
251 /**
252 * @test
253 */
254 public function theRelationsAreDirtyOnReAddingAtOtherPosition() {
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($object2);
263 $objectStorage->attach($object1);
264 $this->assertTrue($objectStorage->isRelationDirty($object1));
265 $this->assertTrue($objectStorage->isRelationDirty($object2));
266 }
267 }