b55e1e7dfcc898fc30a729a15c1cb0ccd215aedc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / PersistenceManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Nico de Haen
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 require_once __DIR__ . '/../Fixture/Model/Entity3.php';
31 require_once __DIR__ . '/../Fixture/Model/Entity2.php';
32
33 /**
34 * A PersistenceManager Test
35 */
36 class PersistenceManagerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
37
38 /**
39 * @test
40 */
41 public function persistAllPassesAddedObjectsToBackend() {
42 $entity2 = new \TYPO3\CMS\Extbase\Tests\Persistence\Fixture\Model\Entity2();
43 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
44 $objectStorage->attach($entity2);
45 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
46 $mockBackend->expects($this->once())->method('setAggregateRootObjects')->with($objectStorage);
47
48 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
49 $manager->injectBackend($mockBackend);
50 $manager->add($entity2);
51
52 $manager->persistAll();
53 }
54
55 /**
56 * @test
57 */
58 public function persistAllPassesRemovedObjectsToBackend() {
59 $entity2 = new \TYPO3\CMS\Extbase\Tests\Persistence\Fixture\Model\Entity2();
60 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
61 $objectStorage->attach($entity2);
62 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
63 $mockBackend->expects($this->once())->method('setDeletedEntities')->with($objectStorage);
64
65 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
66 $manager->injectBackend($mockBackend);
67 $manager->remove($entity2);
68
69 $manager->persistAll();
70 }
71
72 /**
73 * @test
74 */
75 public function getIdentifierByObjectReturnsIdentifierFromSession() {
76 $fakeUuid = 'fakeUuid';
77 $object = new \stdClass();
78
79 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
80 $mockSession->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
81
82 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
83 $manager->injectPersistenceSession($mockSession);
84
85 $this->assertEquals($manager->getIdentifierByObject($object), $fakeUuid);
86 }
87
88 /**
89 * @test
90 */
91 public function getObjectByIdentifierReturnsObjectFromSessionIfAvailable() {
92 $fakeUuid = 'fakeUuid';
93 $object = new \stdClass();
94
95 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
96 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(TRUE));
97 $mockSession->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid)->will($this->returnValue($object));
98
99 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
100 $manager->injectPersistenceSession($mockSession);
101
102 $this->assertEquals($manager->getObjectByIdentifier($fakeUuid), $object);
103 }
104
105 /**
106 * @test
107 */
108 public function getObjectByIdentifierReturnsObjectFromPersistenceIfAvailable() {
109 $fakeUuid = '42';
110 $object = new \stdClass();
111 $fakeEntityType = get_class($object);
112
113 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
114 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(FALSE));
115
116 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
117 $mockBackend->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue($object));
118
119 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
120 $manager->injectPersistenceSession($mockSession);
121 $manager->injectBackend($mockBackend);
122
123 $this->assertEquals($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType), $object);
124 }
125
126 /**
127 * @test
128 */
129 public function getObjectByIdentifierReturnsNullForUnknownObject() {
130 $fakeUuid = '42';
131 $fakeEntityType = 'foobar';
132
133 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
134 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue(FALSE));
135
136 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
137 $mockBackend->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue(NULL));
138
139 $manager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
140 $manager->injectPersistenceSession($mockSession);
141 $manager->injectBackend($mockBackend);
142
143 $this->assertNull($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType));
144 }
145
146 /**
147 * @test
148 */
149 public function addActuallyAddsAnObjectToTheInternalObjectsArray() {
150 $someObject = new \stdClass();
151 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
152 $persistenceManager->add($someObject);
153
154 $this->assertAttributeContains($someObject, 'addedObjects', $persistenceManager);
155 }
156
157 /**
158 * @test
159 */
160 public function removeActuallyRemovesAnObjectFromTheInternalObjectsArray() {
161 $object1 = new \stdClass();
162 $object2 = new \stdClass();
163 $object3 = new \stdClass();
164
165 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
166 $persistenceManager->add($object1);
167 $persistenceManager->add($object2);
168 $persistenceManager->add($object3);
169
170 $persistenceManager->remove($object2);
171
172 $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
173 $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
174 $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
175 }
176
177 /**
178 * @test
179 */
180 public function removeRemovesTheRightObjectEvenIfItHasBeenModifiedSinceItsAddition() {
181 $object1 = new \ArrayObject(array('val' => '1'));
182 $object2 = new \ArrayObject(array('val' => '2'));
183 $object3 = new \ArrayObject(array('val' => '3'));
184
185 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
186 $persistenceManager->add($object1);
187 $persistenceManager->add($object2);
188 $persistenceManager->add($object3);
189
190 $object2['foo'] = 'bar';
191 $object3['val'] = '2';
192
193 $persistenceManager->remove($object2);
194
195 $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
196 $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
197 $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
198 }
199
200 /**
201 * Make sure we remember the objects that are not currently add()ed
202 * but might be in persistent storage.
203 *
204 * @test
205 */
206 public function removeRetainsObjectForObjectsNotInCurrentSession() {
207 $object = new \ArrayObject(array('val' => '1'));
208 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
209 $persistenceManager->remove($object);
210
211 $this->assertAttributeContains($object, 'removedObjects', $persistenceManager);
212 }
213
214 /**
215 * @test
216 */
217 public function updateSchedulesAnObjectForPersistence() {
218 $className = uniqid('BazFixture');
219 eval ('
220 namespace Foo\\Bar\\Domain\\Model;
221 class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {
222 protected $uid = 42;
223 }
224 ');
225 eval ('
226 namespace Foo\\Bar\\Domain\\Repository;
227 class ' . $className . 'Repository extends \\TYPO3\\CMS\\Extbase\\Persistence\\Repository {}
228 ');
229 $classNameWithNamespace = 'Foo\\Bar\\Domain\\Model\\' . $className;
230 $repositorClassNameWithNamespace = 'Foo\\Bar\\Domain\\Repository\\' . $className . 'Repository';
231
232 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
233 $session = new \TYPO3\CMS\Extbase\Persistence\Generic\Session();
234 $changedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
235 $entity1 = new $classNameWithNamespace();
236 $repository = new $repositorClassNameWithNamespace;
237 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setChangedEntities'), array(), '', FALSE);
238 $mockBackend->expects($this->once())
239 ->method('setChangedEntities')
240 ->with($this->equalTo($changedEntities));
241
242 $persistenceManager->injectBackend($mockBackend);
243 $persistenceManager->injectPersistenceSession($session);
244 $repository->injectPersistenceManager($persistenceManager);
245
246 $session->registerObject($entity1, 42);
247 $changedEntities->attach($entity1);
248 $repository->update($entity1);
249 $persistenceManager->persistAll();
250
251 }
252
253 /**
254 * @test
255 */
256 public function clearStateForgetsAboutNewObjects() {
257 $mockObject = $this->getMock('TYPO3\CMS\Extbase\Persistence\Aspect\PersistenceMagicInterface');
258 $mockObject->Persistence_Object_Identifier = 'abcdefg';
259
260 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
261 $mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(FALSE));
262 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
263 $mockBackend->expects($this->any())->method('getObjectDataByIdentifier')->will($this->returnValue(FALSE));
264
265 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
266 $persistenceManager->injectPersistenceSession($mockSession);
267 $persistenceManager->injectBackend($mockBackend);
268
269 $persistenceManager->registerNewObject($mockObject);
270 $persistenceManager->clearState();
271
272 $object = $persistenceManager->getObjectByIdentifier('abcdefg');
273 $this->assertNull($object);
274 }
275
276 /**
277 * @test
278 */
279 public function tearDownWithBackendNotSupportingTearDownDoesNothing() {
280 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
281 $mockBackend->expects($this->never())->method('tearDown');
282
283 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
284 $persistenceManager->injectBackend($mockBackend);
285
286 $persistenceManager->tearDown();
287 }
288
289 /**
290 * @test
291 */
292 public function tearDownWithBackendSupportingTearDownDelegatesCallToBackend() {
293 $methods = array_merge(get_class_methods('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface'), array('tearDown'));
294 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface', $methods);
295 $mockBackend->expects($this->once())->method('tearDown');
296
297 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
298 $persistenceManager->injectBackend($mockBackend);
299
300 $persistenceManager->tearDown();
301 }
302
303 /**
304 * @test
305 *
306 * This test and the related Fixtures TxDomainModelTestEntity and
307 * TxDomainRepositoryTestEntityRepository can be removed if we do not need to support
308 * underscore class names instead of namespaced class names
309 */
310 public function persistAllAddsReconstitutedObjectFromSessionToBackendsAggregateRootObjects() {
311 $className = uniqid('BazFixture');
312 eval ('
313 class Foo_Bar_Domain_Model_' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {}
314 ');
315 eval ('
316 class Foo_Bar_Domain_Repository_' . $className . 'Repository {}
317 ');
318 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
319 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
320 $fullClassName = 'Foo_Bar_Domain_Model_' . $className;
321 $entity1 = new $fullClassName();
322 $aggregateRootObjects->attach($entity1);
323 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setAggregateRootObjects', 'setDeletedEntities'), array(), '', FALSE);
324 $mockBackend->expects($this->once())
325 ->method('setAggregateRootObjects')
326 ->with($this->equalTo($aggregateRootObjects));
327 $persistenceManager->injectBackend($mockBackend);
328 $persistenceManager->add($entity1);
329 $persistenceManager->persistAll();
330 }
331
332 /**
333 * @test
334 */
335 public function persistAllAddsNamespacedReconstitutedObjectFromSessionToBackendsAggregateRootObjects() {
336 $className = uniqid('BazFixture');
337 eval ('
338 namespace Foo\\Bar\\Domain\\Model;
339 class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {}
340 ');
341 eval ('
342 namespace Foo\\Bar\\Domain\\Repository;
343 class ' . $className . 'Repository {}
344 ');
345 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
346 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
347 $classNameWithNamespace = 'Foo\\Bar\\Domain\\Model\\' . $className;
348 $entity1 = new $classNameWithNamespace();
349 $aggregateRootObjects->attach($entity1);
350 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setAggregateRootObjects', 'setDeletedEntities'), array(), '', FALSE);
351 $mockBackend->expects($this->once())
352 ->method('setAggregateRootObjects')
353 ->with($this->equalTo($aggregateRootObjects));
354 $persistenceManager->injectBackend($mockBackend);
355 $persistenceManager->add($entity1);
356 $persistenceManager->persistAll();
357 }
358
359 }
360 ?>