TYPO3 CMS  TYPO3_8-7
PersistenceManagerTest.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 
20 class PersistenceManagerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
25  protected $mockObjectManager;
26 
27  protected function setUp()
28  {
29  $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
30  }
31 
36  {
37  $entity2 = new \TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\Entity2();
38  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
39  $objectStorage->attach($entity2);
40  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
41  $mockBackend->expects($this->once())->method('setAggregateRootObjects')->with($objectStorage);
42 
43  $manager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
44  $manager->_set('backend', $mockBackend);
45  $manager->add($entity2);
46 
47  $manager->persistAll();
48  }
49 
54  {
55  $entity2 = new \TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\Entity2();
56  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
57  $objectStorage->attach($entity2);
58  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
59  $mockBackend->expects($this->once())->method('setDeletedEntities')->with($objectStorage);
60 
61  $manager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
62  $manager->_set('backend', $mockBackend);
63  $manager->remove($entity2);
64 
65  $manager->persistAll();
66  }
67 
71  public function getIdentifierByObjectReturnsIdentifierFromBackend()
72  {
73  $fakeUuid = 'fakeUuid';
74  $object = new \stdClass();
75 
76  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
77  $mockBackend->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
78 
80  $manager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
81  $manager->_set('backend', $mockBackend);
82 
83  $this->assertEquals($manager->getIdentifierByObject($object), $fakeUuid);
84  }
85 
90  {
91  $fakeUuid = 'fakeUuid';
92  $object = new \stdClass();
93 
94  $mockSession = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
95  $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(true));
96  $mockSession->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid)->will($this->returnValue($object));
97 
98  $manager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
99  $manager->_set('persistenceSession', $mockSession);
100 
101  $this->assertEquals($manager->getObjectByIdentifier($fakeUuid), $object);
102  }
103 
108  {
109  $fakeUuid = '42';
110  $object = new \stdClass();
111  $fakeEntityType = get_class($object);
112 
113  $mockSession = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
114  $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(false));
115 
116  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
117  $mockBackend->expects($this->once())->method('getObjectByIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue($object));
118 
119  $manager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
120  $manager->_set('persistenceSession', $mockSession);
121  $manager->_set('backend', $mockBackend);
122 
123  $this->assertEquals($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType), $object);
124  }
125 
130  {
131  $fakeUuid = '42';
132  $fakeEntityType = 'foobar';
133 
134  $mockSession = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
135  $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid, $fakeEntityType)->will($this->returnValue(false));
136 
137  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
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::class, ['dummy']);
141  $manager->_set('persistenceSession', $mockSession);
142  $manager->_set('backend', $mockBackend);
143 
144  $this->assertNull($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType));
145  }
146 
151  {
152  $someObject = new \stdClass();
153  $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
154  $persistenceManager->add($someObject);
155 
156  $this->assertAttributeContains($someObject, 'addedObjects', $persistenceManager);
157  }
158 
163  {
164  $object1 = new \stdClass();
165  $object2 = new \stdClass();
166  $object3 = new \stdClass();
167 
168  $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
169  $persistenceManager->add($object1);
170  $persistenceManager->add($object2);
171  $persistenceManager->add($object3);
172 
173  $persistenceManager->remove($object2);
174 
175  $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
176  $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
177  $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
178  }
179 
184  {
185  $object1 = new \ArrayObject(['val' => '1']);
186  $object2 = new \ArrayObject(['val' => '2']);
187  $object3 = new \ArrayObject(['val' => '3']);
188 
189  $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
190  $persistenceManager->add($object1);
191  $persistenceManager->add($object2);
192  $persistenceManager->add($object3);
193 
194  $object2['foo'] = 'bar';
195  $object3['val'] = '2';
196 
197  $persistenceManager->remove($object2);
198 
199  $this->assertAttributeContains($object1, 'addedObjects', $persistenceManager);
200  $this->assertAttributeNotContains($object2, 'addedObjects', $persistenceManager);
201  $this->assertAttributeContains($object3, 'addedObjects', $persistenceManager);
202  }
203 
211  {
212  $object = new \ArrayObject(['val' => '1']);
213  $persistenceManager = new \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager();
214  $persistenceManager->remove($object);
215 
216  $this->assertAttributeContains($object, 'removedObjects', $persistenceManager);
217  }
218 
223  {
224  $className = $this->getUniqueId('BazFixture');
225  eval('
226  namespace ' . __NAMESPACE__ . '\\Domain\\Model;
227  class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {
228  protected $uid = 42;
229  }
230  ');
231  eval('
232  namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
233  class ' . $className . 'Repository extends \\TYPO3\\CMS\\Extbase\\Persistence\\Repository {}
234  ');
235  $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
236  $repositorClassNameWithNamespace = __NAMESPACE__ . '\\Domain\\Repository\\' . $className . 'Repository';
237 
238  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
239  $session = new \TYPO3\CMS\Extbase\Persistence\Generic\Session();
240  $changedEntities = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
241  $entity1 = new $classNameWithNamespace();
242  $repository = $this->getAccessibleMock($repositorClassNameWithNamespace, ['dummy'], [$this->mockObjectManager]);
243  $repository->_set('objectType', get_class($entity1));
244  $mockBackend = $this->getMockBuilder($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class))
245  ->setMethods(['commit', 'setChangedEntities'])
246  ->disableOriginalConstructor()
247  ->getMock();
248  $mockBackend->expects($this->once())
249  ->method('setChangedEntities')
250  ->with($this->equalTo($changedEntities));
251 
252  $persistenceManager->_set('backend', $mockBackend);
253  $persistenceManager->_set('persistenceSession', $session);
254 
255  $repository->_set('persistenceManager', $persistenceManager);
256  $session->registerObject($entity1, 42);
257  $changedEntities->attach($entity1);
258  $repository->update($entity1);
259  $persistenceManager->persistAll();
260  }
261 
266  {
267  $mockObject = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
268  $mockObject->Persistence_Object_Identifier = 'abcdefg';
269 
270  $mockSession = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
271  $mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(false));
272  $mockBackend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
273 
274  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
275  $persistenceManager->_set('persistenceSession', $mockSession);
276  $persistenceManager->_set('backend', $mockBackend);
277 
278  $persistenceManager->registerNewObject($mockObject);
279  $persistenceManager->clearState();
280 
281  $object = $persistenceManager->getObjectByIdentifier('abcdefg');
282  $this->assertNull($object);
283  }
284 
289  {
290  $methods = array_merge(get_class_methods(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class), ['tearDown']);
291  $mockBackend = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class)
292  ->setMethods($methods)
293  ->getMock();
294  $mockBackend->expects($this->once())->method('tearDown');
295 
296  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
297  $persistenceManager->_set('backend', $mockBackend);
298 
299  $persistenceManager->tearDown();
300  }
301 
310  {
311  $className = $this->getUniqueId('BazFixture');
312  eval('
313  class Foo_Bar_Domain_Model_' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
314  ');
315  eval('
316  class Foo_Bar_Domain_Repository_' . $className . 'Repository {}
317  ');
318  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
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->getMockBuilder($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class))
324  ->setMethods(['commit', 'setAggregateRootObjects', 'setDeletedEntities'])
325  ->disableOriginalConstructor()
326  ->getMock();
327  $mockBackend->expects($this->once())
328  ->method('setAggregateRootObjects')
329  ->with($this->equalTo($aggregateRootObjects));
330  $persistenceManager->_set('backend', $mockBackend);
331  $persistenceManager->add($entity1);
332  $persistenceManager->persistAll();
333  }
334 
339  {
340  $className = $this->getUniqueId('BazFixture');
341  eval('
342  namespace ' . __NAMESPACE__ . '\\Domain\\Model;
343  class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
344  ');
345  eval('
346  namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
347  class ' . $className . 'Repository {}
348  ');
349  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
350  $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
351  $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
352  $entity1 = new $classNameWithNamespace();
353  $aggregateRootObjects->attach($entity1);
354  $mockBackend = $this->getMockBuilder($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class))
355  ->setMethods(['commit', 'setAggregateRootObjects', 'setDeletedEntities'])
356  ->disableOriginalConstructor()
357  ->getMock();
358  $mockBackend->expects($this->once())
359  ->method('setAggregateRootObjects')
360  ->with($this->equalTo($aggregateRootObjects));
361  $persistenceManager->_set('backend', $mockBackend);
362  $persistenceManager->add($entity1);
363  $persistenceManager->persistAll();
364  }
365 }