[CLEANUP] Remove deprecated repository constructor
[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->_set('persistenceManager', $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 $this->repository->_set('objectType', 'ExpectedType');
109 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
110
111 $this->repository->createQuery();
112 }
113
114 /**
115 * @test
116 */
117 public function createQuerySetsDefaultOrderingIfDefined() {
118 $orderings = array('foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
119 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
120 $mockQuery->expects($this->once())->method('setOrderings')->with($orderings);
121 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
122 $mockPersistenceManager->expects($this->exactly(2))->method('createQueryForType')->with('ExpectedType')->will($this->returnValue($mockQuery));
123
124 $this->repository->_set('objectType', 'ExpectedType');
125 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
126 $this->repository->setDefaultOrderings($orderings);
127 $this->repository->createQuery();
128
129 $this->repository->setDefaultOrderings(array());
130 $this->repository->createQuery();
131 }
132
133 /**
134 * @test
135 */
136 public function findAllCreatesQueryAndReturnsResultOfExecuteCall() {
137 $expectedResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
138
139 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
140 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($expectedResult));
141
142 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
143 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
144
145 $this->assertSame($expectedResult, $repository->findAll());
146 }
147
148 /**
149 * @test
150 */
151 public function findByidentifierReturnsResultOfGetObjectByIdentifierCall() {
152 $identifier = '42';
153 $object = new \stdClass();
154
155 $expectedResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
156 $expectedResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
157
158 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
159 $mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
160 $mockQuery->expects($this->once())->method('matching')->will($this->returnValue($mockQuery));
161 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
162
163 $session = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\Session');
164 $session->expects($this->once())->method('hasIdentifier')->will($this->returnValue(FALSE));
165
166 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
167 $repository->_set('session', $session);
168 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
169 $this->assertSame($object, $repository->findByIdentifier($identifier));
170 }
171
172 /**
173 * @test
174 */
175 public function addDelegatesToPersistenceManager() {
176 $object = new \stdClass();
177 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
178 $mockPersistenceManager->expects($this->once())->method('add')->with($object);
179 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
180 $this->repository->_set('objectType', get_class($object));
181 $this->repository->add($object);
182 }
183
184 /**
185 * @test
186 */
187 public function removeDelegatesToPersistenceManager() {
188 $object = new \stdClass();
189 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
190 $mockPersistenceManager->expects($this->once())->method('remove')->with($object);
191 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
192 $this->repository->_set('objectType', get_class($object));
193 $this->repository->remove($object);
194 }
195
196 /**
197 * @test
198 */
199 public function updateDelegatesToPersistenceManager() {
200 $object = new \stdClass();
201 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
202 $mockPersistenceManager->expects($this->once())->method('update')->with($object);
203 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
204 $this->repository->_set('objectType', get_class($object));
205 $this->repository->update($object);
206 }
207
208 /**
209 * @test
210 */
211 public function magicCallMethodAcceptsFindBySomethingCallsAndExecutesAQueryWithThatCriteria() {
212 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
213 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
214 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
215 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
216 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($mockQueryResult));
217
218 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
219 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
220
221 $this->assertSame($mockQueryResult, $repository->findByFoo('bar'));
222 }
223
224 /**
225 * @test
226 */
227 public function magicCallMethodAcceptsFindOneBySomethingCallsAndExecutesAQueryWithThatCriteria() {
228 $object = new \stdClass();
229 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
230 $mockQueryResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
231 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
232 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
233 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
234 $mockQuery->expects($this->once())->method('setLimit')->will($this->returnValue($mockQuery));
235 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
236
237 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
238 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
239
240 $this->assertSame($object, $repository->findOneByFoo('bar'));
241 }
242
243 /**
244 * @test
245 */
246 public function magicCallMethodAcceptsCountBySomethingCallsAndExecutesAQueryWithThatCriteria() {
247 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
248 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
249 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
250 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
251 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
252 $mockQueryResult->expects($this->once())->method('count')->will($this->returnValue(2));
253
254 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
255 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
256
257 $this->assertSame(2, $repository->countByFoo('bar'));
258 }
259
260 /**
261 * @test
262 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
263 */
264 public function magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled() {
265 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'), array($this->mockObjectManager));
266 $repository->__call('foo', array());
267 }
268
269 /**
270 * @test
271 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
272 */
273 public function addChecksObjectType() {
274 $this->repository->_set('objectType', 'ExpectedObjectType');
275 $this->repository->add(new \stdClass());
276 }
277
278 /**
279 * @test
280 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
281 */
282 public function removeChecksObjectType() {
283 $this->repository->_set('objectType', 'ExpectedObjectType');
284 $this->repository->remove(new \stdClass());
285 }
286 /**
287 * @test
288 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
289 */
290 public function updateChecksObjectType() {
291 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'), array($this->mockObjectManager));
292 $repository->_set('objectType', 'ExpectedObjectType');
293
294 $repository->update(new \stdClass());
295 }
296
297 /**
298 * dataProvider for createQueryCallsQueryFactoryWithExpectedType
299 *
300 * @return array
301 */
302 public function modelAndRepositoryClassNames() {
303 return array(
304 array('Tx_BlogExample_Domain_Repository_BlogRepository', 'Tx_BlogExample_Domain_Model_Blog'),
305 array('_Domain_Repository_Content_PageRepository', '_Domain_Model_Content_Page'),
306 array('Tx_RepositoryExample_Domain_Repository_SomeModelRepository', 'Tx_RepositoryExample_Domain_Model_SomeModel'),
307 array('Tx_RepositoryExample_Domain_Repository_RepositoryRepository', 'Tx_RepositoryExample_Domain_Model_Repository'),
308 array('Tx_Repository_Domain_Repository_RepositoryRepository', 'Tx_Repository_Domain_Model_Repository')
309 );
310 }
311
312 /**
313 * @test
314 * @dataProvider modelAndRepositoryClassNames
315 * @param string $repositoryClassName
316 * @param string $modelClassName
317 */
318 public function constructSetsObjectTypeFromClassName($repositoryClassName, $modelClassName) {
319 $repositoryClassNameWithNS = __NAMESPACE__ . '\\' . $repositoryClassName;
320 eval('namespace ' . __NAMESPACE__ . '; class ' . $repositoryClassName . ' extends \\TYPO3\\CMS\\Extbase\\Persistence\\Repository {
321 protected function getRepositoryClassName() {
322 return \'' . $repositoryClassName . '\';
323 }
324 public function _getObjectType() {
325 return $this->objectType;
326 }
327 }');
328 $this->repository = new $repositoryClassNameWithNS($this->mockObjectManager);
329 $this->assertEquals($modelClassName, $this->repository->_getObjectType());
330 }
331
332 /**
333 * @test
334 */
335 public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings() {
336 $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('foo');
337 $mockDefaultQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
338 $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
339 $query = $this->repository->createQuery();
340 $instanceQuerySettings = $query->getQuerySettings();
341 $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
342 $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
343 }
344
345 /**
346 * @test
347 */
348 public function findByUidReturnsResultOfGetObjectByIdentifierCall() {
349 $fakeUid = '123';
350 $object = new \stdClass();
351 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('findByIdentifier'), array($this->mockObjectManager));
352 $expectedResult = $object;
353 $repository->expects($this->once())->method('findByIdentifier')->will($this->returnValue($object));
354 $actualResult = $repository->findByUid($fakeUid);
355 $this->assertSame($expectedResult, $actualResult);
356 }
357
358 /**
359 * @test
360 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
361 */
362 public function updateRejectsObjectsOfWrongType() {
363 $this->repository->_set('objectType', 'Foo');
364 $this->repository->update(new \stdClass());
365 }
366
367 /**
368 * @test
369 */
370 public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult() {
371 $queryResultArray = array(
372 0 => array(
373 'foo' => 'bar',
374 ),
375 );
376 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
377 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
378 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
379 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
380 $this->assertSame(array('foo' => 'bar'), $this->repository->findOneByFoo('bar'));
381 }
382
383 /**
384 * @test
385 */
386 public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult() {
387 $queryResultArray = array();
388 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
389 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
390 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
391 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
392 $this->assertNull($this->repository->findOneByFoo('bar'));
393 }
394
395 }
396
397 ?>