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