TYPO3 CMS  TYPO3_7-6
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 
21 {
25  protected $mockObjectManager;
26 
27  protected function setUp()
28  {
29  $this->mockObjectManager = $this->getMock(\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->getMock(\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->getMock(\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->getMock(\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->getMock(\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->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
114  $mockSession->expects($this->once())->method('hasIdentifier')->with($fakeUuid)->will($this->returnValue(false));
115 
116  $mockBackend = $this->getMock(\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->getMock(\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->getMock(\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->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class), ['commit', 'setChangedEntities'], [], '', false);
245  $mockBackend->expects($this->once())
246  ->method('setChangedEntities')
247  ->with($this->equalTo($changedEntities));
248 
249  $persistenceManager->_set('backend', $mockBackend);
250  $persistenceManager->_set('persistenceSession', $session);
251 
252  $repository->_set('persistenceManager', $persistenceManager);
253  $session->registerObject($entity1, 42);
254  $changedEntities->attach($entity1);
255  $repository->update($entity1);
256  $persistenceManager->persistAll();
257  }
258 
263  {
264  $mockObject = $this->getMock('TYPO3\CMS\Extbase\Persistence\Aspect\PersistenceMagicInterface');
265  $mockObject->Persistence_Object_Identifier = 'abcdefg';
266 
267  $mockSession = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
268  $mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(false));
269  $mockBackend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
270  $mockBackend->expects($this->any())->method('getObjectDataByIdentifier')->will($this->returnValue(false));
271 
272  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
273  $persistenceManager->_set('persistenceSession', $mockSession);
274  $persistenceManager->_set('backend', $mockBackend);
275 
276  $persistenceManager->registerNewObject($mockObject);
277  $persistenceManager->clearState();
278 
279  $object = $persistenceManager->getObjectByIdentifier('abcdefg');
280  $this->assertNull($object);
281  }
282 
287  {
288  $mockBackend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
289  $mockBackend->expects($this->never())->method('tearDown');
290 
291  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
292  $persistenceManager->_set('backend', $mockBackend);
293 
294  $persistenceManager->tearDown();
295  }
296 
301  {
302  $methods = array_merge(get_class_methods(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class), ['tearDown']);
303  $mockBackend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class, $methods);
304  $mockBackend->expects($this->once())->method('tearDown');
305 
306  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
307  $persistenceManager->_set('backend', $mockBackend);
308 
309  $persistenceManager->tearDown();
310  }
311 
320  {
321  $className = $this->getUniqueId('BazFixture');
322  eval('
323  class Foo_Bar_Domain_Model_' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
324  ');
325  eval('
326  class Foo_Bar_Domain_Repository_' . $className . 'Repository {}
327  ');
328  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['dummy']);
329  $aggregateRootObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
330  $fullClassName = 'Foo_Bar_Domain_Model_' . $className;
331  $entity1 = new $fullClassName();
332  $aggregateRootObjects->attach($entity1);
333  $mockBackend = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class), ['commit', 'setAggregateRootObjects', 'setDeletedEntities'], [], '', false);
334  $mockBackend->expects($this->once())
335  ->method('setAggregateRootObjects')
336  ->with($this->equalTo($aggregateRootObjects));
337  $persistenceManager->_set('backend', $mockBackend);
338  $persistenceManager->add($entity1);
339  $persistenceManager->persistAll();
340  }
341 
346  {
347  $className = $this->getUniqueId('BazFixture');
348  eval('
349  namespace ' . __NAMESPACE__ . '\\Domain\\Model;
350  class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
351  ');
352  eval('
353  namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
354  class ' . $className . 'Repository {}
355  ');
356  $persistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['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::class), ['commit', 'setAggregateRootObjects', 'setDeletedEntities'], [], '', 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 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)