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