90390c47f1e60d412c413b0cf784ce594bf6c565
[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 getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry() {
109 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
110 $object1 = new \StdClass();
111 $object2 = new \StdClass();
112 $object3 = new \StdClass();
113 $objectStorage->attach($object1, 42);
114 $objectStorage->attach($object2, 'foo');
115 $objectStorage->attach($object3, array('bar', 'baz'));
116 $objectStorage->rewind();
117 $this->assertEquals($objectStorage->getInfo(), 42);
118 $objectStorage->next();
119 $this->assertEquals($objectStorage->getInfo(), 'foo');
120 $objectStorage->next();
121 $this->assertEquals($objectStorage->getInfo(), array('bar', 'baz'));
122 }
123
124 /**
125 * @test
126 */
127 public function setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry() {
128 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
129 $object1 = new \StdClass();
130 $object2 = new \StdClass();
131 $objectStorage->attach($object1);
132 $objectStorage->attach($object2, 'foo');
133 $objectStorage->rewind();
134 $objectStorage->setInfo(42);
135 $objectStorage->next();
136 $objectStorage->setInfo('bar');
137 $this->assertEquals($objectStorage[$object1], 42);
138 $this->assertEquals($objectStorage[$object2], 'bar');
139 }
140
141 /**
142 * @test
143 */
144 public function removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage() {
145 $object1 = new \StdClass();
146 $object2 = new \StdClass();
147 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
148 $objectStorageA->attach($object1, 'foo');
149 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
150 $objectStorageB->attach($object1, 'bar');
151 $objectStorageB->attach($object2, 'baz');
152 $this->assertEquals(count($objectStorageB), 2);
153 $objectStorageB->removeAll($objectStorageA);
154 $this->assertEquals(count($objectStorageB), 1);
155 }
156
157 /**
158 * @test
159 */
160 public function addAllAddsAllObjectsFromAnotherStorage() {
161 $object1 = new \StdClass();
162 $object2 = new \StdClass();
163 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
164 // It might be better to mock this
165 $objectStorageA->attach($object1, 'foo');
166 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
167 $objectStorageB->attach($object2, 'baz');
168 $this->assertEquals($objectStorageB->offsetExists($object1), FALSE);
169 $objectStorageB->addAll($objectStorageA);
170 $this->assertEquals($objectStorageB[$object1], 'foo');
171 $this->assertEquals($objectStorageB[$object2], 'baz');
172 }
173
174 /**
175 * @test
176 */
177 public function theStorageCanBeRetrievedAsArray() {
178 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
179 $object1 = new \StdClass();
180 $object2 = new \StdClass();
181 $objectStorage->attach($object1, 'foo');
182 $objectStorage->attach($object2, 'bar');
183 $this->assertEquals($objectStorage->toArray(), array($object1, $object2));
184 }
185
186 /**
187 * @test
188 */
189 public function allRelationsAreNotDirtyOnAttaching() {
190 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
191 $object1 = new \StdClass();
192 $object2 = new \StdClass();
193 $object3 = new \StdClass();
194 $objectStorage->attach($object1);
195 $objectStorage->attach($object2);
196 $objectStorage->attach($object3);
197 $this->assertFalse($objectStorage->isRelationDirty($object1));
198 $this->assertFalse($objectStorage->isRelationDirty($object2));
199 $this->assertFalse($objectStorage->isRelationDirty($object3));
200 }
201
202 /**
203 * @test
204 */
205 public function allRelationsAreNotDirtyOnAttachingAndRemoving() {
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->detach($object2);
213 $objectStorage->attach($object3);
214 $this->assertFalse($objectStorage->isRelationDirty($object1));
215 $this->assertFalse($objectStorage->isRelationDirty($object3));
216 }
217
218 /**
219 * @test
220 */
221 public function theRelationsAreNotDirtyOnReAddingAtSamePosition() {
222 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
223 $object1 = new \StdClass;
224 $object2 = new \StdClass;
225 $objectStorage->attach($object1);
226 $objectStorage->attach($object2);
227 $clonedStorage = clone $objectStorage;
228 $objectStorage->removeAll($clonedStorage);
229 $objectStorage->attach($object1);
230 $objectStorage->attach($object2);
231 $this->assertFalse($objectStorage->isRelationDirty($object1));
232 $this->assertFalse($objectStorage->isRelationDirty($object2));
233 }
234
235 /**
236 * @test
237 */
238 public function theRelationsAreDirtyOnReAddingAtOtherPosition() {
239 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
240 $object1 = new \StdClass;
241 $object2 = new \StdClass;
242 $objectStorage->attach($object1);
243 $objectStorage->attach($object2);
244 $clonedStorage = clone $objectStorage;
245 $objectStorage->removeAll($clonedStorage);
246 $objectStorage->attach($object2);
247 $objectStorage->attach($object1);
248 $this->assertTrue($objectStorage->isRelationDirty($object1));
249 $this->assertTrue($objectStorage->isRelationDirty($object2));
250 }
251 }