‪TYPO3CMS  11.5
ObjectStorageTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪ObjectStorageTest extends UnitTestCase
28 {
33  {
34  $objectStorage = new ‪ObjectStorage();
35 
36  $result = $objectStorage->current();
37 
38  self::assertNull($result);
39  }
40 
45  {
46  $objectStorage = new ‪ObjectStorage();
47 
48  $result = $objectStorage->getInfo();
49 
50  self::assertNull($result);
51  }
52 
56  public function ‪anObjectCanBeAttached(): void
57  {
58  $objectStorage = new ‪ObjectStorage();
59  $object1 = new \stdClass();
60  $object2 = new \stdClass();
61  $objectStorage->attach($object1);
62  $objectStorage->attach($object2, 'foo');
63  self::assertNull($objectStorage[$object1]);
64  self::assertEquals('foo', $objectStorage[$object2]);
65  }
66 
70  public function ‪anObjectCanBeDetached(): void
71  {
72  $objectStorage = new ‪ObjectStorage();
73  $object1 = new \stdClass();
74  $object2 = new \stdClass();
75  $objectStorage->attach($object1);
76  $objectStorage->attach($object2, 'foo');
77  self::assertCount(2, $objectStorage);
78  $objectStorage->detach($object1);
79  self::assertCount(1, $objectStorage);
80  $objectStorage->detach($object2);
81  self::assertCount(0, $objectStorage);
82  }
83 
88  {
89  $objectStorage = new ‪ObjectStorage();
90  $object1 = new \stdClass();
91  $object2 = new \stdClass();
92  $objectStorage->offsetSet($object1, 'foo');
93  self::assertCount(1, $objectStorage);
94  $objectStorage[$object2] = 'bar';
95  self::assertCount(2, $objectStorage);
96  }
97 
102  {
103  $objectStorage = new ‪ObjectStorage();
104  $object1 = new \stdClass();
105  $object2 = new \stdClass();
106  $objectStorage->attach($object1);
107  $objectStorage->attach($object2, 'foo');
108  self::assertCount(2, $objectStorage);
109  $objectStorage->offsetUnset($object2);
110  self::assertCount(1, $objectStorage);
111  $objectStorage->offsetUnset($object1);
112  self::assertCount(0, $objectStorage);
113  }
114 
119  {
120  $objectStorage = new ‪ObjectStorage();
121  $object1 = new \stdClass();
122  $object2 = new \stdClass();
123  $objectStorage->attach($object1);
124  $objectStorage->attach($object2, 'foo');
125  self::assertCount(2, $objectStorage);
126  $objectStorage->offsetUnset(0);
127  self::assertCount(1, $objectStorage);
128  $objectStorage->offsetUnset(0);
129  self::assertCount(0, $objectStorage);
130  }
131 
133  {
135  $objectStorage = new ‪ObjectStorage();
136 
137  self::assertNull($objectStorage->offsetGet(1));
138  }
139 
144  {
146  $objectStorage = new ‪ObjectStorage();
147  $object = new ‪Entity();
148 
149  self::assertNull($objectStorage->offsetGet($object));
150  }
151 
156  {
157  $objectStorage = new ‪ObjectStorage();
158  $object1 = new \stdClass();
159  $object2 = new \stdClass();
160  $objectStorage[$object1] = 'foo';
161  $objectStorage->attach($object2);
162  self::assertEquals('foo', $objectStorage->offsetGet($object1));
163  self::assertNull($objectStorage->offsetGet($object2));
164  }
165 
169  public function ‪offsetGetKeyReturnsTheObject(): void
170  {
171  $objectStorage = new ‪ObjectStorage();
172  $object1 = new \stdClass();
173  $object2 = new \stdClass();
174  $objectStorage->attach($object1);
175  $objectStorage->attach($object2);
176  self::assertSame($object1, $objectStorage->offsetGet(0));
177  self::assertSame($object2, $objectStorage->offsetGet(1));
178  }
179 
184  {
185  $objectStorage = new ‪ObjectStorage();
186  $object1 = new \stdClass();
187  $object2 = new \stdClass();
188  $objectStorage->attach($object1);
189  self::assertTrue($objectStorage->offsetExists($object1));
190  self::assertFalse($objectStorage->offsetExists($object2));
191  }
192 
197  {
198  $objectStorage = new ‪ObjectStorage();
199  $objectStorage->attach(new \stdClass());
200  self::assertTrue($objectStorage->offsetExists(0));
201  self::assertFalse($objectStorage->offsetExists(1));
202  }
203 
208  {
209  $objectStorage = new ‪ObjectStorage();
210  self::assertFalse($objectStorage->offsetExists(0));
211  }
212 
217  {
218  $objectStorage = new ‪ObjectStorage();
219  self::assertFalse($objectStorage->offsetExists('0'));
220  }
221 
226  {
227  $objectStorage = new ‪ObjectStorage();
228  $object1 = new \stdClass();
229  $object2 = new \stdClass();
230  $object3 = new \stdClass();
231  $objectStorage->attach($object1, 42);
232  $objectStorage->attach($object2, 'foo');
233  $objectStorage->attach($object3, ['bar', 'baz']);
234  $objectStorage->rewind();
235  self::assertEquals(42, $objectStorage->getInfo());
236  $objectStorage->next();
237  self::assertEquals('foo', $objectStorage->getInfo());
238  $objectStorage->next();
239  self::assertEquals(['bar', 'baz'], $objectStorage->getInfo());
240  }
241 
246  {
247  $objectStorage = new ‪ObjectStorage();
248  $object1 = new \stdClass();
249  $object2 = new \stdClass();
250  $objectStorage->attach($object1);
251  $objectStorage->attach($object2, 'foo');
252  $objectStorage->rewind();
253  $objectStorage->setInfo(42);
254  $objectStorage->next();
255  $objectStorage->setInfo('bar');
256  self::assertEquals(42, $objectStorage[$object1]);
257  self::assertEquals('bar', $objectStorage[$object2]);
258  }
259 
264  {
265  $object1 = new \stdClass();
266  $object2 = new \stdClass();
267  $objectStorageA = new ‪ObjectStorage();
268  $objectStorageA->attach($object1, 'foo');
269  $objectStorageB = new ‪ObjectStorage();
270  $objectStorageB->attach($object1, 'bar');
271  $objectStorageB->attach($object2, 'baz');
272  self::assertCount(2, $objectStorageB);
273  $objectStorageB->removeAll($objectStorageA);
274  self::assertCount(1, $objectStorageB);
275  }
276 
281  {
282  $object1 = new \stdClass();
283  $object2 = new \stdClass();
284  $objectStorageA = new ‪ObjectStorage();
285  // It might be better to mock this
286  $objectStorageA->attach($object1, 'foo');
287  $objectStorageB = new ‪ObjectStorage();
288  $objectStorageB->attach($object2, 'baz');
289  self::assertFalse($objectStorageB->offsetExists($object1));
290  $objectStorageB->addAll($objectStorageA);
291  self::assertEquals('foo', $objectStorageB[$object1]);
292  self::assertEquals('baz', $objectStorageB[$object2]);
293  }
294 
298  public function ‪theStorageCanBeRetrievedAsArray(): void
299  {
300  $objectStorage = new ‪ObjectStorage();
301  $object1 = new \stdClass();
302  $object2 = new \stdClass();
303  $objectStorage->attach($object1, 'foo');
304  $objectStorage->attach($object2, 'bar');
305  self::assertEquals([$object1, $object2], $objectStorage->toArray());
306  self::assertEquals([$object1, $object2], $objectStorage->getArray());
307  }
308 
313  {
314  $objectStorage = new ‪ObjectStorage();
315  $object1 = new \stdClass();
316  $object2 = new \stdClass();
317  $object3 = new \stdClass();
318  $objectStorage->attach($object1);
319  $objectStorage->attach($object2);
320  $objectStorage->attach($object3);
321  self::assertFalse($objectStorage->isRelationDirty($object1));
322  self::assertFalse($objectStorage->isRelationDirty($object2));
323  self::assertFalse($objectStorage->isRelationDirty($object3));
324  }
325 
330  {
331  $objectStorage = new ‪ObjectStorage();
332  $object1 = new \stdClass();
333  $object2 = new \stdClass();
334  $object3 = new \stdClass();
335  $objectStorage->attach($object1);
336  $objectStorage->attach($object2);
337  $objectStorage->detach($object2);
338  $objectStorage->attach($object3);
339  self::assertFalse($objectStorage->isRelationDirty($object1));
340  self::assertFalse($objectStorage->isRelationDirty($object3));
341  }
342 
347  {
348  $objectStorage = new ‪ObjectStorage();
349  $object1 = new \stdClass();
350  $object2 = new \stdClass();
351  $objectStorage->attach($object1);
352  $objectStorage->attach($object2);
353  $clonedStorage = clone $objectStorage;
354  $objectStorage->removeAll($clonedStorage);
355  $objectStorage->attach($object1);
356  $objectStorage->attach($object2);
357  self::assertFalse($objectStorage->isRelationDirty($object1));
358  self::assertFalse($objectStorage->isRelationDirty($object2));
359  }
360 
365  {
366  $objectStorage = new ‪ObjectStorage();
367  $object1 = new \stdClass();
368  $object2 = new \stdClass();
369  $objectStorage->attach($object1);
370  $objectStorage->attach($object2);
371  $clonedStorage = clone $objectStorage;
372  $objectStorage->removeAll($clonedStorage);
373  $objectStorage->attach($object2);
374  $objectStorage->attach($object1);
375  self::assertTrue($objectStorage->isRelationDirty($object1));
376  self::assertTrue($objectStorage->isRelationDirty($object2));
377  }
378 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\currentForAnEmptyStorageReturnsNull
‪currentForAnEmptyStorageReturnsNull()
Definition: ObjectStorageTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreDirtyOnReAddingAtOtherPosition
‪theRelationsAreDirtyOnReAddingAtOtherPosition()
Definition: ObjectStorageTest.php:364
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetKeyRemovesAnObjectFromTheStorage
‪offsetUnsetKeyRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:118
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetSetAssociatesDataToAnObjectInTheStorage
‪offsetSetAssociatesDataToAnObjectInTheStorage()
Definition: ObjectStorageTest.php:87
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreNotDirtyOnReAddingAtSamePosition
‪theRelationsAreNotDirtyOnReAddingAtSamePosition()
Definition: ObjectStorageTest.php:346
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage
‪removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage()
Definition: ObjectStorageTest.php:263
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\addAllAddsAllObjectsFromAnotherStorage
‪addAllAddsAllObjectsFromAnotherStorage()
Definition: ObjectStorageTest.php:280
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetKeyReturnsTheObject
‪offsetGetKeyReturnsTheObject()
Definition: ObjectStorageTest.php:169
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest
Definition: ObjectStorageTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndIntegerKey
‪offsetExistsWorksWithEmptyStorageAndIntegerKey()
Definition: ObjectStorageTest.php:207
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttachingAndRemoving
‪allRelationsAreNotDirtyOnAttachingAndRemoving()
Definition: ObjectStorageTest.php:329
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetForNoneExistingIntegerEntryReturnsNull
‪offsetGetForNoneExistingIntegerEntryReturnsNull()
Definition: ObjectStorageTest.php:132
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttaching
‪allRelationsAreNotDirtyOnAttaching()
Definition: ObjectStorageTest.php:312
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetForNoneExistingObjectEntryReturnsNull
‪offsetGetForNoneExistingObjectEntryReturnsNull()
Definition: ObjectStorageTest.php:143
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeDetached
‪anObjectCanBeDetached()
Definition: ObjectStorageTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theStorageCanBeRetrievedAsArray
‪theStorageCanBeRetrievedAsArray()
Definition: ObjectStorageTest.php:298
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetRemovesAnObjectFromTheStorage
‪offsetUnsetRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:101
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry
‪setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:245
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherKeyExistsInTheStorage
‪offsetExistsChecksWhetherKeyExistsInTheStorage()
Definition: ObjectStorageTest.php:196
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeAttached
‪anObjectCanBeAttached()
Definition: ObjectStorageTest.php:56
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndStringKey
‪offsetExistsWorksWithEmptyStorageAndStringKey()
Definition: ObjectStorageTest.php:216
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherAnObjectExistsInTheStorage
‪offsetExistsChecksWhetherAnObjectExistsInTheStorage()
Definition: ObjectStorageTest.php:183
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence
Definition: ClassesConfigurationFactoryTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry
‪getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:225
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetReturnsTheDataAssociatedWithAnObject
‪offsetGetReturnsTheDataAssociatedWithAnObject()
Definition: ObjectStorageTest.php:155
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\getInfoForAnEmptyStorageReturnsNull
‪getInfoForAnEmptyStorageReturnsNull()
Definition: ObjectStorageTest.php:44
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Domain\Model\Entity
Definition: Entity.php:22