6eddaefc1c1f230db946e24c3d88e581e0907ac2
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / RepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Persistence\Repository|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
30 */
31 protected $repository;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
35 */
36 protected $mockObjectManager;
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap
40 */
41 protected $mockIdentityMap;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory
45 */
46 protected $mockQueryFactory;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
50 */
51 protected $mockBackend;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
55 */
56 protected $mockSession;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
60 */
61 protected $mockPersistenceManager;
62
63 /**
64 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
65 */
66 protected $mockQuery;
67
68 /**
69 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
70 */
71 protected $querySettings;
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
75 */
76 protected $mockQuerySettings;
77
78 public function setUp() {
79 $this->mockIdentityMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\IdentityMap');
80 $this->mockQueryFactory = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryFactory');
81 $this->mockQuery = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface');
82 $this->mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
83 $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
84 $this->mockQueryFactory->expects($this->any())->method('create')->will($this->returnValue($this->mockQuery));
85 $this->mockBackend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
86 $this->mockSession = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
87 $this->mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
88 $this->mockPersistenceManager->expects($this->any())->method('createQueryForType')->will($this->returnValue($this->mockQuery));
89 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
90 $this->repository = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Repository', array('dummy'), array($this->mockObjectManager));
91 $this->repository->injectPersistenceManager($this->mockPersistenceManager);
92 }
93
94 /**
95 * @test
96 */
97 public function abstractRepositoryImplementsRepositoryInterface() {
98 $this->assertTrue($this->repository instanceof \TYPO3\CMS\Extbase\Persistence\RepositoryInterface);
99 }
100
101 /**
102 * @test
103 */
104 public function createQueryCallsPersistenceManagerWithExpectedClassName() {
105 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
106 $mockPersistenceManager->expects($this->once())->method('createQueryForType')->with('ExpectedType');
107
108 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
109 $repository->_set('objectType', 'ExpectedType');
110 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
111
112 $repository->createQuery();
113 }
114
115 /**
116 * @test
117 */
118 public function createQuerySetsDefaultOrderingIfDefined() {
119 $orderings = array('foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
120 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
121 $mockQuery->expects($this->once())->method('setOrderings')->with($orderings);
122 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
123 $mockPersistenceManager->expects($this->exactly(2))->method('createQueryForType')->with('ExpectedType')->will($this->returnValue($mockQuery));
124
125 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
126 $repository->_set('objectType', 'ExpectedType');
127 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
128 $repository->setDefaultOrderings($orderings);
129 $repository->createQuery();
130
131 $repository->setDefaultOrderings(array());
132 $repository->createQuery();
133 }
134
135 /**
136 * @test
137 */
138 public function findAllCreatesQueryAndReturnsResultOfExecuteCall() {
139 $expectedResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
140
141 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
142 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($expectedResult));
143
144 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
145 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
146
147 $this->assertSame($expectedResult, $repository->findAll());
148 }
149
150 /**
151 * @test
152 */
153 public function findByidentifierReturnsResultOfGetObjectByIdentifierCall() {
154 $identifier = '42';
155 $object = new \stdClass();
156
157 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
158 $mockPersistenceManager->expects($this->once())->method('getObjectByIdentifier')->with($identifier, 'stdClass')->will($this->returnValue($object));
159
160 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
161 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
162 $repository->_set('objectType', 'stdClass');
163
164 $this->assertSame($object, $repository->findByIdentifier($identifier));
165 }
166
167 /**
168 * @test
169 */
170 public function addDelegatesToPersistenceManager() {
171 $object = new \stdClass();
172 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
173 $mockPersistenceManager->expects($this->once())->method('add')->with($object);
174 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
175 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
176 $repository->_set('objectType', get_class($object));
177 $repository->add($object);
178 }
179
180 /**
181 * @test
182 */
183 public function removeDelegatesToPersistenceManager() {
184 $object = new \stdClass();
185 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
186 $mockPersistenceManager->expects($this->once())->method('remove')->with($object);
187 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
188 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
189 $repository->_set('objectType', get_class($object));
190 $repository->remove($object);
191 }
192
193 /**
194 * @test
195 */
196 public function updateDelegatesToPersistenceManager() {
197 $object = new \stdClass();
198 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
199 $mockPersistenceManager->expects($this->once())->method('update')->with($object);
200 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
201 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
202 $repository->_set('objectType', get_class($object));
203 $repository->update($object);
204 }
205
206 /**
207 * @test
208 */
209 public function magicCallMethodAcceptsFindBySomethingCallsAndExecutesAQueryWithThatCriteria() {
210 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
211 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
212 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
213 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
214 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($mockQueryResult));
215
216 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
217 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
218
219 $this->assertSame($mockQueryResult, $repository->findByFoo('bar'));
220 }
221
222 /**
223 * @test
224 */
225 public function magicCallMethodAcceptsFindOneBySomethingCallsAndExecutesAQueryWithThatCriteria() {
226 $object = new \stdClass();
227 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
228 $mockQueryResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
229 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
230 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
231 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
232 $mockQuery->expects($this->once())->method('setLimit')->will($this->returnValue($mockQuery));
233 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
234
235 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
236 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
237
238 $this->assertSame($object, $repository->findOneByFoo('bar'));
239 }
240
241 /**
242 * @test
243 */
244 public function magicCallMethodAcceptsCountBySomethingCallsAndExecutesAQueryWithThatCriteria() {
245 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
246 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
247 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
248 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
249 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
250 $mockQueryResult->expects($this->once())->method('count')->will($this->returnValue(2));
251
252 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
253 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
254
255 $this->assertSame(2, $repository->countByFoo('bar'));
256 }
257
258 /**
259 * @test
260 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
261 */
262 public function magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled() {
263 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
264 $repository->__call('foo', array());
265 }
266
267 /**
268 * @test
269 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
270 */
271 public function addChecksObjectType() {
272 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
273 $repository->_set('objectType', 'ExpectedObjectType');
274
275 $repository->add(new \stdClass());
276 }
277
278 /**
279 * @test
280 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
281 */
282 public function removeChecksObjectType() {
283 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
284 $repository->_set('objectType', 'ExpectedObjectType');
285
286 $repository->remove(new \stdClass());
287 }
288 /**
289 * @test
290 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
291 */
292 public function updateChecksObjectType() {
293 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
294 $repository->_set('objectType', 'ExpectedObjectType');
295
296 $repository->update(new \stdClass());
297 }
298
299 /**
300 * dataProvider for createQueryCallsQueryFactoryWithExpectedType
301 *
302 * @return array
303 */
304 public function modelAndRepositoryClassNames() {
305 return array(
306 array('Tx_BlogExample_Domain_Repository_BlogRepository', 'Tx_BlogExample_Domain_Model_Blog'),
307 array('_Domain_Repository_Content_PageRepository', '_Domain_Model_Content_Page'),
308 array('Tx_RepositoryExample_Domain_Repository_SomeModelRepository', 'Tx_RepositoryExample_Domain_Model_SomeModel'),
309 array('Tx_RepositoryExample_Domain_Repository_RepositoryRepository', 'Tx_RepositoryExample_Domain_Model_Repository'),
310 array('Tx_Repository_Domain_Repository_RepositoryRepository', 'Tx_Repository_Domain_Model_Repository')
311 );
312 }
313
314 /**
315 * @test
316 * @dataProvider modelAndRepositoryClassNames
317 * @param string $repositoryClassName
318 * @param string $modelClassName
319 */
320 public function constructSetsObjectTypeFromClassName($repositoryClassName, $modelClassName) {
321 $mockClassName = 'MockRepository' . uniqid();
322 eval('class ' . $mockClassName . ' extends TYPO3\\CMS\\Extbase\\Persistence\\Repository {
323 protected function getRepositoryClassName() {
324 return \'' . $repositoryClassName . '\';
325 }
326 public function _getObjectType() {
327 return $this->objectType;
328 }
329 }');
330 $this->repository = new $mockClassName($this->mockObjectManager);
331 $this->assertEquals($modelClassName, $this->repository->_getObjectType());
332 }
333
334 /**
335 * @test
336 */
337 public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings() {
338 $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('foo');
339 $mockDefaultQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
340 $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
341 $query = $this->repository->createQuery();
342 $instanceQuerySettings = $query->getQuerySettings();
343 $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
344 $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
345 }
346
347 /**
348 * @test
349 */
350 public function findByUidReturnsResultOfGetObjectByIdentifierCall() {
351 $fakeUid = '123';
352 $object = new \stdClass();
353 $this->repository->_set('objectType', 'someObjectType');
354 $this->mockPersistenceManager->expects($this->once())->method('getObjectByIdentifier')->with($fakeUid, 'someObjectType')->will($this->returnValue($object));
355 $expectedResult = $object;
356 $actualResult = $this->repository->findByUid($fakeUid);
357 $this->assertSame($expectedResult, $actualResult);
358 }
359
360 /**
361 * @test
362 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
363 */
364 public function updateRejectsObjectsOfWrongType() {
365 $this->repository->_set('objectType', 'Foo');
366 $this->repository->update(new \stdClass());
367 }
368
369 /**
370 * @test
371 */
372 public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult() {
373 $queryResultArray = array(
374 0 => array(
375 'foo' => 'bar',
376 ),
377 );
378 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
379 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
380 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
381 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
382 $this->assertSame(array('foo' => 'bar'), $this->repository->findOneByFoo('bar'));
383 }
384
385 /**
386 * @test
387 */
388 public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult() {
389 $queryResultArray = array();
390 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
391 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
392 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
393 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
394 $this->assertNull($this->repository->findOneByFoo('bar'));
395 }
396
397 }
398
399 ?>