[CLEANUP] Remove deprecated repository constructor
[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 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
40 */
41 protected $mockObjectManager;
42
43 public function setUp() {
44 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
45 }
46
47 /**
48 * @test
49 */
50 public function persistAllPassesAddedObjectsToBackend() {
51 $entity2 = new \TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\Entity2();
52 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
53 $objectStorage->attach($entity2);
54 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
55 $mockBackend->expects($this->once())->method('setAggregateRootObjects')->with($objectStorage);
56
57 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
58 $manager->_set('backend', $mockBackend);
59 $manager->add($entity2);
60
61 $manager->persistAll();
62 }
63
64 /**
65 * @test
66 */
67 public function persistAllPassesRemovedObjectsToBackend() {
68 $entity2 = new \TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\Entity2();
69 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
70 $objectStorage->attach($entity2);
71 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
72 $mockBackend->expects($this->once())->method('setDeletedEntities')->with($objectStorage);
73
74 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
75 $manager->_set('backend', $mockBackend);
76 $manager->remove($entity2);
77
78 $manager->persistAll();
79 }
80
81 /**
82 * @test
83 */
84 public function getIdentifierByObjectReturnsIdentifierFromSession() {
85 $fakeUuid = 'fakeUuid';
86 $object = new \stdClass();
87
88 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
89 $mockSession->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
90
91 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $manager */
92 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
93 $manager->_set('persistenceSession', $mockSession);
94
95 $this->assertEquals($manager->getIdentifierByObject($object), $fakeUuid);
96 }
97
98 /**
99 * @test
100 */
101 public function getObjectByIdentifierReturnsObjectFromSessionIfAvailable() {
102 $fakeUuid = 'fakeUuid';
103 $object = new \stdClass();
104
105 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
106 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(TRUE));
107 $mockSession->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid)->will($this->returnValue($object));
108
109 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
110 $manager->_set('persistenceSession', $mockSession);
111
112 $this->assertEquals($manager->getObjectByIdentifier($fakeUuid), $object);
113 }
114
115 /**
116 * @test
117 */
118 public function getObjectByIdentifierReturnsObjectFromPersistenceIfAvailable() {
119 $fakeUuid = '42';
120 $object = new \stdClass();
121 $fakeEntityType = get_class($object);
122
123 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
124 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(FALSE));
125
126 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
127 $mockBackend->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue($object));
128
129 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
130 $manager->_set('persistenceSession', $mockSession);
131 $manager->_set('backend', $mockBackend);
132
133 $this->assertEquals($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType), $object);
134 }
135
136 /**
137 * @test
138 */
139 public function getObjectByIdentifierReturnsNullForUnknownObject() {
140 $fakeUuid = '42';
141 $fakeEntityType = 'foobar';
142
143 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
144 $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue(FALSE));
145
146 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
147 $mockBackend->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue(NULL));
148
149 $manager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
150 $manager->_set('persistenceSession', $mockSession);
151 $manager->_set('backend', $mockBackend);
152
153 $this->assertNull($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType));
154 }
155
156 /**
157 * @test
158 */
159 public function addActuallyAddsAnObjectToTheInternalObjectsArray() {
160 $someObject = new \stdClass();
161 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
162 $persistenceManager->add($someObject);
163
164 $this->assertAttributeContains($someObject, 'addedObjects', $persistenceManager);
165 }
166
167 /**
168 * @test
169 */
170 public function removeActuallyRemovesAnObjectFromTheInternalObjectsArray() {
171 $object1 = new \stdClass();
172 $object2 = new \stdClass();
173 $object3 = new \stdClass();
174
175 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
176 $persistenceManager->add($object1);
177 $persistenceManager->add($object2);
178 $persistenceManager->add($object3);
179
180 $persistenceManager->remove($object2);
181
182 $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
183 $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
184 $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
185 }
186
187 /**
188 * @test
189 */
190 public function removeRemovesTheRightObjectEvenIfItHasBeenModifiedSinceItsAddition() {
191 $object1 = new \ArrayObject(array('val' => '1'));
192 $object2 = new \ArrayObject(array('val' => '2'));
193 $object3 = new \ArrayObject(array('val' => '3'));
194
195 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
196 $persistenceManager->add($object1);
197 $persistenceManager->add($object2);
198 $persistenceManager->add($object3);
199
200 $object2['foo'] = 'bar';
201 $object3['val'] = '2';
202
203 $persistenceManager->remove($object2);
204
205 $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
206 $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
207 $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
208 }
209
210 /**
211 * Make sure we remember the objects that are not currently add()ed
212 * but might be in persistent storage.
213 *
214 * @test
215 */
216 public function removeRetainsObjectForObjectsNotInCurrentSession() {
217 $object = new \ArrayObject(array('val' => '1'));
218 $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
219 $persistenceManager->remove($object);
220
221 $this->assertAttributeContains($object, 'removedObjects', $persistenceManager);
222 }
223
224 /**
225 * @test
226 */
227 public function updateSchedulesAnObjectForPersistence() {
228 $className = uniqid('BazFixture');
229 eval ('
230 namespace ' . __NAMESPACE__ . '\\Domain\\Model;
231 class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {
232 protected $uid = 42;
233 }
234 ');
235 eval ('
236 namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
237 class ' . $className . 'Repository extends \\TYPO3\\CMS\\Extbase\\Persistence\\Repository {}
238 ');
239 $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
240 $repositorClassNameWithNamespace = __NAMESPACE__ . '\\Domain\\Repository\\' . $className . 'Repository';
241
242 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
243 $session = new \TYPO3\CMS\Extbase\Persistence\Generic\Session();
244 $changedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
245 $entity1 = new $classNameWithNamespace();
246 $repository = $this->getAccessibleMock($repositorClassNameWithNamespace, array('dummy'), array($this->mockObjectManager));
247 $repository->_set('objectType', get_class($entity1));
248 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setChangedEntities'), array(), '', FALSE);
249 $mockBackend->expects($this->once())
250 ->method('setChangedEntities')
251 ->with($this->equalTo($changedEntities));
252
253 $persistenceManager->_set('backend', $mockBackend);
254 $persistenceManager->_set('persistenceSession', $session);
255
256 $repository->_set('persistenceManager', $persistenceManager);
257 $session->registerObject($entity1, 42);
258 $changedEntities->attach($entity1);
259 $repository->update($entity1);
260 $persistenceManager->persistAll();
261
262 }
263
264 /**
265 * @test
266 */
267 public function clearStateForgetsAboutNewObjects() {
268 $mockObject = $this->getMock('TYPO3\CMS\Extbase\Persistence\Aspect\PersistenceMagicInterface');
269 $mockObject->Persistence_Object_Identifier = 'abcdefg';
270
271 $mockSession = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
272 $mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(FALSE));
273 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
274 $mockBackend->expects($this->any())->method('getObjectDataByIdentifier')->will($this->returnValue(FALSE));
275
276 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
277 $persistenceManager->_set('persistenceSession', $mockSession);
278 $persistenceManager->_set('backend', $mockBackend);
279
280 $persistenceManager->registerNewObject($mockObject);
281 $persistenceManager->clearState();
282
283 $object = $persistenceManager->getObjectByIdentifier('abcdefg');
284 $this->assertNull($object);
285 }
286
287 /**
288 * @test
289 */
290 public function tearDownWithBackendNotSupportingTearDownDoesNothing() {
291 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface');
292 $mockBackend->expects($this->never())->method('tearDown');
293
294 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
295 $persistenceManager->_set('backend', $mockBackend);
296
297 $persistenceManager->tearDown();
298 }
299
300 /**
301 * @test
302 */
303 public function tearDownWithBackendSupportingTearDownDelegatesCallToBackend() {
304 $methods = array_merge(get_class_methods('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface'), array('tearDown'));
305 $mockBackend = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface', $methods);
306 $mockBackend->expects($this->once())->method('tearDown');
307
308 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
309 $persistenceManager->_set('backend', $mockBackend);
310
311 $persistenceManager->tearDown();
312 }
313
314 /**
315 * @test
316 *
317 * This test and the related Fixtures TxDomainModelTestEntity and
318 * TxDomainRepositoryTestEntityRepository can be removed if we do not need to support
319 * underscore class names instead of namespaced class names
320 */
321 public function persistAllAddsReconstitutedObjectFromSessionToBackendsAggregateRootObjects() {
322 $className = uniqid('BazFixture');
323 eval ('
324 class Foo_Bar_Domain_Model_' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {}
325 ');
326 eval ('
327 class Foo_Bar_Domain_Repository_' . $className . 'Repository {}
328 ');
329 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
330 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
331 $fullClassName = 'Foo_Bar_Domain_Model_' . $className;
332 $entity1 = new $fullClassName();
333 $aggregateRootObjects->attach($entity1);
334 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setAggregateRootObjects', 'setDeletedEntities'), array(), '', FALSE);
335 $mockBackend->expects($this->once())
336 ->method('setAggregateRootObjects')
337 ->with($this->equalTo($aggregateRootObjects));
338 $persistenceManager->_set('backend', $mockBackend);
339 $persistenceManager->add($entity1);
340 $persistenceManager->persistAll();
341 }
342
343 /**
344 * @test
345 */
346 public function persistAllAddsNamespacedReconstitutedObjectFromSessionToBackendsAggregateRootObjects() {
347 $className = uniqid('BazFixture');
348 eval ('
349 namespace ' . __NAMESPACE__ . '\\Domain\\Model;
350 class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {}
351 ');
352 eval ('
353 namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
354 class ' . $className . 'Repository {}
355 ');
356 $persistenceManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager', array('dummy'));
357 $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
358 $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
359 $entity1 = new $classNameWithNamespace();
360 $aggregateRootObjects->attach($entity1);
361 $mockBackend = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend'), array('commit', 'setAggregateRootObjects', 'setDeletedEntities'), array(), '', FALSE);
362 $mockBackend->expects($this->once())
363 ->method('setAggregateRootObjects')
364 ->with($this->equalTo($aggregateRootObjects));
365 $persistenceManager->_set('backend', $mockBackend);
366 $persistenceManager->add($entity1);
367 $persistenceManager->persistAll();
368 }
369
370 }
371 ?>