‪TYPO3CMS  10.4
PersistenceManagerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use PHPUnit\Framework\MockObject\MockObject;
19 use Prophecy\Argument;
20 use Psr\Container\ContainerInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
39 class ‪PersistenceManagerTest extends UnitTestCase
40 {
44  protected ‪$mockObjectManager;
45 
46  protected function ‪setUp(): void
47  {
48  parent::setUp();
49  $this->mockObjectManager = $this->createMock(ObjectManagerInterface::class);
50  }
51 
55  public function ‪persistAllPassesAddedObjectsToBackend(): void
56  {
57  $entity2 = new ‪Entity2();
58  $objectStorage = new ‪ObjectStorage();
59  $objectStorage->attach($entity2);
60  $mockBackend = $this->createMock(BackendInterface::class);
61  $mockBackend->expects(self::once())->method('setAggregateRootObjects')->with($objectStorage);
62 
63  $manager = new ‪PersistenceManager(
64  $this->createMock(QueryFactoryInterface::class),
65  $mockBackend,
66  $this->createMock(Session::class)
67  );
68  $manager->add($entity2);
69 
70  $manager->persistAll();
71  }
72 
76  public function ‪persistAllPassesRemovedObjectsToBackend(): void
77  {
78  $entity2 = new Entity2();
79  $objectStorage = new ‪ObjectStorage();
80  $objectStorage->attach($entity2);
81  $mockBackend = $this->createMock(BackendInterface::class);
82  $mockBackend->expects(self::once())->method('setDeletedEntities')->with($objectStorage);
83 
84  $manager = new ‪PersistenceManager(
85  $this->createMock(QueryFactoryInterface::class),
86  $mockBackend,
87  $this->createMock(Session::class)
88  );
89  $manager->remove($entity2);
90 
91  $manager->persistAll();
92  }
93 
98  {
99  $fakeUuid = 'fakeUuid';
100  $object = new \stdClass();
101 
102  $mockBackend = $this->createMock(BackendInterface::class);
103  $mockBackend->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid);
104 
105  $manager = new PersistenceManager(
106  $this->createMock(QueryFactoryInterface::class),
107  $mockBackend,
108  $this->createMock(Session::class)
109  );
110 
111  self::assertEquals($manager->getIdentifierByObject($object), $fakeUuid);
112  }
113 
118  {
119  $fakeUuid = 'fakeUuid';
120  $object = new \stdClass();
121 
122  $mockSession = $this->createMock(Session::class);
123  $mockSession->expects(self::once())->method('hasIdentifier')->with($fakeUuid)->willReturn(true);
124  $mockSession->expects(self::once())->method('getObjectByIdentifier')->with($fakeUuid)->willReturn($object);
125 
126  $manager = new PersistenceManager(
127  $this->createMock(QueryFactoryInterface::class),
128  $this->createMock(BackendInterface::class),
129  $mockSession
130  );
131 
132  self::assertEquals($manager->getObjectByIdentifier($fakeUuid), $object);
133  }
134 
139  {
140  $fakeUuid = '42';
141  $object = new \stdClass();
142  $fakeEntityType = get_class($object);
143 
144  $mockSession = $this->createMock(Session::class);
145  $mockSession->expects(self::once())->method('hasIdentifier')->with($fakeUuid)->willReturn(false);
146 
147  $mockBackend = $this->createMock(BackendInterface::class);
148  $mockBackend->expects(self::once())->method('getObjectByIdentifier')->with(
149  $fakeUuid,
150  $fakeEntityType
151  )->willReturn($object);
152 
153  $manager = new PersistenceManager(
154  $this->createMock(QueryFactoryInterface::class),
155  $mockBackend,
156  $mockSession
157  );
158 
159  self::assertEquals($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType), $object);
160  }
161 
166  {
167  $fakeUuid = '42';
168  $fakeEntityType = 'foobar';
169 
170  $mockSession = $this->createMock(Session::class);
171  $mockSession->expects(self::once())->method('hasIdentifier')->with(
172  $fakeUuid,
173  $fakeEntityType
174  )->willReturn(false);
175 
176  $mockBackend = $this->createMock(BackendInterface::class);
177  $mockBackend->expects(self::once())->method('getObjectByIdentifier')->with(
178  $fakeUuid,
179  $fakeEntityType
180  )->willReturn(null);
181 
182  $manager = new PersistenceManager(
183  $this->createMock(QueryFactoryInterface::class),
184  $mockBackend,
185  $mockSession
186  );
187 
188  self::assertNull($manager->getObjectByIdentifier($fakeUuid, $fakeEntityType));
189  }
190 
195  {
196  $someObject = new \stdClass();
197  $backend = $this->prophesize(BackendInterface::class);
198  $persistenceManager = new PersistenceManager(
199  $this->createMock(QueryFactoryInterface::class),
200  $backend->reveal(),
201  $this->createMock(Session::class)
202  );
203  $persistenceManager->add($someObject);
204 
205  $expectedAddedObjects = new ObjectStorage();
206  $expectedAddedObjects->attach($someObject);
207 
208  // this is the actual assertion
209  $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
210 
211  $backend->setChangedEntities(Argument::any())->shouldBeCalled();
212  $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
213  $backend->commit()->shouldBeCalled();
214  $persistenceManager->persistAll();
215  }
216 
221  {
222  $object1 = new \stdClass();
223  $object2 = new \stdClass();
224  $object3 = new \stdClass();
225 
226  $backend = $this->prophesize(BackendInterface::class);
227  $persistenceManager = new PersistenceManager(
228  $this->createMock(QueryFactoryInterface::class),
229  $backend->reveal(),
230  $this->createMock(Session::class)
231  );
232  $persistenceManager->add($object1);
233  $persistenceManager->add($object2);
234  $persistenceManager->add($object3);
235 
236  $persistenceManager->remove($object2);
237 
238  $expectedAddedObjects = new ObjectStorage();
239  $expectedAddedObjects->attach($object1);
240  $expectedAddedObjects->attach($object2);
241  $expectedAddedObjects->attach($object3);
242  $expectedAddedObjects->detach($object2);
243 
244  // this is the actual assertion
245  $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
246 
247  $backend->setChangedEntities(Argument::any())->shouldBeCalled();
248  $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
249  $backend->commit()->shouldBeCalled();
250 
251  $persistenceManager->persistAll();
252  }
253 
258  {
259  $object1 = new \ArrayObject(['val' => '1']);
260  $object2 = new \ArrayObject(['val' => '2']);
261  $object3 = new \ArrayObject(['val' => '3']);
262 
263  $backend = $this->prophesize(BackendInterface::class);
264  $persistenceManager = new PersistenceManager(
265  $this->createMock(QueryFactoryInterface::class),
266  $backend->reveal(),
267  $this->createMock(Session::class)
268  );
269  $persistenceManager->add($object1);
270  $persistenceManager->add($object2);
271  $persistenceManager->add($object3);
272 
273  $object2['foo'] = 'bar';
274  $object3['val'] = '2';
275 
276  $persistenceManager->remove($object2);
277 
278  // replay the actual sequence of actions, that makes the objectStorages comparable
279  $expectedAddedObjects = new ObjectStorage();
280  $expectedAddedObjects->attach($object1);
281  $expectedAddedObjects->attach($object2);
282  $expectedAddedObjects->attach($object3);
283  $expectedAddedObjects->detach($object2);
284 
285  // this is the actual assertion
286  $backend->setAggregateRootObjects($expectedAddedObjects)->shouldBeCalled();
287 
288  $backend->setChangedEntities(Argument::any())->shouldBeCalled();
289  $backend->setDeletedEntities(Argument::any())->shouldBeCalled();
290  $backend->commit()->shouldBeCalled();
291  $persistenceManager->persistAll();
292  }
293 
301  {
302  $object = new \ArrayObject(['val' => '1']);
303  $backend = $this->prophesize(BackendInterface::class);
304  $persistenceManager = new PersistenceManager(
305  $this->createMock(QueryFactoryInterface::class),
306  $backend->reveal(),
307  $this->createMock(Session::class)
308  );
309  $persistenceManager->remove($object);
310 
311  $expectedDeletedObjects = new ObjectStorage();
312  $expectedDeletedObjects->attach($object);
313  $backend->setAggregateRootObjects(Argument::any())->shouldBeCalled();
314  $backend->setChangedEntities(Argument::any())->shouldBeCalled();
315 
316  // this is the actual assertion
317  $backend->setDeletedEntities($expectedDeletedObjects)->shouldBeCalled();
318 
319  $backend->commit()->shouldBeCalled();
320  $persistenceManager->persistAll();
321  }
322 
326  public function ‪updateSchedulesAnObjectForPersistence(): void
327  {
328  $className = ‪StringUtility::getUniqueId('BazFixture');
329  eval('
330  namespace ' . __NAMESPACE__ . '\\Domain\\Model;
331  class ' . $className . ' extends \\' . AbstractEntity::class . ' {
332  protected $uid = 42;
333  }
334  ');
335  eval('
336  namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
337  class ' . $className . 'Repository extends \\TYPO3\\CMS\\Extbase\\Persistence\\Repository {}
338  ');
339  $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
340  $repositoryClassNameWithNamespace = __NAMESPACE__ . '\\Domain\\Repository\\' . $className . 'Repository';
341 
342  $psrContainer = $this->getMockBuilder(ContainerInterface::class)
343  ->setMethods(['has', 'get'])
344  ->disableOriginalConstructor()
345  ->getMock();
346  $psrContainer->expects(self::any())->method('has')->willReturn(false);
347  $session = new Session(new Container($psrContainer));
348  $changedEntities = new ObjectStorage();
349  $entity1 = new $classNameWithNamespace();
351  $repository = $this->getAccessibleMock($repositoryClassNameWithNamespace, ['dummy'], [$this->mockObjectManager]);
352  $repository->_set('objectType', get_class($entity1));
354  $mockBackend = $this->getMockBuilder(Backend::class)
355  ->setMethods(['commit', 'setChangedEntities'])
356  ->disableOriginalConstructor()
357  ->getMock();
358  $mockBackend->expects(self::once())
359  ->method('setChangedEntities')
360  ->with(self::equalTo($changedEntities));
361 
362  $persistenceManager = new PersistenceManager(
363  $this->createMock(QueryFactoryInterface::class),
364  $mockBackend,
365  $session
366  );
367 
368  $repository->_set('persistenceManager', $persistenceManager);
369  $session->registerObject($entity1, 42);
370  $changedEntities->attach($entity1);
371  $repository->update($entity1);
372  $persistenceManager->persistAll();
373  }
374 
378  public function ‪clearStateForgetsAboutNewObjects(): void
379  {
381  $mockObject = $this->createMock(PersistenceManagerInterface::class);
382  $mockObject->Persistence_Object_Identifier = 'abcdefg';
383 
384  $mockSession = $this->createMock(Session::class);
385  $mockSession->expects(self::any())->method('hasIdentifier')->willReturn(false);
386  $mockBackend = $this->createMock(BackendInterface::class);
387 
388  $persistenceManager = new PersistenceManager(
389  $this->createMock(QueryFactoryInterface::class),
390  $mockBackend,
391  $mockSession
392  );
393 
394  $persistenceManager->registerNewObject($mockObject);
395  $persistenceManager->clearState();
396 
397  $object = $persistenceManager->getObjectByIdentifier('abcdefg');
398  self::assertNull($object);
399  }
400 
405  {
406  $methods = array_merge(
407  get_class_methods(BackendInterface::class),
408  ['tearDown']
409  );
411  $mockBackend = $this->getMockBuilder(BackendInterface::class)
412  ->setMethods($methods)
413  ->getMock();
414  $mockBackend->expects(self::once())->method('tearDown');
415 
416  $persistenceManager = new PersistenceManager(
417  $this->createMock(QueryFactoryInterface::class),
418  $mockBackend,
419  $this->createMock(Session::class)
420  );
421 
422  $persistenceManager->tearDown();
423  }
424 
429  {
430  $className = ‪StringUtility::getUniqueId('BazFixture');
431  eval('
432  namespace ' . __NAMESPACE__ . '\\Domain\\Model;
433  class ' . $className . ' extends \\' . AbstractEntity::class . ' {}
434  ');
435  eval('
436  namespace ' . __NAMESPACE__ . '\\Domain\\Repository;
437  class ' . $className . 'Repository {}
438  ');
439  $aggregateRootObjects = new ObjectStorage();
440  $classNameWithNamespace = __NAMESPACE__ . '\\Domain\\Model\\' . $className;
441  $entity1 = new $classNameWithNamespace();
442  $aggregateRootObjects->attach($entity1);
444  $mockBackend = $this->getMockBuilder(Backend::class)
445  ->setMethods(['commit', 'setAggregateRootObjects', 'setDeletedEntities'])
446  ->disableOriginalConstructor()
447  ->getMock();
448  $mockBackend->expects(self::once())
449  ->method('setAggregateRootObjects')
450  ->with(self::equalTo($aggregateRootObjects));
451 
452  $persistenceManager = new PersistenceManager(
453  $this->createMock(QueryFactoryInterface::class),
454  $mockBackend,
455  $this->createMock(Session::class)
456  );
457 
458  $persistenceManager->add($entity1);
459  $persistenceManager->persistAll();
460  }
461 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Backend
Definition: Backend.php:51
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\setUp
‪setUp()
Definition: PersistenceManagerTest.php:45
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Object\Container\Container
Definition: Container.php:39
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\Entity2
Definition: Entity2.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\getObjectByIdentifierReturnsNullForUnknownObject
‪getObjectByIdentifierReturnsNullForUnknownObject()
Definition: PersistenceManagerTest.php:164
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\removeActuallyRemovesAnObjectFromTheInternalObjectsArray
‪removeActuallyRemovesAnObjectFromTheInternalObjectsArray()
Definition: PersistenceManagerTest.php:219
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest
Definition: PersistenceManagerTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\getIdentifierByObjectReturnsIdentifierFromBackend
‪getIdentifierByObjectReturnsIdentifierFromBackend()
Definition: PersistenceManagerTest.php:96
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\getObjectByIdentifierReturnsObjectFromPersistenceIfAvailable
‪getObjectByIdentifierReturnsObjectFromPersistenceIfAvailable()
Definition: PersistenceManagerTest.php:137
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\removeRemovesTheRightObjectEvenIfItHasBeenModifiedSinceItsAddition
‪removeRemovesTheRightObjectEvenIfItHasBeenModifiedSinceItsAddition()
Definition: PersistenceManagerTest.php:256
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\addActuallyAddsAnObjectToTheInternalObjectsArray
‪addActuallyAddsAnObjectToTheInternalObjectsArray()
Definition: PersistenceManagerTest.php:193
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
Definition: BackendInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
Definition: QueryFactoryInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
Definition: PersistenceManager.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\persistAllAddsNamespacedReconstitutedObjectFromSessionToBackendsAggregateRootObjects
‪persistAllAddsNamespacedReconstitutedObjectFromSessionToBackendsAggregateRootObjects()
Definition: PersistenceManagerTest.php:427
‪TYPO3\CMS\Extbase\Persistence\Generic\Session
Definition: Session.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\updateSchedulesAnObjectForPersistence
‪updateSchedulesAnObjectForPersistence()
Definition: PersistenceManagerTest.php:325
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\removeRetainsObjectForObjectsNotInCurrentSession
‪removeRetainsObjectForObjectsNotInCurrentSession()
Definition: PersistenceManagerTest.php:299
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\tearDownWithBackendSupportingTearDownDelegatesCallToBackend
‪tearDownWithBackendSupportingTearDownDelegatesCallToBackend()
Definition: PersistenceManagerTest.php:403
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\persistAllPassesRemovedObjectsToBackend
‪persistAllPassesRemovedObjectsToBackend()
Definition: PersistenceManagerTest.php:75
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\persistAllPassesAddedObjectsToBackend
‪persistAllPassesAddedObjectsToBackend()
Definition: PersistenceManagerTest.php:54
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\$mockObjectManager
‪ObjectManagerInterface $mockObjectManager
Definition: PersistenceManagerTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\clearStateForgetsAboutNewObjects
‪clearStateForgetsAboutNewObjects()
Definition: PersistenceManagerTest.php:377
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\PersistenceManagerTest\getObjectByIdentifierReturnsObjectFromSessionIfAvailable
‪getObjectByIdentifierReturnsObjectFromSessionIfAvailable()
Definition: PersistenceManagerTest.php:116