TYPO3 CMS  TYPO3_8-7
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 
20 class ObjectStorageTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
25  public function anObjectCanBeAttached()
26  {
27  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
28  $object1 = new \stdClass();
29  $object2 = new \stdClass();
30  $objectStorage->attach($object1);
31  $objectStorage->attach($object2, 'foo');
32  $this->assertEquals($objectStorage[$object1], null);
33  $this->assertEquals($objectStorage[$object2], 'foo');
34  }
35 
39  public function anObjectCanBeDetached()
40  {
41  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
42  $object1 = new \stdClass();
43  $object2 = new \stdClass();
44  $objectStorage->attach($object1);
45  $objectStorage->attach($object2, 'foo');
46  $this->assertEquals(count($objectStorage), 2);
47  $objectStorage->detach($object1);
48  $this->assertEquals(count($objectStorage), 1);
49  $objectStorage->detach($object2);
50  $this->assertEquals(count($objectStorage), 0);
51  }
52 
57  {
58  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
59  $object1 = new \stdClass();
60  $object2 = new \stdClass();
61  $objectStorage->offsetSet($object1, 'foo');
62  $this->assertEquals(count($objectStorage), 1);
63  $objectStorage[$object2] = 'bar';
64  $this->assertEquals(count($objectStorage), 2);
65  }
66 
71  {
72  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
73  $object1 = new \stdClass();
74  $object2 = new \stdClass();
75  $objectStorage->attach($object1);
76  $objectStorage->attach($object2, 'foo');
77  $this->assertEquals(count($objectStorage), 2);
78  $objectStorage->offsetUnset($object2);
79  $this->assertEquals(count($objectStorage), 1);
80  $objectStorage->offsetUnset($object1);
81  $this->assertEquals(count($objectStorage), 0);
82  }
83 
88  {
89  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
90  $object1 = new \stdClass();
91  $object2 = new \stdClass();
92  $objectStorage[$object1] = 'foo';
93  $objectStorage->attach($object2);
94  $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
95  $this->assertEquals($objectStorage->offsetGet($object2), null);
96  }
97 
102  {
103  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
104  $object1 = new \stdClass();
105  $object2 = new \stdClass();
106  $objectStorage->attach($object1);
107  $this->assertEquals($objectStorage->offsetExists($object1), true);
108  $this->assertEquals($objectStorage->offsetExists($object2), false);
109  }
110 
115  {
116  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
117  $this->assertEquals($objectStorage->offsetExists(0), false);
118  }
119 
124  {
125  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
126  $this->assertEquals($objectStorage->offsetExists('0'), false);
127  }
128 
133  {
134  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
135  $object1 = new \stdClass();
136  $object2 = new \stdClass();
137  $object3 = new \stdClass();
138  $objectStorage->attach($object1, 42);
139  $objectStorage->attach($object2, 'foo');
140  $objectStorage->attach($object3, ['bar', 'baz']);
141  $objectStorage->rewind();
142  $this->assertEquals($objectStorage->getInfo(), 42);
143  $objectStorage->next();
144  $this->assertEquals($objectStorage->getInfo(), 'foo');
145  $objectStorage->next();
146  $this->assertEquals($objectStorage->getInfo(), ['bar', 'baz']);
147  }
148 
153  {
154  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
155  $object1 = new \stdClass();
156  $object2 = new \stdClass();
157  $objectStorage->attach($object1);
158  $objectStorage->attach($object2, 'foo');
159  $objectStorage->rewind();
160  $objectStorage->setInfo(42);
161  $objectStorage->next();
162  $objectStorage->setInfo('bar');
163  $this->assertEquals($objectStorage[$object1], 42);
164  $this->assertEquals($objectStorage[$object2], 'bar');
165  }
166 
171  {
172  $object1 = new \stdClass();
173  $object2 = new \stdClass();
174  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
175  $objectStorageA->attach($object1, 'foo');
176  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
177  $objectStorageB->attach($object1, 'bar');
178  $objectStorageB->attach($object2, 'baz');
179  $this->assertEquals(count($objectStorageB), 2);
180  $objectStorageB->removeAll($objectStorageA);
181  $this->assertEquals(count($objectStorageB), 1);
182  }
183 
188  {
189  $object1 = new \stdClass();
190  $object2 = new \stdClass();
191  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
192  // It might be better to mock this
193  $objectStorageA->attach($object1, 'foo');
194  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
195  $objectStorageB->attach($object2, 'baz');
196  $this->assertEquals($objectStorageB->offsetExists($object1), false);
197  $objectStorageB->addAll($objectStorageA);
198  $this->assertEquals($objectStorageB[$object1], 'foo');
199  $this->assertEquals($objectStorageB[$object2], 'baz');
200  }
201 
206  {
207  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
208  $object1 = new \stdClass();
209  $object2 = new \stdClass();
210  $objectStorage->attach($object1, 'foo');
211  $objectStorage->attach($object2, 'bar');
212  $this->assertEquals([$object1, $object2], $objectStorage->toArray());
213  $this->assertEquals([$object1, $object2], $objectStorage->getArray());
214  }
215 
220  {
221  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
222  $object1 = new \stdClass();
223  $object2 = new \stdClass();
224  $object3 = new \stdClass();
225  $objectStorage->attach($object1);
226  $objectStorage->attach($object2);
227  $objectStorage->attach($object3);
228  $this->assertFalse($objectStorage->isRelationDirty($object1));
229  $this->assertFalse($objectStorage->isRelationDirty($object2));
230  $this->assertFalse($objectStorage->isRelationDirty($object3));
231  }
232 
237  {
238  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
239  $object1 = new \stdClass;
240  $object2 = new \stdClass;
241  $object3 = new \stdClass;
242  $objectStorage->attach($object1);
243  $objectStorage->attach($object2);
244  $objectStorage->detach($object2);
245  $objectStorage->attach($object3);
246  $this->assertFalse($objectStorage->isRelationDirty($object1));
247  $this->assertFalse($objectStorage->isRelationDirty($object3));
248  }
249 
254  {
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($object1);
263  $objectStorage->attach($object2);
264  $this->assertFalse($objectStorage->isRelationDirty($object1));
265  $this->assertFalse($objectStorage->isRelationDirty($object2));
266  }
267 
272  {
273  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
274  $object1 = new \stdClass;
275  $object2 = new \stdClass;
276  $objectStorage->attach($object1);
277  $objectStorage->attach($object2);
278  $clonedStorage = clone $objectStorage;
279  $objectStorage->removeAll($clonedStorage);
280  $objectStorage->attach($object2);
281  $objectStorage->attach($object1);
282  $this->assertTrue($objectStorage->isRelationDirty($object1));
283  $this->assertTrue($objectStorage->isRelationDirty($object2));
284  }
285 }