EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Session_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(PATH_tslib . 'class.tslib_content.php');
26
27 class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
28
29 public function setUp() {
30 }
31
32 public function test_NewSessionIsEmpty() {
33 $persistenceSession = new TX_EXTMVC_Persistence_Session;
34 $addedObjects = $persistenceSession->getAddedObjects();
35 $removedObjects = $persistenceSession->getRemovedObjects();
36 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
37 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
38 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
39 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
40 }
41
42 public function test_ObjectCanBeRegisteredAsAdded() {
43 $persistenceSession = new TX_EXTMVC_Persistence_Session;
44 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
45 $persistenceSession->registerAddedObject($entity);
46 $addedObjects = $persistenceSession->getAddedObjects();
47 $removedObjects = $persistenceSession->getRemovedObjects();
48 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
49
50 $this->assertEquals(1, count($addedObjects), 'The added objects storage holds 0 or more than 1 objects.');
51 $this->assertSame($entity, $addedObjects[0], 'The returned object was not the same as the registered one.');
52 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
53 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
54 }
55
56 public function test_ObjectCanBeRegisteredAsRemoved() {
57 $persistenceSession = new TX_EXTMVC_Persistence_Session;
58 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
59 $persistenceSession->registerRemovedObject($entity);
60 $addedObjects = $persistenceSession->getAddedObjects();
61 $removedObjects = $persistenceSession->getRemovedObjects();
62 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
63
64 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
65 $this->assertEquals(1, count($removedObjects), 'The removed objects storage holds 0 or more than 1 objects.');
66 $this->assertSame($entity, $removedObjects[0], 'The returned object was not the same as the registered one.');
67 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
68 }
69
70 public function test_ObjectCanBeRegisteredAsReconstituted() {
71 $persistenceSession = new TX_EXTMVC_Persistence_Session;
72 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
73 $persistenceSession->registerReconstitutedObject($entity);
74 $addedObjects = $persistenceSession->getAddedObjects();
75 $removedObjects = $persistenceSession->getRemovedObjects();
76 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
77
78 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
79 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
80 $this->assertEquals(1, count($reconstitutedObjects), 'The reconstituted objects storage holds 0 or more than 1 objects.');
81 $this->assertSame($entity, $reconstitutedObjects[0], 'The returned object was not the same as the registered one.');
82 }
83
84 public function test_ObjectCanBeUnregisteredAsAdded() {
85 $persistenceSession = new TX_EXTMVC_Persistence_Session;
86 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
87 $persistenceSession->registerAddedObject($entity);
88 $persistenceSession->unregisterAddedObject($entity);
89 $addedObjects = $persistenceSession->getAddedObjects();
90 $removedObjects = $persistenceSession->getRemovedObjects();
91 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
92
93 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
94 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
95 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
96 }
97
98 public function test_ObjectCanBeUnregisteredAsRemoved() {
99 $persistenceSession = new TX_EXTMVC_Persistence_Session;
100 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
101 $persistenceSession->registerRemovedObject($entity);
102 $persistenceSession->unregisterRemovedObject($entity);
103 $addedObjects = $persistenceSession->getAddedObjects();
104 $removedObjects = $persistenceSession->getRemovedObjects();
105 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
106
107 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
108 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
109 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
110 }
111
112 public function test_ObjectCanBeUnregisteredAsReconstituted() {
113 $persistenceSession = new TX_EXTMVC_Persistence_Session;
114 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
115 $persistenceSession->registerReconstitutedObject($entity);
116 $persistenceSession->unregisterReconstitutedObject($entity);
117 $addedObjects = $persistenceSession->getAddedObjects();
118 $removedObjects = $persistenceSession->getRemovedObjects();
119 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
120
121 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
122 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
123 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
124 }
125 public function test_ObjectCanBeRemovedAfterBeingAdded() {
126 $persistenceSession = new TX_EXTMVC_Persistence_Session;
127 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
128 $persistenceSession->registerAddedObject($entity);
129 $persistenceSession->registerRemovedObject($entity);
130 $addedObjects = $persistenceSession->getAddedObjects();
131 $removedObjects = $persistenceSession->getRemovedObjects();
132 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
133
134 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
135 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
136 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
137 }
138
139 public function test_AnObjectCanBeRemovedAfterBeingAdded() {
140 $persistenceSession = new TX_EXTMVC_Persistence_Session;
141 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
142 $persistenceSession->registerAddedObject($entity);
143 $persistenceSession->registerRemovedObject($entity);
144 $addedObjects = $persistenceSession->getAddedObjects();
145 $removedObjects = $persistenceSession->getRemovedObjects();
146 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
147
148 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
149 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
150 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
151 }
152
153 public function test_TryingToRegisterReconstitutedObjectsAsAddedResultsInAnException() {
154 $this->setExpectedException('InvalidArgumentException');
155 $persistenceSession = new TX_EXTMVC_Persistence_Session;
156 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
157 $persistenceSession->registerReconstitutedObject($entity);
158 $persistenceSession->registerAddedObject($entity);
159 }
160
161 public function test_TryingToRegisterAddedObjectsAsReconstitutedResultsInAnException() {
162 $this->setExpectedException('InvalidArgumentException');
163 $persistenceSession = new TX_EXTMVC_Persistence_Session;
164 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
165 $persistenceSession->registerAddedObject($entity1);
166 $persistenceSession->registerReconstitutedObject($entity1);
167 }
168
169 public function test_SessionCanBeCleared() {
170 $persistenceSession = new TX_EXTMVC_Persistence_Session;
171 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
172 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
173 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
174 $persistenceSession->registerAddedObject($entity1);
175 $persistenceSession->registerRemovedObject($entity2);
176 $persistenceSession->registerReconstitutedObject($entity3);
177 $persistenceSession->registerAggregateRootClassName('TX_EXTMVC_DomainObject_Entity');
178 $persistenceSession->clear();
179 $addedObjects = $persistenceSession->getAddedObjects();
180 $removedObjects = $persistenceSession->getRemovedObjects();
181 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
182 $aggregateRootClassNames = $persistenceSession->getAggregateRootClassNames();
183
184 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
185 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
186 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
187 $this->assertEquals(0, count($aggregateRootClassName), 'The aggregate root class name was not empty.');
188 }
189
190 public function test_ObjectCanBeUnregistered() {
191 $persistenceSession = new TX_EXTMVC_Persistence_Session;
192 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
193 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
194 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
195 $persistenceSession->registerAddedObject($entity1);
196 $persistenceSession->registerRemovedObject($entity2);
197 $persistenceSession->registerReconstitutedObject($entity3);
198 $persistenceSession->unregisterObject($entity1);
199 $persistenceSession->unregisterObject($entity2);
200 $persistenceSession->unregisterObject($entity3);
201 $addedObjects = $persistenceSession->getAddedObjects();
202 $removedObjects = $persistenceSession->getRemovedObjects();
203 $reconstitutedObjects = $persistenceSession->getReconstitutedObjects();
204
205 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
206 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
207 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
208 }
209
210 public function test_DirtyEntitiesAreReturned() {
211 $persistenceSession = new TX_EXTMVC_Persistence_Session;
212 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
213 $entity->expects($this->any())
214 ->method('_isDirty')
215 ->will($this->returnValue(TRUE));
216 $persistenceSession->registerReconstitutedObject($entity);
217 $dirtyObjects = $persistenceSession->getDirtyObjects();
218 $this->assertEquals(1, count($dirtyObjects), 'There is more than one dirty object.');
219 $this->assertEquals($entity, $dirtyObjects[0], 'The entity doesn\'t equal to the dirty object retrieved from the persistenceSession.');
220 }
221
222
223 }
224 ?>