[!!!][TASK] Remove support for non namespaced classes in Extbase
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
18 use TYPO3\CMS\Extbase\Persistence\Generic\Backend;
19 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class RepositoryTest extends UnitTestCase
26 {
27 /**
28 * Subject is not notice free, disable E_NOTICES
29 */
30 protected static $suppressNotices = true;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Persistence\Repository|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
34 */
35 protected $repository;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
39 */
40 protected $mockObjectManager;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory
44 */
45 protected $mockQueryFactory;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
49 */
50 protected $mockBackend;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
54 */
55 protected $mockSession;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
59 */
60 protected $mockPersistenceManager;
61
62 /**
63 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
64 */
65 protected $mockQuery;
66
67 /**
68 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
69 */
70 protected $mockQuerySettings;
71
72 /**
73 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
74 */
75 protected $mockConfigurationManager;
76
77 protected function setUp()
78 {
79 $this->mockQueryFactory = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory::class);
80 $this->mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
81 $this->mockQuerySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
82 $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
83 $this->mockQueryFactory->expects($this->any())->method('create')->will($this->returnValue($this->mockQuery));
84 $this->mockSession = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
85 $this->mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
86 $this->mockBackend = $this->getAccessibleMock(Backend::class, ['dummy'], [$this->mockConfigurationManager], '', false);
87 $this->inject($this->mockBackend, 'session', $this->mockSession);
88 $this->mockPersistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['createQueryForType']);
89 $this->inject($this->mockBackend, 'persistenceManager', $this->mockPersistenceManager);
90 $this->inject($this->mockPersistenceManager, 'persistenceSession', $this->mockSession);
91 $this->inject($this->mockPersistenceManager, 'backend', $this->mockBackend);
92 $this->mockPersistenceManager->expects($this->any())->method('createQueryForType')->will($this->returnValue($this->mockQuery));
93 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
94 $this->repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['dummy'], [$this->mockObjectManager]);
95 $this->repository->_set('persistenceManager', $this->mockPersistenceManager);
96 }
97
98 /**
99 * @test
100 */
101 public function abstractRepositoryImplementsRepositoryInterface()
102 {
103 $this->assertTrue($this->repository instanceof \TYPO3\CMS\Extbase\Persistence\RepositoryInterface);
104 }
105
106 /**
107 * @test
108 */
109 public function createQueryCallsPersistenceManagerWithExpectedClassName()
110 {
111 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
112 $mockPersistenceManager->expects($this->once())->method('createQueryForType')->with('ExpectedType');
113
114 $this->repository->_set('objectType', 'ExpectedType');
115 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
116
117 $this->repository->createQuery();
118 }
119
120 /**
121 * @test
122 */
123 public function createQuerySetsDefaultOrderingIfDefined()
124 {
125 $orderings = ['foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING];
126 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
127 $mockQuery->expects($this->once())->method('setOrderings')->with($orderings);
128 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
129 $mockPersistenceManager->expects($this->exactly(2))->method('createQueryForType')->with('ExpectedType')->will($this->returnValue($mockQuery));
130
131 $this->repository->_set('objectType', 'ExpectedType');
132 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
133 $this->repository->setDefaultOrderings($orderings);
134 $this->repository->createQuery();
135
136 $this->repository->setDefaultOrderings([]);
137 $this->repository->createQuery();
138 }
139
140 /**
141 * @test
142 */
143 public function findAllCreatesQueryAndReturnsResultOfExecuteCall()
144 {
145 $expectedResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
146
147 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
148 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($expectedResult));
149
150 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
151 ->setMethods(['createQuery'])
152 ->setConstructorArgs([$this->mockObjectManager])
153 ->getMock();
154 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
155
156 $this->assertSame($expectedResult, $repository->findAll());
157 }
158
159 /**
160 * @test
161 */
162 public function findByidentifierReturnsResultOfGetObjectByIdentifierCallFromBackend()
163 {
164 $identifier = '42';
165 $object = new \stdClass();
166
167 $expectedResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
168 $expectedResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
169
170 $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
171 $this->mockQuery->expects($this->once())->method('matching')->will($this->returnValue($this->mockQuery));
172 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
173
174 // skip backend, as we want to test the backend
175 $this->mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(false));
176 $this->assertSame($object, $this->repository->findByIdentifier($identifier));
177 }
178
179 /**
180 * @test
181 */
182 public function addDelegatesToPersistenceManager()
183 {
184 $object = new \stdClass();
185 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
186 $mockPersistenceManager->expects($this->once())->method('add')->with($object);
187 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
188 $this->repository->_set('objectType', get_class($object));
189 $this->repository->add($object);
190 }
191
192 /**
193 * @test
194 */
195 public function removeDelegatesToPersistenceManager()
196 {
197 $object = new \stdClass();
198 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
199 $mockPersistenceManager->expects($this->once())->method('remove')->with($object);
200 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
201 $this->repository->_set('objectType', get_class($object));
202 $this->repository->remove($object);
203 }
204
205 /**
206 * @test
207 */
208 public function updateDelegatesToPersistenceManager()
209 {
210 $object = new \stdClass();
211 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
212 $mockPersistenceManager->expects($this->once())->method('update')->with($object);
213 $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
214 $this->repository->_set('objectType', get_class($object));
215 $this->repository->update($object);
216 }
217
218 /**
219 * @test
220 */
221 public function magicCallMethodAcceptsFindBySomethingCallsAndExecutesAQueryWithThatCriteria()
222 {
223 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
224 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
225 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
226 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
227 $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($mockQueryResult));
228
229 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
230 ->setMethods(['createQuery'])
231 ->setConstructorArgs([$this->mockObjectManager])
232 ->getMock();
233 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
234
235 $this->assertSame($mockQueryResult, $repository->findByFoo('bar'));
236 }
237
238 /**
239 * @test
240 */
241 public function magicCallMethodAcceptsFindOneBySomethingCallsAndExecutesAQueryWithThatCriteria()
242 {
243 $object = new \stdClass();
244 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
245 $mockQueryResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
246 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
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('setLimit')->will($this->returnValue($mockQuery));
250 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
251
252 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
253 ->setMethods(['createQuery'])
254 ->setConstructorArgs([$this->mockObjectManager])
255 ->getMock();
256 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
257
258 $this->assertSame($object, $repository->findOneByFoo('bar'));
259 }
260
261 /**
262 * @test
263 */
264 public function magicCallMethodAcceptsCountBySomethingCallsAndExecutesAQueryWithThatCriteria()
265 {
266 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
267 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
268 $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
269 $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
270 $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
271 $mockQueryResult->expects($this->once())->method('count')->will($this->returnValue(2));
272
273 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
274 ->setMethods(['createQuery'])
275 ->setConstructorArgs([$this->mockObjectManager])
276 ->getMock();
277 $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
278
279 $this->assertSame(2, $repository->countByFoo('bar'));
280 }
281
282 /**
283 * @test
284 */
285 public function magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled()
286 {
287 $this->expectException(UnsupportedMethodException::class);
288 $this->expectExceptionCode(1233180480);
289 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
290 ->setMethods(['createQuery'])
291 ->setConstructorArgs([$this->mockObjectManager])
292 ->getMock();
293 $repository->__call('foo', []);
294 }
295
296 /**
297 * @test
298 */
299 public function addChecksObjectType()
300 {
301 $this->expectException(IllegalObjectTypeException::class);
302 $this->expectExceptionCode(1248363335);
303 $this->repository->_set('objectType', 'ExpectedObjectType');
304 $this->repository->add(new \stdClass());
305 }
306
307 /**
308 * @test
309 */
310 public function removeChecksObjectType()
311 {
312 $this->expectException(IllegalObjectTypeException::class);
313 $this->expectExceptionCode(1248363336);
314 $this->repository->_set('objectType', 'ExpectedObjectType');
315 $this->repository->remove(new \stdClass());
316 }
317
318 /**
319 * @test
320 */
321 public function updateChecksObjectType()
322 {
323 $this->expectException(IllegalObjectTypeException::class);
324 $this->expectExceptionCode(1249479625);
325 $repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['dummy'], [$this->mockObjectManager]);
326 $repository->_set('objectType', 'ExpectedObjectType');
327
328 $repository->update(new \stdClass());
329 }
330
331 /**
332 * @test
333 */
334 public function constructSetsObjectTypeFromClassName()
335 {
336 $repository = new Fixture\Domain\Repository\EntityRepository($this->mockObjectManager);
337
338 $reflectionClass = new \ReflectionClass($repository);
339 $reflectionProperty = $reflectionClass->getProperty('objectType');
340 $reflectionProperty->setAccessible(true);
341 $objectType = $reflectionProperty->getValue($repository);
342
343 $this->assertEquals(Fixture\Domain\Model\Entity::class, $objectType);
344 }
345
346 /**
347 * @test
348 */
349 public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings()
350 {
351 $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('foo');
352 $mockDefaultQuerySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
353 $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
354 $query = $this->repository->createQuery();
355 $instanceQuerySettings = $query->getQuerySettings();
356 $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
357 $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
358 }
359
360 /**
361 * @test
362 */
363 public function findByUidReturnsResultOfGetObjectByIdentifierCall()
364 {
365 $fakeUid = '123';
366 $object = new \stdClass();
367 $repository = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Repository::class)
368 ->setMethods(['findByIdentifier'])
369 ->setConstructorArgs([$this->mockObjectManager])
370 ->getMock();
371 $expectedResult = $object;
372 $repository->expects($this->once())->method('findByIdentifier')->will($this->returnValue($object));
373 $actualResult = $repository->findByUid($fakeUid);
374 $this->assertSame($expectedResult, $actualResult);
375 }
376
377 /**
378 * @test
379 */
380 public function updateRejectsObjectsOfWrongType()
381 {
382 $this->expectException(IllegalObjectTypeException::class);
383 $this->expectExceptionCode(1249479625);
384 $this->repository->_set('objectType', 'Foo');
385 $this->repository->update(new \stdClass());
386 }
387
388 /**
389 * @test
390 */
391 public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult()
392 {
393 $queryResultArray = [
394 0 => [
395 'foo' => 'bar',
396 ],
397 ];
398 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
399 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
400 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
401 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
402 $this->assertSame(['foo' => 'bar'], $this->repository->findOneByFoo('bar'));
403 }
404
405 /**
406 * @test
407 */
408 public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult()
409 {
410 $queryResultArray = [];
411 $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
412 $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
413 $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
414 $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
415 $this->assertNull($this->repository->findOneByFoo('bar'));
416 }
417 }