TYPO3 CMS  TYPO3_7-6
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 
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($objectStorage->toArray(), [$object1, $object2]);
213  }
214 
219  {
220  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
221  $object1 = new \StdClass();
222  $object2 = new \StdClass();
223  $object3 = new \StdClass();
224  $objectStorage->attach($object1);
225  $objectStorage->attach($object2);
226  $objectStorage->attach($object3);
227  $this->assertFalse($objectStorage->isRelationDirty($object1));
228  $this->assertFalse($objectStorage->isRelationDirty($object2));
229  $this->assertFalse($objectStorage->isRelationDirty($object3));
230  }
231 
236  {
237  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
238  $object1 = new \StdClass;
239  $object2 = new \StdClass;
240  $object3 = new \StdClass;
241  $objectStorage->attach($object1);
242  $objectStorage->attach($object2);
243  $objectStorage->detach($object2);
244  $objectStorage->attach($object3);
245  $this->assertFalse($objectStorage->isRelationDirty($object1));
246  $this->assertFalse($objectStorage->isRelationDirty($object3));
247  }
248 
253  {
254  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
255  $object1 = new \StdClass;
256  $object2 = new \StdClass;
257  $objectStorage->attach($object1);
258  $objectStorage->attach($object2);
259  $clonedStorage = clone $objectStorage;
260  $objectStorage->removeAll($clonedStorage);
261  $objectStorage->attach($object1);
262  $objectStorage->attach($object2);
263  $this->assertFalse($objectStorage->isRelationDirty($object1));
264  $this->assertFalse($objectStorage->isRelationDirty($object2));
265  }
266 
271  {
272  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
273  $object1 = new \StdClass;
274  $object2 = new \StdClass;
275  $objectStorage->attach($object1);
276  $objectStorage->attach($object2);
277  $clonedStorage = clone $objectStorage;
278  $objectStorage->removeAll($clonedStorage);
279  $objectStorage->attach($object2);
280  $objectStorage->attach($object1);
281  $this->assertTrue($objectStorage->isRelationDirty($object1));
282  $this->assertTrue($objectStorage->isRelationDirty($object2));
283  }
284 }