TYPO3 CMS  TYPO3_7-6
RepositoryTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $repository;
26 
30  protected $mockObjectManager;
31 
35  protected $mockQueryFactory;
36 
40  protected $mockBackend;
41 
45  protected $mockSession;
46 
51 
55  protected $mockQuery;
56 
60  protected $mockQuerySettings;
61 
66 
67  protected function setUp()
68  {
69  $this->mockQueryFactory = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory::class);
70  $this->mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
71  $this->mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
72  $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
73  $this->mockQueryFactory->expects($this->any())->method('create')->will($this->returnValue($this->mockQuery));
74  $this->mockSession = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
75  $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
76  $this->mockBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Backend::class, ['dummy'], [$this->mockConfigurationManager]);
77  $this->inject($this->mockBackend, 'session', $this->mockSession);
78  $this->mockPersistenceManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class, ['createQueryForType']);
79  $this->inject($this->mockBackend, 'persistenceManager', $this->mockPersistenceManager);
80  $this->inject($this->mockPersistenceManager, 'persistenceSession', $this->mockSession);
81  $this->inject($this->mockPersistenceManager, 'backend', $this->mockBackend);
82  $this->mockPersistenceManager->expects($this->any())->method('createQueryForType')->will($this->returnValue($this->mockQuery));
83  $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
84  $this->repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['dummy'], [$this->mockObjectManager]);
85  $this->repository->_set('persistenceManager', $this->mockPersistenceManager);
86  }
87 
92  {
93  $this->assertTrue($this->repository instanceof \TYPO3\CMS\Extbase\Persistence\RepositoryInterface);
94  }
95 
100  {
101  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
102  $mockPersistenceManager->expects($this->once())->method('createQueryForType')->with('ExpectedType');
103 
104  $this->repository->_set('objectType', 'ExpectedType');
105  $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
106 
107  $this->repository->createQuery();
108  }
109 
114  {
116  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
117  $mockQuery->expects($this->once())->method('setOrderings')->with($orderings);
118  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
119  $mockPersistenceManager->expects($this->exactly(2))->method('createQueryForType')->with('ExpectedType')->will($this->returnValue($mockQuery));
120 
121  $this->repository->_set('objectType', 'ExpectedType');
122  $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
123  $this->repository->setDefaultOrderings($orderings);
124  $this->repository->createQuery();
125 
126  $this->repository->setDefaultOrderings([]);
127  $this->repository->createQuery();
128  }
129 
134  {
135  $expectedResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
136 
137  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
138  $mockQuery->expects($this->once())->method('execute')->with()->will($this->returnValue($expectedResult));
139 
140  $repository = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['createQuery'], [$this->mockObjectManager]);
141  $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
142 
143  $this->assertSame($expectedResult, $repository->findAll());
144  }
145 
150  {
151  $identifier = '42';
152  $object = new \stdClass();
153 
154  $expectedResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
155  $expectedResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
156 
157  $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
158  $this->mockQuery->expects($this->once())->method('matching')->will($this->returnValue($this->mockQuery));
159  $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($expectedResult));
160 
161  // skip backend, as we want to test the backend
162  $this->mockSession->expects($this->any())->method('hasIdentifier')->will($this->returnValue(false));
163  $this->assertSame($object, $this->repository->findByIdentifier($identifier));
164  }
165 
170  {
171  $object = new \stdClass();
172  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
173  $mockPersistenceManager->expects($this->once())->method('add')->with($object);
174  $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
175  $this->repository->_set('objectType', get_class($object));
176  $this->repository->add($object);
177  }
178 
183  {
184  $object = new \stdClass();
185  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
186  $mockPersistenceManager->expects($this->once())->method('remove')->with($object);
187  $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
188  $this->repository->_set('objectType', get_class($object));
189  $this->repository->remove($object);
190  }
191 
196  {
197  $object = new \stdClass();
198  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
199  $mockPersistenceManager->expects($this->once())->method('update')->with($object);
200  $this->inject($this->repository, 'persistenceManager', $mockPersistenceManager);
201  $this->repository->_set('objectType', get_class($object));
202  $this->repository->update($object);
203  }
204 
209  {
210  $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
211  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
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::class, ['createQuery'], [$this->mockObjectManager]);
217  $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
218 
219  $this->assertSame($mockQueryResult, $repository->findByFoo('bar'));
220  }
221 
226  {
227  $object = new \stdClass();
228  $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
229  $mockQueryResult->expects($this->once())->method('getFirst')->will($this->returnValue($object));
230  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
231  $mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
232  $mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($mockQuery));
233  $mockQuery->expects($this->once())->method('setLimit')->will($this->returnValue($mockQuery));
234  $mockQuery->expects($this->once())->method('execute')->will($this->returnValue($mockQueryResult));
235 
236  $repository = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['createQuery'], [$this->mockObjectManager]);
237  $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
238 
239  $this->assertSame($object, $repository->findOneByFoo('bar'));
240  }
241 
246  {
247  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
248  $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
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::class, ['createQuery'], [$this->mockObjectManager]);
255  $repository->expects($this->once())->method('createQuery')->will($this->returnValue($mockQuery));
256 
257  $this->assertSame(2, $repository->countByFoo('bar'));
258  }
259 
265  {
266  $repository = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['createQuery'], [$this->mockObjectManager]);
267  $repository->__call('foo', []);
268  }
269 
274  public function addChecksObjectType()
275  {
276  $this->repository->_set('objectType', 'ExpectedObjectType');
277  $this->repository->add(new \stdClass());
278  }
279 
284  public function removeChecksObjectType()
285  {
286  $this->repository->_set('objectType', 'ExpectedObjectType');
287  $this->repository->remove(new \stdClass());
288  }
293  public function updateChecksObjectType()
294  {
295  $repository = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['dummy'], [$this->mockObjectManager]);
296  $repository->_set('objectType', 'ExpectedObjectType');
297 
298  $repository->update(new \stdClass());
299  }
300 
307  {
308  return [
309  ['Tx_BlogExample_Domain_Repository_BlogRepository', 'Tx_BlogExample_Domain_Model_Blog'],
310  ['_Domain_Repository_Content_PageRepository', '_Domain_Model_Content_Page'],
311  ['Tx_RepositoryExample_Domain_Repository_SomeModelRepository', 'Tx_RepositoryExample_Domain_Model_SomeModel'],
312  ['Tx_RepositoryExample_Domain_Repository_RepositoryRepository', 'Tx_RepositoryExample_Domain_Model_Repository'],
313  ['Tx_Repository_Domain_Repository_RepositoryRepository', 'Tx_Repository_Domain_Model_Repository']
314  ];
315  }
316 
323  public function constructSetsObjectTypeFromClassName($repositoryClassName, $modelClassName)
324  {
325  $repositoryClassNameWithNS = __NAMESPACE__ . '\\' . $repositoryClassName;
326  eval('namespace ' . __NAMESPACE__ . '; class ' . $repositoryClassName . ' 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 $repositoryClassNameWithNS($this->mockObjectManager);
335  $this->assertEquals($modelClassName, $this->repository->_getObjectType());
336  }
337 
341  public function createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings()
342  {
343  $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('foo');
344  $mockDefaultQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
345  $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
346  $query = $this->repository->createQuery();
347  $instanceQuerySettings = $query->getQuerySettings();
348  $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
349  $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
350  }
351 
355  public function findByUidReturnsResultOfGetObjectByIdentifierCall()
356  {
357  $fakeUid = '123';
358  $object = new \stdClass();
359  $repository = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Repository::class, ['findByIdentifier'], [$this->mockObjectManager]);
360  $expectedResult = $object;
361  $repository->expects($this->once())->method('findByIdentifier')->will($this->returnValue($object));
362  $actualResult = $repository->findByUid($fakeUid);
363  $this->assertSame($expectedResult, $actualResult);
364  }
365 
370  public function updateRejectsObjectsOfWrongType()
371  {
372  $this->repository->_set('objectType', 'Foo');
373  $this->repository->update(new \stdClass());
374  }
375 
379  public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult()
380  {
381  $queryResultArray = [
382  0 => [
383  'foo' => 'bar',
384  ],
385  ];
386  $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
387  $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
388  $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
389  $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
390  $this->assertSame(['foo' => 'bar'], $this->repository->findOneByFoo('bar'));
391  }
392 
396  public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult()
397  {
398  $queryResultArray = [];
399  $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
400  $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
401  $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
402  $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
403  $this->assertNull($this->repository->findOneByFoo('bar'));
404  }
405 }
inject($target, $name, $dependency)
constructSetsObjectTypeFromClassName($repositoryClassName, $modelClassName)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)