[TASK] Replace inject methods with @inject
[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 $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 $expectedResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
158 $expectedResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
159
160 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
161 $mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
162 $mockQuery->expects($this->once())->method('matching')->will($this->returnValue($mockQuery));
163 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
164
165 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
166 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
167
168 $this->assertSame($object, $repository->findByIdentifier($identifier));
169 }
170
171 /**
172 * @test
173 */
174 public function addDelegatesToPersistenceManager() {
175 $object = new \stdClass();
176 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
177 $mockPersistenceManager->expects($this->once())->method('add')->with($object);
178 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
179 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
180 $repository->_set('objectType', get_class($object));
181 $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 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
192 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
193 $repository->_set('objectType', get_class($object));
194 $repository->remove($object);
195 }
196
197 /**
198 * @test
199 */
200 public function updateDelegatesToPersistenceManager() {
201 $object = new \stdClass();
202 $mockPersistenceManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface');
203 $mockPersistenceManager->expects($this->once())->method('update')->with($object);
204 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
205 $this->inject($repository, 'persistenceManager', $mockPersistenceManager);
206 $repository->_set('objectType', get_class($object));
207 $repository->update($object);
208 }
209
210 /**
211 * @test
212 */
213 public function magicCallMethodAcceptsFindBySomethingCallsAndExecutesAQueryWithThatCriteria() {
214 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
215 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
216 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
217 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
218 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($mockQueryResult));
219
220 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
221 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
222
223 $this->assertSame($mockQueryResult, $repository->findByFoo('bar'));
224 }
225
226 /**
227 * @test
228 */
229 public function magicCallMethodAcceptsFindOneBySomethingCallsAndExecutesAQueryWithThatCriteria() {
230 $object = new \stdClass();
231 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
232 $mockQueryResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
233 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
234 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
235 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
236 $mockQuery->expects($this->once())->method('setLimit')->will($this->returnValue($mockQuery));
237 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
238
239 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
240 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
241
242 $this->assertSame($object, $repository->findOneByFoo('bar'));
243 }
244
245 /**
246 * @test
247 */
248 public function magicCallMethodAcceptsCountBySomethingCallsAndExecutesAQueryWithThatCriteria() {
249 $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface');
250 $mockQueryResult = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryResultInterface');
251 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
252 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
253 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
254 $mockQueryResult->expects($this->once())->method('count')->will($this->returnValue(2));
255
256 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
257 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
258
259 $this->assertSame(2, $repository->countByFoo('bar'));
260 }
261
262 /**
263 * @test
264 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
265 */
266 public function magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled() {
267 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('createQuery'));
268 $repository->__call('foo', array());
269 }
270
271 /**
272 * @test
273 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
274 */
275 public function addChecksObjectType() {
276 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
277 $repository->_set('objectType', 'ExpectedObjectType');
278
279 $repository->add(new \stdClass());
280 }
281
282 /**
283 * @test
284 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
285 */
286 public function removeChecksObjectType() {
287 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
288 $repository->_set('objectType', 'ExpectedObjectType');
289
290 $repository->remove(new \stdClass());
291 }
292 /**
293 * @test
294 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
295 */
296 public function updateChecksObjectType() {
297 $repository = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Repository', array('dummy'));
298 $repository->_set('objectType', 'ExpectedObjectType');
299
300 $repository->update(new \stdClass());
301 }
302
303 /**
304 * dataProvider for createQueryCallsQueryFactoryWithExpectedType
305 *
306 * @return array
307 */
308 public function modelAndRepositoryClassNames() {
309 return array(
310 array('Tx_BlogExample_Domain_Repository_BlogRepository', 'Tx_BlogExample_Domain_Model_Blog'),
311 array('_Domain_Repository_Content_PageRepository', '_Domain_Model_Content_Page'),
312 array('Tx_RepositoryExample_Domain_Repository_SomeModelRepository', 'Tx_RepositoryExample_Domain_Model_SomeModel'),
313 array('Tx_RepositoryExample_Domain_Repository_RepositoryRepository', 'Tx_RepositoryExample_Domain_Model_Repository'),
314 array('Tx_Repository_Domain_Repository_RepositoryRepository', 'Tx_Repository_Domain_Model_Repository')
315 );
316 }
317
318 /**
319 * @test
320 * @dataProvider modelAndRepositoryClassNames
321 * @param string $repositoryClassName
322 * @param string $modelClassName
323 */
324 public function constructSetsObjectTypeFromClassName($repositoryClassName, $modelClassName) {
325 $mockClassName = 'MockRepository' . uniqid();
326 eval('class ' . $mockClassName . ' extends TYPO3\\CMS\\Extbase\\Persistence\\Repository {
327 protected function getRepositoryClassName() {
328 return \'' . $repositoryClassName . '\';
329 }
330 public function _getObjectType() {
331 return $this->objectType;
332 }
333 }');
334 $this->repository = new $mockClassName($this->mockObjectManager);
335 $this->assertEquals($modelClassName, $this->repository->_getObjectType());
336 }
337
338 /**
339 * @test
340 */
341 public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings() {
342 $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('foo');
343 $mockDefaultQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
344 $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
345 $query = $this->repository->createQuery();
346 $instanceQuerySettings = $query->getQuerySettings();
347 $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
348 $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
349 }
350
351 /**
352 * @test
353 */
354 public function findByUidReturnsResultOfGetObjectByIdentifierCall() {
355 $fakeUid = '123';
356 $object = new \stdClass();
357 $repository = $this->getMock('TYPO3\CMS\Extbase\Persistence\Repository', array('findByIdentifier'));
358 $expectedResult = $object;
359 $repository->expects($this->once())->method('findByIdentifier')->will($this->returnValue($object));
360 $actualResult = $repository->findByUid($fakeUid);
361 $this->assertSame($expectedResult, $actualResult);
362 }
363
364 /**
365 * @test
366 * @expectedException \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
367 */
368 public function updateRejectsObjectsOfWrongType() {
369 $this->repository->_set('objectType', 'Foo');
370 $this->repository->update(new \stdClass());
371 }
372
373 /**
374 * @test
375 */
376 public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult() {
377 $queryResultArray = array(
378 0 => array(
379 'foo' => 'bar',
380 ),
381 );
382 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
383 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
384 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
385 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
386 $this->assertSame(array('foo' => 'bar'), $this->repository->findOneByFoo('bar'));
387 }
388
389 /**
390 * @test
391 */
392 public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult() {
393 $queryResultArray = array();
394 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
395 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
396 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
397 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
398 $this->assertNull($this->repository->findOneByFoo('bar'));
399 }
400
401 }
402
403 ?>