‪TYPO3CMS  10.4
ObjectStorageTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪ObjectStorageTest extends UnitTestCase
25 {
29  public function ‪anObjectCanBeAttached()
30  {
31  $objectStorage = new ‪ObjectStorage();
32  $object1 = new \stdClass();
33  $object2 = new \stdClass();
34  $objectStorage->attach($object1);
35  $objectStorage->attach($object2, 'foo');
36  self::assertEquals($objectStorage[$object1], null);
37  self::assertEquals($objectStorage[$object2], 'foo');
38  }
39 
43  public function ‪anObjectCanBeDetached()
44  {
45  $objectStorage = new ‪ObjectStorage();
46  $object1 = new \stdClass();
47  $object2 = new \stdClass();
48  $objectStorage->attach($object1);
49  $objectStorage->attach($object2, 'foo');
50  self::assertEquals(count($objectStorage), 2);
51  $objectStorage->detach($object1);
52  self::assertEquals(count($objectStorage), 1);
53  $objectStorage->detach($object2);
54  self::assertEquals(count($objectStorage), 0);
55  }
56 
61  {
62  $objectStorage = new ‪ObjectStorage();
63  $object1 = new \stdClass();
64  $object2 = new \stdClass();
65  $objectStorage->offsetSet($object1, 'foo');
66  self::assertEquals(count($objectStorage), 1);
67  $objectStorage[$object2] = 'bar';
68  self::assertEquals(count($objectStorage), 2);
69  }
70 
75  {
76  $objectStorage = new ‪ObjectStorage();
77  $object1 = new \stdClass();
78  $object2 = new \stdClass();
79  $objectStorage->attach($object1);
80  $objectStorage->attach($object2, 'foo');
81  self::assertEquals(count($objectStorage), 2);
82  $objectStorage->offsetUnset($object2);
83  self::assertEquals(count($objectStorage), 1);
84  $objectStorage->offsetUnset($object1);
85  self::assertEquals(count($objectStorage), 0);
86  }
87 
92  {
93  $objectStorage = new ‪ObjectStorage();
94  $object1 = new \stdClass();
95  $object2 = new \stdClass();
96  $objectStorage->attach($object1);
97  $objectStorage->attach($object2, 'foo');
98  self::assertEquals(count($objectStorage), 2);
99  $objectStorage->offsetUnset(0);
100  self::assertEquals(count($objectStorage), 1);
101  $objectStorage->offsetUnset(0);
102  self::assertEquals(count($objectStorage), 0);
103  }
104 
109  {
110  $objectStorage = new ‪ObjectStorage();
111  $object1 = new \stdClass();
112  $object2 = new \stdClass();
113  $objectStorage[$object1] = 'foo';
114  $objectStorage->attach($object2);
115  self::assertEquals($objectStorage->offsetGet($object1), 'foo');
116  self::assertEquals($objectStorage->offsetGet($object2), null);
117  }
118 
123  {
124  $objectStorage = new ‪ObjectStorage();
125  $object1 = new \stdClass();
126  $object2 = new \stdClass();
127  $objectStorage->attach($object1);
128  $objectStorage->attach($object2);
129  self::assertSame($object1, $objectStorage->offsetGet(0));
130  self::assertSame($object2, $objectStorage->offsetGet(1));
131  }
132 
137  {
138  $objectStorage = new ‪ObjectStorage();
139  $object1 = new \stdClass();
140  $object2 = new \stdClass();
141  $objectStorage->attach($object1);
142  self::assertEquals($objectStorage->offsetExists($object1), true);
143  self::assertEquals($objectStorage->offsetExists($object2), false);
144  }
145 
150  {
151  $objectStorage = new ‪ObjectStorage();
152  $objectStorage->attach(new \stdClass());
153  self::assertTrue($objectStorage->offsetExists(0));
154  self::assertFalse($objectStorage->offsetExists(1));
155  }
156 
161  {
162  $objectStorage = new ‪ObjectStorage();
163  self::assertEquals($objectStorage->offsetExists(0), false);
164  }
165 
170  {
171  $objectStorage = new ‪ObjectStorage();
172  self::assertEquals($objectStorage->offsetExists('0'), false);
173  }
174 
179  {
180  $objectStorage = new ‪ObjectStorage();
181  $object1 = new \stdClass();
182  $object2 = new \stdClass();
183  $object3 = new \stdClass();
184  $objectStorage->attach($object1, 42);
185  $objectStorage->attach($object2, 'foo');
186  $objectStorage->attach($object3, ['bar', 'baz']);
187  $objectStorage->rewind();
188  self::assertEquals($objectStorage->getInfo(), 42);
189  $objectStorage->next();
190  self::assertEquals($objectStorage->getInfo(), 'foo');
191  $objectStorage->next();
192  self::assertEquals($objectStorage->getInfo(), ['bar', 'baz']);
193  }
194 
199  {
200  $objectStorage = new ‪ObjectStorage();
201  $object1 = new \stdClass();
202  $object2 = new \stdClass();
203  $objectStorage->attach($object1);
204  $objectStorage->attach($object2, 'foo');
205  $objectStorage->rewind();
206  $objectStorage->setInfo(42);
207  $objectStorage->next();
208  $objectStorage->setInfo('bar');
209  self::assertEquals($objectStorage[$object1], 42);
210  self::assertEquals($objectStorage[$object2], 'bar');
211  }
212 
217  {
218  $object1 = new \stdClass();
219  $object2 = new \stdClass();
220  $objectStorageA = new ‪ObjectStorage();
221  $objectStorageA->attach($object1, 'foo');
222  $objectStorageB = new ‪ObjectStorage();
223  $objectStorageB->attach($object1, 'bar');
224  $objectStorageB->attach($object2, 'baz');
225  self::assertEquals(count($objectStorageB), 2);
226  $objectStorageB->removeAll($objectStorageA);
227  self::assertEquals(count($objectStorageB), 1);
228  }
229 
234  {
235  $object1 = new \stdClass();
236  $object2 = new \stdClass();
237  $objectStorageA = new ‪ObjectStorage();
238  // It might be better to mock this
239  $objectStorageA->attach($object1, 'foo');
240  $objectStorageB = new ‪ObjectStorage();
241  $objectStorageB->attach($object2, 'baz');
242  self::assertEquals($objectStorageB->offsetExists($object1), false);
243  $objectStorageB->addAll($objectStorageA);
244  self::assertEquals($objectStorageB[$object1], 'foo');
245  self::assertEquals($objectStorageB[$object2], 'baz');
246  }
247 
252  {
253  $objectStorage = new ‪ObjectStorage();
254  $object1 = new \stdClass();
255  $object2 = new \stdClass();
256  $objectStorage->attach($object1, 'foo');
257  $objectStorage->attach($object2, 'bar');
258  self::assertEquals([$object1, $object2], $objectStorage->toArray());
259  self::assertEquals([$object1, $object2], $objectStorage->getArray());
260  }
261 
266  {
267  $objectStorage = new ‪ObjectStorage();
268  $object1 = new \stdClass();
269  $object2 = new \stdClass();
270  $object3 = new \stdClass();
271  $objectStorage->attach($object1);
272  $objectStorage->attach($object2);
273  $objectStorage->attach($object3);
274  self::assertFalse($objectStorage->isRelationDirty($object1));
275  self::assertFalse($objectStorage->isRelationDirty($object2));
276  self::assertFalse($objectStorage->isRelationDirty($object3));
277  }
278 
283  {
284  $objectStorage = new ‪ObjectStorage();
285  $object1 = new \stdClass();
286  $object2 = new \stdClass();
287  $object3 = new \stdClass();
288  $objectStorage->attach($object1);
289  $objectStorage->attach($object2);
290  $objectStorage->detach($object2);
291  $objectStorage->attach($object3);
292  self::assertFalse($objectStorage->isRelationDirty($object1));
293  self::assertFalse($objectStorage->isRelationDirty($object3));
294  }
295 
300  {
301  $objectStorage = new ‪ObjectStorage();
302  $object1 = new \stdClass();
303  $object2 = new \stdClass();
304  $objectStorage->attach($object1);
305  $objectStorage->attach($object2);
306  $clonedStorage = clone $objectStorage;
307  $objectStorage->removeAll($clonedStorage);
308  $objectStorage->attach($object1);
309  $objectStorage->attach($object2);
310  self::assertFalse($objectStorage->isRelationDirty($object1));
311  self::assertFalse($objectStorage->isRelationDirty($object2));
312  }
313 
318  {
319  $objectStorage = new ‪ObjectStorage();
320  $object1 = new \stdClass();
321  $object2 = new \stdClass();
322  $objectStorage->attach($object1);
323  $objectStorage->attach($object2);
324  $clonedStorage = clone $objectStorage;
325  $objectStorage->removeAll($clonedStorage);
326  $objectStorage->attach($object2);
327  $objectStorage->attach($object1);
328  self::assertTrue($objectStorage->isRelationDirty($object1));
329  self::assertTrue($objectStorage->isRelationDirty($object2));
330  }
331 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreDirtyOnReAddingAtOtherPosition
‪theRelationsAreDirtyOnReAddingAtOtherPosition()
Definition: ObjectStorageTest.php:317
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetKeyRemovesAnObjectFromTheStorage
‪offsetUnsetKeyRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:91
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetSetAssociatesDataToAnObjectInTheStorage
‪offsetSetAssociatesDataToAnObjectInTheStorage()
Definition: ObjectStorageTest.php:60
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreNotDirtyOnReAddingAtSamePosition
‪theRelationsAreNotDirtyOnReAddingAtSamePosition()
Definition: ObjectStorageTest.php:299
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage
‪removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage()
Definition: ObjectStorageTest.php:216
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\addAllAddsAllObjectsFromAnotherStorage
‪addAllAddsAllObjectsFromAnotherStorage()
Definition: ObjectStorageTest.php:233
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetKeyReturnsTheObject
‪offsetGetKeyReturnsTheObject()
Definition: ObjectStorageTest.php:122
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest
Definition: ObjectStorageTest.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndIntegerKey
‪offsetExistsWorksWithEmptyStorageAndIntegerKey()
Definition: ObjectStorageTest.php:160
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttachingAndRemoving
‪allRelationsAreNotDirtyOnAttachingAndRemoving()
Definition: ObjectStorageTest.php:282
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttaching
‪allRelationsAreNotDirtyOnAttaching()
Definition: ObjectStorageTest.php:265
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeDetached
‪anObjectCanBeDetached()
Definition: ObjectStorageTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theStorageCanBeRetrievedAsArray
‪theStorageCanBeRetrievedAsArray()
Definition: ObjectStorageTest.php:251
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetRemovesAnObjectFromTheStorage
‪offsetUnsetRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:74
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry
‪setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:198
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherKeyExistsInTheStorage
‪offsetExistsChecksWhetherKeyExistsInTheStorage()
Definition: ObjectStorageTest.php:149
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeAttached
‪anObjectCanBeAttached()
Definition: ObjectStorageTest.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndStringKey
‪offsetExistsWorksWithEmptyStorageAndStringKey()
Definition: ObjectStorageTest.php:169
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherAnObjectExistsInTheStorage
‪offsetExistsChecksWhetherAnObjectExistsInTheStorage()
Definition: ObjectStorageTest.php:136
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence
Definition: ClassesConfigurationFactoryTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry
‪getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:178
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetReturnsTheDataAssociatedWithAnObject
‪offsetGetReturnsTheDataAssociatedWithAnObject()
Definition: ObjectStorageTest.php:108