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