‪TYPO3CMS  9.5
ObjectStorageTest.php
Go to the documentation of this file.
1 <?php
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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪ObjectStorageTest extends UnitTestCase
23 {
27  public function ‪anObjectCanBeAttached()
28  {
29  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
30  $object1 = new \stdClass();
31  $object2 = new \stdClass();
32  $objectStorage->attach($object1);
33  $objectStorage->attach($object2, 'foo');
34  $this->assertEquals($objectStorage[$object1], null);
35  $this->assertEquals($objectStorage[$object2], 'foo');
36  }
37 
41  public function ‪anObjectCanBeDetached()
42  {
43  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
44  $object1 = new \stdClass();
45  $object2 = new \stdClass();
46  $objectStorage->attach($object1);
47  $objectStorage->attach($object2, 'foo');
48  $this->assertEquals(count($objectStorage), 2);
49  $objectStorage->detach($object1);
50  $this->assertEquals(count($objectStorage), 1);
51  $objectStorage->detach($object2);
52  $this->assertEquals(count($objectStorage), 0);
53  }
54 
59  {
60  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
61  $object1 = new \stdClass();
62  $object2 = new \stdClass();
63  $objectStorage->offsetSet($object1, 'foo');
64  $this->assertEquals(count($objectStorage), 1);
65  $objectStorage[$object2] = 'bar';
66  $this->assertEquals(count($objectStorage), 2);
67  }
68 
73  {
74  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
75  $object1 = new \stdClass();
76  $object2 = new \stdClass();
77  $objectStorage->attach($object1);
78  $objectStorage->attach($object2, 'foo');
79  $this->assertEquals(count($objectStorage), 2);
80  $objectStorage->offsetUnset($object2);
81  $this->assertEquals(count($objectStorage), 1);
82  $objectStorage->offsetUnset($object1);
83  $this->assertEquals(count($objectStorage), 0);
84  }
85 
90  {
91  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
92  $object1 = new \stdClass();
93  $object2 = new \stdClass();
94  $objectStorage->attach($object1);
95  $objectStorage->attach($object2, 'foo');
96  $this->assertEquals(count($objectStorage), 2);
97  $objectStorage->offsetUnset(0);
98  $this->assertEquals(count($objectStorage), 1);
99  $objectStorage->offsetUnset(0);
100  $this->assertEquals(count($objectStorage), 0);
101  }
102 
107  {
108  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
109  $object1 = new \stdClass();
110  $object2 = new \stdClass();
111  $objectStorage[$object1] = 'foo';
112  $objectStorage->attach($object2);
113  $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
114  $this->assertEquals($objectStorage->offsetGet($object2), null);
115  }
116 
121  {
122  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
123  $object1 = new \stdClass();
124  $object2 = new \stdClass();
125  $objectStorage->attach($object1);
126  $objectStorage->attach($object2);
127  $this->assertSame($object1, $objectStorage->offsetGet(0));
128  $this->assertSame($object2, $objectStorage->offsetGet(1));
129  }
130 
135  {
136  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
137  $object1 = new \stdClass();
138  $object2 = new \stdClass();
139  $objectStorage->attach($object1);
140  $this->assertEquals($objectStorage->offsetExists($object1), true);
141  $this->assertEquals($objectStorage->offsetExists($object2), false);
142  }
143 
148  {
149  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
150  $objectStorage->attach(new \stdClass());
151  $this->assertTrue($objectStorage->offsetExists(0));
152  $this->assertFalse($objectStorage->offsetExists(1));
153  }
154 
159  {
160  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
161  $this->assertEquals($objectStorage->offsetExists(0), false);
162  }
163 
168  {
169  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
170  $this->assertEquals($objectStorage->offsetExists('0'), false);
171  }
172 
177  {
178  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
179  $object1 = new \stdClass();
180  $object2 = new \stdClass();
181  $object3 = new \stdClass();
182  $objectStorage->attach($object1, 42);
183  $objectStorage->attach($object2, 'foo');
184  $objectStorage->attach($object3, ['bar', 'baz']);
185  $objectStorage->rewind();
186  $this->assertEquals($objectStorage->getInfo(), 42);
187  $objectStorage->next();
188  $this->assertEquals($objectStorage->getInfo(), 'foo');
189  $objectStorage->next();
190  $this->assertEquals($objectStorage->getInfo(), ['bar', 'baz']);
191  }
192 
197  {
198  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
199  $object1 = new \stdClass();
200  $object2 = new \stdClass();
201  $objectStorage->attach($object1);
202  $objectStorage->attach($object2, 'foo');
203  $objectStorage->rewind();
204  $objectStorage->setInfo(42);
205  $objectStorage->next();
206  $objectStorage->setInfo('bar');
207  $this->assertEquals($objectStorage[$object1], 42);
208  $this->assertEquals($objectStorage[$object2], 'bar');
209  }
210 
215  {
216  $object1 = new \stdClass();
217  $object2 = new \stdClass();
218  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
219  $objectStorageA->attach($object1, 'foo');
220  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
221  $objectStorageB->attach($object1, 'bar');
222  $objectStorageB->attach($object2, 'baz');
223  $this->assertEquals(count($objectStorageB), 2);
224  $objectStorageB->removeAll($objectStorageA);
225  $this->assertEquals(count($objectStorageB), 1);
226  }
227 
232  {
233  $object1 = new \stdClass();
234  $object2 = new \stdClass();
235  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
236  // It might be better to mock this
237  $objectStorageA->attach($object1, 'foo');
238  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
239  $objectStorageB->attach($object2, 'baz');
240  $this->assertEquals($objectStorageB->offsetExists($object1), false);
241  $objectStorageB->addAll($objectStorageA);
242  $this->assertEquals($objectStorageB[$object1], 'foo');
243  $this->assertEquals($objectStorageB[$object2], 'baz');
244  }
245 
250  {
251  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
252  $object1 = new \stdClass();
253  $object2 = new \stdClass();
254  $objectStorage->attach($object1, 'foo');
255  $objectStorage->attach($object2, 'bar');
256  $this->assertEquals([$object1, $object2], $objectStorage->toArray());
257  $this->assertEquals([$object1, $object2], $objectStorage->getArray());
258  }
259 
264  {
265  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
266  $object1 = new \stdClass();
267  $object2 = new \stdClass();
268  $object3 = new \stdClass();
269  $objectStorage->attach($object1);
270  $objectStorage->attach($object2);
271  $objectStorage->attach($object3);
272  $this->assertFalse($objectStorage->isRelationDirty($object1));
273  $this->assertFalse($objectStorage->isRelationDirty($object2));
274  $this->assertFalse($objectStorage->isRelationDirty($object3));
275  }
276 
281  {
282  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
283  $object1 = new \stdClass;
284  $object2 = new \stdClass;
285  $object3 = new \stdClass;
286  $objectStorage->attach($object1);
287  $objectStorage->attach($object2);
288  $objectStorage->detach($object2);
289  $objectStorage->attach($object3);
290  $this->assertFalse($objectStorage->isRelationDirty($object1));
291  $this->assertFalse($objectStorage->isRelationDirty($object3));
292  }
293 
298  {
299  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
300  $object1 = new \stdClass;
301  $object2 = new \stdClass;
302  $objectStorage->attach($object1);
303  $objectStorage->attach($object2);
304  $clonedStorage = clone $objectStorage;
305  $objectStorage->removeAll($clonedStorage);
306  $objectStorage->attach($object1);
307  $objectStorage->attach($object2);
308  $this->assertFalse($objectStorage->isRelationDirty($object1));
309  $this->assertFalse($objectStorage->isRelationDirty($object2));
310  }
311 
316  {
317  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
318  $object1 = new \stdClass;
319  $object2 = new \stdClass;
320  $objectStorage->attach($object1);
321  $objectStorage->attach($object2);
322  $clonedStorage = clone $objectStorage;
323  $objectStorage->removeAll($clonedStorage);
324  $objectStorage->attach($object2);
325  $objectStorage->attach($object1);
326  $this->assertTrue($objectStorage->isRelationDirty($object1));
327  $this->assertTrue($objectStorage->isRelationDirty($object2));
328  }
329 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreDirtyOnReAddingAtOtherPosition
‪theRelationsAreDirtyOnReAddingAtOtherPosition()
Definition: ObjectStorageTest.php:315
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetKeyRemovesAnObjectFromTheStorage
‪offsetUnsetKeyRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:89
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetSetAssociatesDataToAnObjectInTheStorage
‪offsetSetAssociatesDataToAnObjectInTheStorage()
Definition: ObjectStorageTest.php:58
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theRelationsAreNotDirtyOnReAddingAtSamePosition
‪theRelationsAreNotDirtyOnReAddingAtSamePosition()
Definition: ObjectStorageTest.php:297
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage
‪removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage()
Definition: ObjectStorageTest.php:214
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\addAllAddsAllObjectsFromAnotherStorage
‪addAllAddsAllObjectsFromAnotherStorage()
Definition: ObjectStorageTest.php:231
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetKeyReturnsTheObject
‪offsetGetKeyReturnsTheObject()
Definition: ObjectStorageTest.php:120
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest
Definition: ObjectStorageTest.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndIntegerKey
‪offsetExistsWorksWithEmptyStorageAndIntegerKey()
Definition: ObjectStorageTest.php:158
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttachingAndRemoving
‪allRelationsAreNotDirtyOnAttachingAndRemoving()
Definition: ObjectStorageTest.php:280
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\allRelationsAreNotDirtyOnAttaching
‪allRelationsAreNotDirtyOnAttaching()
Definition: ObjectStorageTest.php:263
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeDetached
‪anObjectCanBeDetached()
Definition: ObjectStorageTest.php:41
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\theStorageCanBeRetrievedAsArray
‪theStorageCanBeRetrievedAsArray()
Definition: ObjectStorageTest.php:249
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetUnsetRemovesAnObjectFromTheStorage
‪offsetUnsetRemovesAnObjectFromTheStorage()
Definition: ObjectStorageTest.php:72
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry
‪setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:196
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherKeyExistsInTheStorage
‪offsetExistsChecksWhetherKeyExistsInTheStorage()
Definition: ObjectStorageTest.php:147
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\anObjectCanBeAttached
‪anObjectCanBeAttached()
Definition: ObjectStorageTest.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsWorksWithEmptyStorageAndStringKey
‪offsetExistsWorksWithEmptyStorageAndStringKey()
Definition: ObjectStorageTest.php:167
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetExistsChecksWhetherAnObjectExistsInTheStorage
‪offsetExistsChecksWhetherAnObjectExistsInTheStorage()
Definition: ObjectStorageTest.php:134
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry
‪getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry()
Definition: ObjectStorageTest.php:176
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\ObjectStorageTest\offsetGetReturnsTheDataAssociatedWithAnObject
‪offsetGetReturnsTheDataAssociatedWithAnObject()
Definition: ObjectStorageTest.php:106