[~CONFIGURATION] Extbase: Set lockType to empty string instead of 'system'.
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Repository_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
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 class Tx_Extbase_Persistence_Repository_testcase extends Tx_Extbase_BaseTestCase {
26
27
28 /**
29 * @var Tx_Extbase_DomainObject_DomainObjectInterface
30 */
31 protected $aggregateRootClassName;
32
33 public function setUp() {
34 $this->aggregateRootClassName = uniqid('Tx_Aggregate_Root_Class_');
35 eval('class ' . $this->aggregateRootClassName . ' implements Tx_Extbase_DomainObject_DomainObjectInterface {
36 public function _memorizeCleanState() {}
37 public function _isNew() {}
38 public function _isDirty() {}
39 public function _setProperty($propertyName, $propertyValue) {}
40 public function _getProperty($propertyName) {}
41 public function _getProperties() {}
42 public function _getDirtyProperties() {}
43 public function getUid() { return 123; }
44 }');
45 }
46
47 /**
48 * @test
49 */
50 public function abstractRepositoryImplementsRepositoryInterface() {
51 $mockRepository = $this->getMock('Tx_Extbase_Persistence_Repository', array('dummy'), array(), '', FALSE);
52 $this->assertTrue($mockRepository instanceof Tx_Extbase_Persistence_RepositoryInterface);
53 }
54
55 /**
56 * @test
57 */
58 public function addCorrectlyUpdatesAddedObjectsAndRemovedObjectsCollections() {
59 $aggregateRootObject = new $this->aggregateRootClassName;
60
61 $mockAddedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
62 $mockAddedObjects->expects($this->once())->method('attach')->with($aggregateRootObject);
63
64 $mockRemovedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
65 $mockRemovedObjects->expects($this->once())->method('detach')->with($aggregateRootObject);
66
67 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
68 $mockRepository->_set('addedObjects', $mockAddedObjects);
69 $mockRepository->_set('removedObjects', $mockRemovedObjects);
70 $mockRepository->_set('objectType', $this->aggregateRootClassName);
71
72 $mockRepository->add($aggregateRootObject);
73 }
74
75 /**
76 * @test
77 */
78 public function removeDetachesObjectFromAddedObjectsCollectionIfItExists() {
79 $aggregateRootObject = new $this->aggregateRootClassName;
80
81 $mockAddedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
82 $mockAddedObjects->expects($this->once())->method('contains')->with($aggregateRootObject)->will($this->returnValue(TRUE));
83 $mockAddedObjects->expects($this->once())->method('detach')->with($aggregateRootObject);
84
85 $mockRemovedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
86 $mockRemovedObjects->expects($this->never())->method('attach');
87
88 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
89 $mockRepository->_set('addedObjects', $mockAddedObjects);
90 $mockRepository->_set('removedObjects', $mockRemovedObjects);
91 $mockRepository->_set('objectType', $this->aggregateRootClassName);
92
93 $mockRepository->remove($aggregateRootObject);
94 }
95
96 /**
97 * @test
98 */
99 public function removeAttachesObjectToRemovedObjectsCollectionIfItDoesNotExistInAddedObjectsCollection() {
100 $aggregateRootObject = new $this->aggregateRootClassName;
101
102 $mockAddedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
103 $mockAddedObjects->expects($this->once())->method('contains')->with($aggregateRootObject)->will($this->returnValue(FALSE));
104 $mockAddedObjects->expects($this->never())->method('detach');
105
106 $mockRemovedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
107 $mockRemovedObjects->expects($this->once())->method('attach')->with($aggregateRootObject);
108
109 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
110 $mockRepository->_set('addedObjects', $mockAddedObjects);
111 $mockRepository->_set('removedObjects', $mockRemovedObjects);
112 $mockRepository->_set('objectType', $this->aggregateRootClassName);
113
114 $mockRepository->remove($aggregateRootObject);
115 }
116
117 /**
118 * @test
119 */
120 public function createQueryCallsQueryFactoryWithExpectedType() {
121 $fakeRepositoryClassName = $this->aggregateRootClassName . 'Repository';
122
123 $mockQueryFactory = $this->getMock('Tx_Extbase_Persistence_QueryFactoryInterface');
124 $mockQueryFactory->expects($this->once())->method('create')->with($this->aggregateRootClassName);
125
126 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('getRepositoryClassName'), array(), $fakeRepositoryClassName, FALSE);
127 $mockRepository->_set('objectType', $this->aggregateRootClassName);
128 $mockRepository->_set('queryFactory', $mockQueryFactory);
129
130 $mockRepository->createQuery();
131 }
132
133 /**
134 * @test
135 */
136 public function findAllCreatesQueryAndReturnsResultOfExecuteCall() {
137 $expectedResult = array('one', 'two');
138
139 $mockQuery = $this->getMock('Tx_Extbase_Persistence_QueryInterface');
140 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
141
142 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('createQuery'), array(), '', FALSE);
143 $mockRepository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
144
145 $this->assertSame($expectedResult, $mockRepository->findAll());
146 }
147
148 /**
149 * @test
150 */
151 public function findByUidCreatesQueryAndReturnsResultOfExecuteCallAndCachesTheObject() {
152 $fakeUid = 123;
153
154 $mockPersistenceSession = $this->getMock('Tx_Extbase_Persistence_Session');
155
156 $mockQuery = $this->getMock('Tx_Extbase_Persistence_QueryInterface');
157 $mockQuery->expects($this->once())->method('withUid')->with($fakeUid)->will($this->returnValue('matchCriteria'));
158 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
159 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue(array('one', 'two')));
160
161 $mockIdentityMap = $this->getMock('Tx_Extbase_Persistence_IdentityMap');
162 $mockIdentityMap->expects($this->once())->method('hasIdentifier')->will($this->returnValue(FALSE));
163 $mockIdentityMap->expects($this->once())->method('registerObject')->with('one', 123);
164
165 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('createQuery'), array(), '', FALSE);
166 $mockRepository->_set('identityMap', $mockIdentityMap);
167 $mockRepository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
168
169 $this->assertSame('one', $mockRepository->findByUid($fakeUid));
170 }
171
172 /**
173 * @test
174 */
175 public function findByUidReturnsAlreadyCachedObject() {
176 $fakeUid = 123;
177
178 $mockPersistenceSession = $this->getMock('Tx_Extbase_Persistence_Session');
179
180 $mockIdentityMap = $this->getMock('Tx_Extbase_Persistence_IdentityMap');
181 $mockIdentityMap->expects($this->once())->method('hasIdentifier')->will($this->returnValue(TRUE));
182 $mockIdentityMap->expects($this->once())->method('getObjectByIdentifier')->with(123, 'MyObjectType')->will($this->returnValue('one'));
183
184 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('createQuery'), array(), '', FALSE);
185 $mockRepository->_set('identityMap', $mockIdentityMap);
186 $mockRepository->_set('objectType', 'MyObjectType');
187 $mockRepository->expects($this->never())->method('createQuery');
188
189 $this->assertSame('one', $mockRepository->findByUid($fakeUid));
190 }
191
192 /**
193 * @test
194 * @expectedException InvalidArgumentException
195 */
196 public function findByUidResultsInAnExceptionForInvalidUidArgument() {
197 $mockRepository = $this->getMock('Tx_Extbase_Persistence_Repository', array('createQuery'), array(), '', FALSE);
198 $mockRepository->findByUid(-123);
199 }
200
201 /**
202 * Replacing a reconstituted object (which has a uid) by a new object
203 * will ask the persistence backend to replace them accordingly in the
204 * identity map.
205 *
206 * @test
207 * @return void
208 */
209 public function replaceReconstitutedObjectByNewObject() {
210 $existingObject = new $this->aggregateRootClassName;
211 $newObject = new $this->aggregateRootClassName;
212
213 $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
214 $mockPersistenceBackend->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue(123));
215 $mockPersistenceBackend->expects($this->once())->method('replaceObject')->with($existingObject, $newObject);
216
217 $mockPersistenceSession = $this->getMock('Tx_Extbase_Persistence_Session', array(), array(), '', FALSE);
218 $mockPersistenceSession->expects($this->once())->method('unregisterReconstitutedObject')->with($existingObject);
219 $mockPersistenceSession->expects($this->once())->method('registerReconstitutedObject')->with($newObject);
220
221 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
222 $mockPersistenceManager->expects($this->once())->method('getSession')->will($this->returnValue($mockPersistenceSession));
223 $mockPersistenceManager->expects($this->once())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
224
225 $mockAddedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
226
227 $mockRemovedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
228
229 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
230 $mockRepository->_set('persistenceManager', $mockPersistenceManager);
231 $mockRepository->_set('objectType', $this->aggregateRootClassName);
232 $mockRepository->_set('addedObjects', $mockAddedObjects);
233 $mockRepository->_set('removedObjects', $mockRemovedObjects);
234 $mockRepository->replace($existingObject, $newObject);
235 }
236
237 /**
238 * @test
239 * @expectedException Tx_Extbase_Persistence_Exception_UnknownObject
240 */
241 public function tryingToReplaceAnUnknownObjectResultsInAnException() {
242 $existingObject = new $this->aggregateRootClassName;
243 $newObject = new $this->aggregateRootClassName;
244
245 $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_Backend', array('getIdentifierByObject'), array(), '', FALSE);
246 $mockPersistenceBackend->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue(NULL));
247
248 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
249 $mockPersistenceManager->expects($this->once())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
250
251 $mockAddedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
252
253 $mockRemovedObjects = $this->getMock('Tx_Extbase_Persistence_ObjectStorage');
254
255 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
256 $mockRepository->_set('persistenceManager', $mockPersistenceManager);
257 $mockRepository->_set('objectType', $this->aggregateRootClassName);
258 $mockRepository->_set('addedObjects', $mockAddedObjects);
259 $mockRepository->_set('removedObjects', $mockRemovedObjects);
260 $mockRepository->replace($existingObject, $newObject);
261 }
262
263 /**
264 * @test
265 */
266 public function magicCallMethodAcceptsFindBySomethingCallsAndExecutesAQueryWithThatCriteria() {
267 $mockPersistenceSession = $this->getMock('Tx_Extbase_Persistence_Session', array(), array(), '', FALSE);
268
269 $mockQuery = $this->getMock('Tx_Extbase_Persistence_QueryInterface');
270 $mockQuery->expects($this->once())->method('equals')->with('fooBaz', 'bar')->will($this->returnValue('matchCriteria'));
271 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
272 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue(array('baz', 'quux')));
273
274 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('createQuery'), array(), '', FALSE);
275 $mockRepository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
276
277 $this->assertSame(array('baz', 'quux'), $mockRepository->findByFooBaz('bar'));
278 }
279
280 /**
281 * @test
282 */
283 public function magicCallMethodAcceptsFindOneBySomethingCallsAndExecutesAQueryWithThatCriteria() {
284 $mockPersistenceSession = $this->getMock('Tx_Extbase_Persistence_Session', array(), array(), '', FALSE);
285
286 $mockQuery = $this->getMock('Tx_Extbase_Persistence_QueryInterface');
287 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
288 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
289 $mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($mockQuery));
290 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue(array('baz', 'foo')));
291
292 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('createQuery'), array(), '', FALSE);
293 $mockRepository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
294
295 $this->assertSame('baz', $mockRepository->findOneByFoo('bar'));
296 }
297
298 /**
299 * @test
300 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedMethod
301 */
302 public function magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled() {
303 $mockRepository = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Repository'), array('dummy'), array(), '', FALSE);
304 $mockRepository->__call('foo', array());
305 }
306
307 }
308 ?>