* added some more unit tests for the Persistence Session
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / TX_EXTMVC_Persistence_Session_testcase.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_Entity.php');
26
27 class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
28
29 public function test_NewSessionIsEmpty() {
30 $session = new TX_EXTMVC_Persistence_Session;
31 $addedObjects = $session->getAddedObjects();
32 $removedObjects = $session->getRemovedObjects();
33 $reconstitutedObjects = $session->getReconstitutedObjects();
34 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
35 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
36 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
37 }
38
39 public function test_ObjectCanBeRegisteredAsAdded() {
40 $session = new TX_EXTMVC_Persistence_Session;
41 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
42 $session->registerAddedObject($entity);
43 $addedObjects = $session->getAddedObjects();
44 $removedObjects = $session->getRemovedObjects();
45 $reconstitutedObjects = $session->getReconstitutedObjects();
46
47 $this->assertEquals(1, count($addedObjects), 'The added objects storage holds 0 or more than 1 objects.');
48 $this->assertSame($entity, $addedObjects[0], 'The returned object was not the same as the registered one.');
49 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
50 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
51 }
52
53 public function test_ObjectCanBeRegisteredAsRemoved() {
54 $session = new TX_EXTMVC_Persistence_Session;
55 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
56 $session->registerRemovedObject($entity);
57 $addedObjects = $session->getAddedObjects();
58 $removedObjects = $session->getRemovedObjects();
59 $reconstitutedObjects = $session->getReconstitutedObjects();
60
61 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
62 $this->assertEquals(1, count($removedObjects), 'The removed objects storage holds 0 or more than 1 objects.');
63 $this->assertSame($entity, $removedObjects[0], 'The returned object was not the same as the registered one.');
64 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
65 }
66
67 public function test_ObjectCanBeRegisteredAsReconstituted() {
68 $session = new TX_EXTMVC_Persistence_Session;
69 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
70 $session->registerReconstitutedObject($entity);
71 $addedObjects = $session->getAddedObjects();
72 $removedObjects = $session->getRemovedObjects();
73 $reconstitutedObjects = $session->getReconstitutedObjects();
74
75 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
76 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
77 $this->assertEquals(1, count($reconstitutedObjects), 'The reconstituted objects storage holds 0 or more than 1 objects.');
78 $this->assertSame($entity, $reconstitutedObjects[0], 'The returned object was not the same as the registered one.');
79 }
80
81 public function test_ObjectCanBeUnregisteredAsAdded() {
82 $session = new TX_EXTMVC_Persistence_Session;
83 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
84 $session->registerAddedObject($entity);
85 $session->unregisterAddedObject($entity);
86 $addedObjects = $session->getAddedObjects();
87 $removedObjects = $session->getRemovedObjects();
88 $reconstitutedObjects = $session->getReconstitutedObjects();
89
90 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
91 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
92 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
93 }
94
95 public function test_ObjectCanBeUnregisteredAsRemoved() {
96 $session = new TX_EXTMVC_Persistence_Session;
97 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
98 $session->registerRemovedObject($entity);
99 $session->unregisterRemovedObject($entity);
100 $addedObjects = $session->getAddedObjects();
101 $removedObjects = $session->getRemovedObjects();
102 $reconstitutedObjects = $session->getReconstitutedObjects();
103
104 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
105 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
106 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
107 }
108
109 public function test_ObjectCanBeUnregisteredAsReconstituted() {
110 $session = new TX_EXTMVC_Persistence_Session;
111 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
112 $session->registerReconstitutedObject($entity);
113 $session->unregisterReconstitutedObject($entity);
114 $addedObjects = $session->getAddedObjects();
115 $removedObjects = $session->getRemovedObjects();
116 $reconstitutedObjects = $session->getReconstitutedObjects();
117
118 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
119 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
120 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
121 }
122 public function test_ObjectCanBeRemovedAfterBeingAdded() {
123 $session = new TX_EXTMVC_Persistence_Session;
124 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
125 $session->registerAddedObject($entity);
126 $session->registerRemovedObject($entity);
127 $addedObjects = $session->getAddedObjects();
128 $removedObjects = $session->getRemovedObjects();
129 $reconstitutedObjects = $session->getReconstitutedObjects();
130
131 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
132 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
133 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
134 }
135
136 public function test_AnObjectCanBeRemovedAfterBeingAdded() {
137 $session = new TX_EXTMVC_Persistence_Session;
138 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
139 $session->registerAddedObject($entity);
140 $session->registerRemovedObject($entity);
141 $addedObjects = $session->getAddedObjects();
142 $removedObjects = $session->getRemovedObjects();
143 $reconstitutedObjects = $session->getReconstitutedObjects();
144
145 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
146 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
147 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
148 }
149
150 public function test_TryingToRegisterReconstitutedObjectsAsAddedResultsInAnException() {
151 $this->setExpectedException('InvalidArgumentException');
152 $session = new TX_EXTMVC_Persistence_Session;
153 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
154 $session->registerReconstitutedObject($entity);
155 $session->registerAddedObject($entity);
156 }
157
158 public function test_TryingToRegisterAddedObjectsAsReconstitutedResultsInAnException() {
159 $this->setExpectedException('InvalidArgumentException');
160 $session = new TX_EXTMVC_Persistence_Session;
161 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
162 $session->registerAddedObject($entity1);
163 $session->registerReconstitutedObject($entity1);
164 }
165
166 public function test_SessionCanBeCleared() {
167 $session = new TX_EXTMVC_Persistence_Session;
168 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
169 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
170 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
171 $session->registerAddedObject($entity1);
172 $session->registerRemovedObject($entity2);
173 $session->registerReconstitutedObject($entity3);
174 $session->registerAggregateRootClassName('TX_EXTMVC_DomainObject_Entity');
175 $session->clear();
176 $addedObjects = $session->getAddedObjects();
177 $removedObjects = $session->getRemovedObjects();
178 $reconstitutedObjects = $session->getReconstitutedObjects();
179 $aggregateRootClassNames = $session->getAggregateRootClassNames();
180
181 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
182 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
183 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
184 $this->assertEquals(0, count($aggregateRootClassName), 'The aggregate root class name was not empty.');
185 }
186
187 public function test_ObjectCanBeUnregistered() {
188 $session = new TX_EXTMVC_Persistence_Session;
189 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
190 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
191 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
192 $session->registerAddedObject($entity1);
193 $session->registerRemovedObject($entity2);
194 $session->registerReconstitutedObject($entity3);
195 $session->unregisterObject($entity1);
196 $session->unregisterObject($entity2);
197 $session->unregisterObject($entity3);
198 $addedObjects = $session->getAddedObjects();
199 $removedObjects = $session->getRemovedObjects();
200 $reconstitutedObjects = $session->getReconstitutedObjects();
201
202 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
203 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
204 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
205 }
206
207
208 }
209 ?>