2 declare(strict_types = 1);
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
73 protected function setUp()
75 $this->mockQueryFactory = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\Generic\QueryFactory::class);
76 $this->mockQuery = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryInterface::class);
77 $this->mockQuerySettings = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\Generic\QuerySettingsInterface::class);
78 $this->mockQuery->expects($this->any())->method(
'getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
79 $this->mockQueryFactory->expects($this->any())->method(
'create')->will($this->returnValue($this->mockQuery));
80 $this->mockSession = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\Generic\Session::class);
81 $this->mockConfigurationManager = $this->createMock(\
TYPO3\CMS\
Extbase\Configuration\ConfigurationManager::class);
82 $this->mockBackend = $this->getAccessibleMock(Backend::class, [
'dummy'], [$this->mockConfigurationManager],
'',
false);
83 $this->inject($this->mockBackend,
'session', $this->mockSession);
84 $this->mockPersistenceManager = $this->getAccessibleMock(\
TYPO3\CMS\
Extbase\Persistence\Generic\PersistenceManager::class, [
'createQueryForType']);
85 $this->inject($this->mockBackend,
'persistenceManager', $this->mockPersistenceManager);
86 $this->inject($this->mockPersistenceManager,
'persistenceSession', $this->mockSession);
87 $this->inject($this->mockPersistenceManager,
'backend', $this->mockBackend);
88 $this->mockPersistenceManager->expects($this->any())->method(
'createQueryForType')->will($this->returnValue($this->mockQuery));
89 $this->mockObjectManager = $this->createMock(\
TYPO3\CMS\
Extbase\Object\ObjectManagerInterface::class);
90 $this->repository = $this->getAccessibleMock(\
TYPO3\CMS\
Extbase\Persistence\Repository::class, [
'dummy'], [$this->mockObjectManager]);
91 $this->repository->_set(
'persistenceManager', $this->mockPersistenceManager);
99 $this->assertTrue($this->repository instanceof \
TYPO3\CMS\
Extbase\Persistence\RepositoryInterface);
110 $this->repository->_set(
'objectType',
'ExpectedType');
113 $this->repository->createQuery();
123 $mockQuery->expects($this->once())->method(
'setOrderings')->with($orderings);
127 $this->repository->_set(
'objectType',
'ExpectedType');
129 $this->repository->setDefaultOrderings($orderings);
130 $this->repository->createQuery();
132 $this->repository->setDefaultOrderings([]);
133 $this->repository->createQuery();
141 $expectedResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
144 $mockQuery->expects($this->once())->method(
'execute')->with()->will($this->returnValue($expectedResult));
147 ->setMethods([
'createQuery'])
148 ->setConstructorArgs([$this->mockObjectManager])
152 $this->assertSame($expectedResult,
$repository->findAll());
161 $object = new \stdClass();
163 $expectedResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
164 $expectedResult->expects($this->once())->method(
'getFirst')->will($this->returnValue($object));
166 $this->mockQuery->expects($this->any())->method(
'getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
167 $this->mockQuery->expects($this->once())->method(
'matching')->will($this->returnValue($this->mockQuery));
168 $this->mockQuery->expects($this->once())->method(
'execute')->will($this->returnValue($expectedResult));
171 $this->mockSession->expects($this->any())->method(
'hasIdentifier')->will($this->returnValue(
false));
172 $this->assertSame($object, $this->repository->findByIdentifier($identifier));
180 $object = new \stdClass();
184 $this->repository->_set(
'objectType', get_class($object));
185 $this->repository->add($object);
193 $object = new \stdClass();
197 $this->repository->_set(
'objectType', get_class($object));
198 $this->repository->remove($object);
206 $object = new \stdClass();
210 $this->repository->_set(
'objectType', get_class($object));
211 $this->repository->update($object);
219 $mockQueryResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
221 $mockQuery->expects($this->once())->method(
'equals')->with(
'foo',
'bar')->will($this->returnValue(
'matchCriteria'));
222 $mockQuery->expects($this->once())->method(
'matching')->with(
'matchCriteria')->will($this->returnValue(
$mockQuery));
223 $mockQuery->expects($this->once())->method(
'execute')->with()->will($this->returnValue($mockQueryResult));
226 ->setMethods([
'createQuery'])
227 ->setConstructorArgs([$this->mockObjectManager])
231 $this->assertSame($mockQueryResult,
$repository->findByFoo(
'bar'));
239 $object = new \stdClass();
240 $mockQueryResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
241 $mockQueryResult->expects($this->once())->method(
'getFirst')->will($this->returnValue($object));
243 $mockQuery->expects($this->once())->method(
'equals')->with(
'foo',
'bar')->will($this->returnValue(
'matchCriteria'));
244 $mockQuery->expects($this->once())->method(
'matching')->with(
'matchCriteria')->will($this->returnValue(
$mockQuery));
246 $mockQuery->expects($this->once())->method(
'execute')->will($this->returnValue($mockQueryResult));
249 ->setMethods([
'createQuery'])
250 ->setConstructorArgs([$this->mockObjectManager])
254 $this->assertSame($object,
$repository->findOneByFoo(
'bar'));
263 $mockQueryResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
264 $mockQuery->expects($this->once())->method(
'equals')->with(
'foo',
'bar')->will($this->returnValue(
'matchCriteria'));
265 $mockQuery->expects($this->once())->method(
'matching')->with(
'matchCriteria')->will($this->returnValue(
$mockQuery));
266 $mockQuery->expects($this->once())->method(
'execute')->will($this->returnValue($mockQueryResult));
267 $mockQueryResult->expects($this->once())->method(
'count')->will($this->returnValue(2));
270 ->setMethods([
'createQuery'])
271 ->setConstructorArgs([$this->mockObjectManager])
283 $this->expectException(UnsupportedMethodException::class);
284 $this->expectExceptionCode(1233180480);
286 ->setMethods([
'createQuery'])
287 ->setConstructorArgs([$this->mockObjectManager])
297 $this->expectException(IllegalObjectTypeException::class);
298 $this->expectExceptionCode(1248363335);
299 $this->repository->_set(
'objectType',
'ExpectedObjectType');
300 $this->repository->add(
new \stdClass());
308 $this->expectException(IllegalObjectTypeException::class);
309 $this->expectExceptionCode(1248363336);
310 $this->repository->_set(
'objectType',
'ExpectedObjectType');
311 $this->repository->remove(
new \stdClass());
319 $this->expectException(IllegalObjectTypeException::class);
320 $this->expectExceptionCode(1249479625);
335 $reflectionProperty = $reflectionClass->getProperty(
'objectType');
336 $reflectionProperty->setAccessible(
true);
337 $objectType = $reflectionProperty->getValue(
$repository);
339 $this->assertEquals(Fixture\Domain\Model\Entity::class, $objectType);
347 $this->mockQuery = new \TYPO3\CMS\Extbase\Persistence\Generic\Query(
'foo');
348 $mockDefaultQuerySettings = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\Generic\QuerySettingsInterface::class);
349 $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
350 $query = $this->repository->createQuery();
351 $instanceQuerySettings = $query->getQuerySettings();
352 $this->assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
353 $this->assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
362 $object = new \stdClass();
364 ->setMethods([
'findByIdentifier'])
365 ->setConstructorArgs([$this->mockObjectManager])
367 $expectedResult = $object;
368 $repository->expects($this->once())->method(
'findByIdentifier')->will($this->returnValue($object));
370 $this->assertSame($expectedResult, $actualResult);
378 $this->expectException(IllegalObjectTypeException::class);
379 $this->expectExceptionCode(1249479625);
380 $this->repository->_set(
'objectType',
'Foo');
381 $this->repository->update(
new \stdClass());
389 $queryResultArray = [
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->assertSame([
'foo' =>
'bar'], $this->repository->findOneByFoo(
'bar'));
406 $queryResultArray = [];
407 $this->mockQuery->expects($this->once())->method(
'equals')->with(
'foo',
'bar')->will($this->returnValue(
'matchCriteria'));
408 $this->mockQuery->expects($this->once())->method(
'matching')->with(
'matchCriteria')->will($this->returnValue($this->mockQuery));
409 $this->mockQuery->expects($this->once())->method(
'setLimit')->with(1)->will($this->returnValue($this->mockQuery));
410 $this->mockQuery->expects($this->once())->method(
'execute')->will($this->returnValue($queryResultArray));
411 $this->assertNull($this->repository->findOneByFoo(
'bar'));