TYPO3 CMS  TYPO3_6-2
ObjectStorageTest.php
Go to the documentation of this file.
1 <?php
3 
21 
25  public function anObjectCanBeAttached() {
26  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
27  $object1 = new \StdClass();
28  $object2 = new \StdClass();
29  $objectStorage->attach($object1);
30  $objectStorage->attach($object2, 'foo');
31  $this->assertEquals($objectStorage[$object1], NULL);
32  $this->assertEquals($objectStorage[$object2], 'foo');
33  }
34 
38  public function anObjectCanBeDetached() {
39  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
40  $object1 = new \StdClass();
41  $object2 = new \StdClass();
42  $objectStorage->attach($object1);
43  $objectStorage->attach($object2, 'foo');
44  $this->assertEquals(count($objectStorage), 2);
45  $objectStorage->detach($object1);
46  $this->assertEquals(count($objectStorage), 1);
47  $objectStorage->detach($object2);
48  $this->assertEquals(count($objectStorage), 0);
49  }
50 
55  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
56  $object1 = new \StdClass();
57  $object2 = new \StdClass();
58  $objectStorage->offsetSet($object1, 'foo');
59  $this->assertEquals(count($objectStorage), 1);
60  $objectStorage[$object2] = 'bar';
61  $this->assertEquals(count($objectStorage), 2);
62  }
63 
68  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
69  $object1 = new \StdClass();
70  $object2 = new \StdClass();
71  $objectStorage->attach($object1);
72  $objectStorage->attach($object2, 'foo');
73  $this->assertEquals(count($objectStorage), 2);
74  $objectStorage->offsetUnset($object2);
75  $this->assertEquals(count($objectStorage), 1);
76  $objectStorage->offsetUnset($object1);
77  $this->assertEquals(count($objectStorage), 0);
78  }
79 
84  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
85  $object1 = new \StdClass();
86  $object2 = new \StdClass();
87  $objectStorage[$object1] = 'foo';
88  $objectStorage->attach($object2);
89  $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
90  $this->assertEquals($objectStorage->offsetGet($object2), NULL);
91  }
92 
97  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
98  $object1 = new \StdClass();
99  $object2 = new \StdClass();
100  $objectStorage->attach($object1);
101  $this->assertEquals($objectStorage->offsetExists($object1), TRUE);
102  $this->assertEquals($objectStorage->offsetExists($object2), FALSE);
103  }
104 
109  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
110  $this->assertEquals($objectStorage->offsetExists(0), FALSE);
111  }
112 
117  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
118  $this->assertEquals($objectStorage->offsetExists('0'), FALSE);
119  }
120 
125  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
126  $object1 = new \StdClass();
127  $object2 = new \StdClass();
128  $object3 = new \StdClass();
129  $objectStorage->attach($object1, 42);
130  $objectStorage->attach($object2, 'foo');
131  $objectStorage->attach($object3, array('bar', 'baz'));
132  $objectStorage->rewind();
133  $this->assertEquals($objectStorage->getInfo(), 42);
134  $objectStorage->next();
135  $this->assertEquals($objectStorage->getInfo(), 'foo');
136  $objectStorage->next();
137  $this->assertEquals($objectStorage->getInfo(), array('bar', 'baz'));
138  }
139 
144  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
145  $object1 = new \StdClass();
146  $object2 = new \StdClass();
147  $objectStorage->attach($object1);
148  $objectStorage->attach($object2, 'foo');
149  $objectStorage->rewind();
150  $objectStorage->setInfo(42);
151  $objectStorage->next();
152  $objectStorage->setInfo('bar');
153  $this->assertEquals($objectStorage[$object1], 42);
154  $this->assertEquals($objectStorage[$object2], 'bar');
155  }
156 
161  $object1 = new \StdClass();
162  $object2 = new \StdClass();
163  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
164  $objectStorageA->attach($object1, 'foo');
165  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
166  $objectStorageB->attach($object1, 'bar');
167  $objectStorageB->attach($object2, 'baz');
168  $this->assertEquals(count($objectStorageB), 2);
169  $objectStorageB->removeAll($objectStorageA);
170  $this->assertEquals(count($objectStorageB), 1);
171  }
172 
177  $object1 = new \StdClass();
178  $object2 = new \StdClass();
179  $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
180  // It might be better to mock this
181  $objectStorageA->attach($object1, 'foo');
182  $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
183  $objectStorageB->attach($object2, 'baz');
184  $this->assertEquals($objectStorageB->offsetExists($object1), FALSE);
185  $objectStorageB->addAll($objectStorageA);
186  $this->assertEquals($objectStorageB[$object1], 'foo');
187  $this->assertEquals($objectStorageB[$object2], 'baz');
188  }
189 
194  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
195  $object1 = new \StdClass();
196  $object2 = new \StdClass();
197  $objectStorage->attach($object1, 'foo');
198  $objectStorage->attach($object2, 'bar');
199  $this->assertEquals($objectStorage->toArray(), array($object1, $object2));
200  }
201 
206  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
207  $object1 = new \StdClass();
208  $object2 = new \StdClass();
209  $object3 = new \StdClass();
210  $objectStorage->attach($object1);
211  $objectStorage->attach($object2);
212  $objectStorage->attach($object3);
213  $this->assertFalse($objectStorage->isRelationDirty($object1));
214  $this->assertFalse($objectStorage->isRelationDirty($object2));
215  $this->assertFalse($objectStorage->isRelationDirty($object3));
216  }
217 
222  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
223  $object1 = new \StdClass;
224  $object2 = new \StdClass;
225  $object3 = new \StdClass;
226  $objectStorage->attach($object1);
227  $objectStorage->attach($object2);
228  $objectStorage->detach($object2);
229  $objectStorage->attach($object3);
230  $this->assertFalse($objectStorage->isRelationDirty($object1));
231  $this->assertFalse($objectStorage->isRelationDirty($object3));
232  }
233 
238  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
239  $object1 = new \StdClass;
240  $object2 = new \StdClass;
241  $objectStorage->attach($object1);
242  $objectStorage->attach($object2);
243  $clonedStorage = clone $objectStorage;
244  $objectStorage->removeAll($clonedStorage);
245  $objectStorage->attach($object1);
246  $objectStorage->attach($object2);
247  $this->assertFalse($objectStorage->isRelationDirty($object1));
248  $this->assertFalse($objectStorage->isRelationDirty($object2));
249  }
250 
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($object2);
263  $objectStorage->attach($object1);
264  $this->assertTrue($objectStorage->isRelationDirty($object1));
265  $this->assertTrue($objectStorage->isRelationDirty($object2));
266  }
267 }