[BUGFIX] Reuse entities of overridden classes in persistence session
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / SessionTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
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
18 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
19 use TYPO3\CMS\Extbase\Object\Container\Container;
20 use TYPO3\CMS\Extbase\Persistence\Generic\Session;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22
23 class SessionTest extends UnitTestCase
24 {
25 /**
26 * @test
27 */
28 public function objectRegisteredWithRegisterReconstitutedEntityCanBeRetrievedWithGetReconstitutedEntities()
29 {
30 $someObject = new \ArrayObject([]);
31 $session = new Session(new Container());
32 $session->registerReconstitutedEntity($someObject);
33
34 $ReconstitutedEntities = $session->getReconstitutedEntities();
35 $this->assertTrue($ReconstitutedEntities->contains($someObject));
36 }
37
38 /**
39 * @test
40 */
41 public function unregisterReconstitutedEntityRemovesObjectFromSession()
42 {
43 $someObject = new \ArrayObject([]);
44 $session = new Session(new Container());
45 $session->registerObject($someObject, 'fakeUuid');
46 $session->registerReconstitutedEntity($someObject);
47 $session->unregisterReconstitutedEntity($someObject);
48
49 $ReconstitutedEntities = $session->getReconstitutedEntities();
50 $this->assertFalse($ReconstitutedEntities->contains($someObject));
51 }
52
53 /**
54 * @test
55 */
56 public function hasObjectReturnsTrueForRegisteredObject()
57 {
58 $object1 = new \stdClass();
59 $object2 = new \stdClass();
60 $session = new Session(new Container());
61 $session->registerObject($object1, 12345);
62
63 $this->assertTrue($session->hasObject($object1), 'Session claims it does not have registered object.');
64 $this->assertFalse($session->hasObject($object2), 'Session claims it does have unregistered object.');
65 }
66
67 /**
68 * @test
69 */
70 public function hasIdentifierReturnsTrueForRegisteredObject()
71 {
72 $session = new Session(new Container());
73 $session->registerObject(new \stdClass(), 12345);
74
75 $this->assertTrue($session->hasIdentifier('12345', 'stdClass'), 'Session claims it does not have registered object.');
76 $this->assertFalse($session->hasIdentifier('67890', 'stdClass'), 'Session claims it does have unregistered object.');
77 }
78
79 /**
80 * @test
81 */
82 public function getIdentifierByObjectReturnsRegisteredUUIDForObject()
83 {
84 $object = new \stdClass();
85 $session = new Session(new Container());
86 $session->registerObject($object, 12345);
87
88 $this->assertEquals($session->getIdentifierByObject($object), 12345, 'Did not get UUID registered for object.');
89 }
90
91 /**
92 * @test
93 */
94 public function getObjectByIdentifierReturnsRegisteredObjectForUUID()
95 {
96 $object = new \stdClass();
97 $session = new Session(new Container());
98 $session->registerObject($object, 12345);
99
100 $this->assertSame($session->getObjectByIdentifier('12345', 'stdClass'), $object, 'Did not get object registered for UUID.');
101 }
102
103 /**
104 * @test
105 */
106 public function unregisterObjectRemovesRegisteredObject()
107 {
108 $object1 = new \stdClass();
109 $object2 = new \stdClass();
110 $session = new Session(new Container());
111 $session->registerObject($object1, 12345);
112 $session->registerObject($object2, 67890);
113
114 $this->assertTrue($session->hasObject($object1), 'Session claims it does not have registered object.');
115 $this->assertTrue($session->hasIdentifier('12345', 'stdClass'), 'Session claims it does not have registered object.');
116 $this->assertTrue($session->hasObject($object1), 'Session claims it does not have registered object.');
117 $this->assertTrue($session->hasIdentifier('67890', 'stdClass'), 'Session claims it does not have registered object.');
118
119 $session->unregisterObject($object1);
120
121 $this->assertFalse($session->hasObject($object1), 'Session claims it does have unregistered object.');
122 $this->assertFalse($session->hasIdentifier('12345', 'stdClass'), 'Session claims it does not have registered object.');
123 $this->assertTrue($session->hasObject($object2), 'Session claims it does not have registered object.');
124 $this->assertTrue($session->hasIdentifier('67890', 'stdClass'), 'Session claims it does not have registered object.');
125 }
126
127 /**
128 * @test
129 */
130 public function newSessionIsEmpty()
131 {
132 $persistenceSession = new Session(new Container());
133 $reconstitutedObjects = $persistenceSession->getReconstitutedEntities();
134 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
135 }
136
137 /**
138 * @test
139 */
140 public function objectCanBeRegisteredAsReconstituted()
141 {
142 $persistenceSession = new Session(new Container());
143 $entity = $this->createMock(AbstractEntity::class);
144 $persistenceSession->registerReconstitutedEntity($entity);
145 $reconstitutedObjects = $persistenceSession->getReconstitutedEntities();
146 $this->assertTrue($reconstitutedObjects->contains($entity), 'The object was not registered as reconstituted.');
147 }
148
149 /**
150 * @test
151 */
152 public function objectCanBeUnregisteredAsReconstituted()
153 {
154 $persistenceSession = new Session(new Container());
155 $entity = $this->createMock(AbstractEntity::class);
156 $persistenceSession->registerReconstitutedEntity($entity);
157 $persistenceSession->unregisterReconstitutedEntity($entity);
158 $reconstitutedObjects = $persistenceSession->getReconstitutedEntities();
159 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
160 }
161 }