‪TYPO3CMS  ‪main
RepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\MockObject\MockObject;
38 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
39 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
40 
41 final class ‪RepositoryTest extends UnitTestCase
42 {
43  protected ‪Repository&MockObject&AccessibleObjectInterface ‪$repository;
44 
48  protected ‪$mockQueryFactory;
49 
53  protected ‪$mockBackend;
54 
58  protected ‪$mockSession;
59 
64 
68  protected ‪$mockQuery;
69 
74 
79 
80  protected function ‪setUp(): void
81  {
82  parent::setUp();
83  $this->mockQueryFactory = $this->createMock(QueryFactory::class);
84  $this->mockQuery = $this->createMock(QueryInterface::class);
85  $this->mockQuerySettings = $this->createMock(QuerySettingsInterface::class);
86  $this->mockQuery->method('getQuerySettings')->willReturn($this->mockQuerySettings);
87  $this->mockQueryFactory->method('create')->willReturn($this->mockQuery);
88  $this->mockSession = $this->createMock(Session::class);
89  $this->mockConfigurationManager = $this->createMock(ConfigurationManager::class);
90  $this->mockBackend = $this->getAccessibleMock(Backend::class, null, [$this->mockConfigurationManager], '', false);
91  $this->mockBackend->_set('session', $this->mockSession);
92  $this->mockPersistenceManager = $this->getAccessibleMock(
93  PersistenceManager::class,
94  ['createQueryForType'],
95  [
96  $this->mockQueryFactory,
97  $this->mockBackend,
98  $this->mockSession,
99  ]
100  );
101  $this->mockBackend->setPersistenceManager($this->mockPersistenceManager);
102  $this->mockPersistenceManager->method('createQueryForType')->willReturn($this->mockQuery);
103  $this->repository = $this->getAccessibleMock(Repository::class, null);
104  $this->repository->injectPersistenceManager($this->mockPersistenceManager);
105  }
106 
111  {
112  self::assertInstanceOf(RepositoryInterface::class, $this->repository);
113  }
114 
119  {
120  ‪$mockPersistenceManager = $this->createMock(PersistenceManager::class);
121  ‪$mockPersistenceManager->expects(self::once())->method('createQueryForType')->with('ExpectedType');
122 
123  $this->repository->_set('objectType', 'ExpectedType');
124  $this->repository->injectPersistenceManager(‪$mockPersistenceManager);
125 
126  $this->repository->createQuery();
127  }
128 
132  public function ‪createQuerySetsDefaultOrderingIfDefined(): void
133  {
134  $orderings = ['foo' => ‪QueryInterface::ORDER_ASCENDING];
135  ‪$mockQuery = $this->createMock(QueryInterface::class);
136  ‪$mockQuery->expects(self::once())->method('setOrderings')->with($orderings);
137  ‪$mockPersistenceManager = $this->createMock(PersistenceManager::class);
138  ‪$mockPersistenceManager->expects(self::exactly(2))->method('createQueryForType')->with('ExpectedType')->willReturn(‪$mockQuery);
139 
140  $this->repository->_set('objectType', 'ExpectedType');
141  $this->repository->injectPersistenceManager(‪$mockPersistenceManager);
142  $this->repository->setDefaultOrderings($orderings);
143  $this->repository->createQuery();
144 
145  $this->repository->setDefaultOrderings([]);
146  $this->repository->createQuery();
147  }
148 
153  {
154  $expectedResult = $this->createMock(QueryResultInterface::class);
155 
156  ‪$mockQuery = $this->createMock(QueryInterface::class);
157  ‪$mockQuery->expects(self::once())->method('execute')->with()->willReturn($expectedResult);
158 
159  ‪$repository = $this->getMockBuilder(Repository::class)
160  ->onlyMethods(['createQuery'])
161  ->getMock();
162  ‪$repository->expects(self::once())->method('createQuery')->willReturn(‪$mockQuery);
163 
164  self::assertSame($expectedResult, ‪$repository->‪findAll());
165  }
166 
171  {
172  ‪$identifier = '42';
173  $object = new \stdClass();
174 
175  $expectedResult = $this->createMock(QueryResultInterface::class);
176  $expectedResult->expects(self::once())->method('getFirst')->willReturn($object);
177 
178  $this->mockQuery->method('getQuerySettings')->willReturn($this->mockQuerySettings);
179  $this->mockQuery->expects(self::once())->method('matching')->willReturn($this->mockQuery);
180  $this->mockQuery->expects(self::once())->method('execute')->willReturn($expectedResult);
181 
182  // skip backend, as we want to test the backend
183  $this->mockSession->method('hasIdentifier')->willReturn(false);
184  self::assertSame($object, $this->repository->findByIdentifier(‪$identifier));
185  }
186 
190  public function ‪addDelegatesToPersistenceManager(): void
191  {
192  $object = new \stdClass();
193  ‪$mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
194  ‪$mockPersistenceManager->expects(self::once())->method('add')->with($object);
195  $this->repository->injectPersistenceManager(‪$mockPersistenceManager);
196  $this->repository->_set('objectType', get_class($object));
197  $this->repository->‪add($object);
198  }
199 
203  public function ‪removeDelegatesToPersistenceManager(): void
204  {
205  $object = new \stdClass();
206  ‪$mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
207  ‪$mockPersistenceManager->expects(self::once())->method('remove')->with($object);
208  $this->repository->injectPersistenceManager(‪$mockPersistenceManager);
209  $this->repository->_set('objectType', get_class($object));
210  $this->repository->‪remove($object);
211  }
212 
216  public function ‪updateDelegatesToPersistenceManager(): void
217  {
218  $object = new \stdClass();
219  ‪$mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
220  ‪$mockPersistenceManager->expects(self::once())->method('update')->with($object);
221  $this->repository->injectPersistenceManager(‪$mockPersistenceManager);
222  $this->repository->_set('objectType', get_class($object));
223  $this->repository->‪update($object);
224  }
225 
230  {
231  $this->expectException(UnsupportedMethodException::class);
232  $this->expectExceptionCode(1233180480);
233  ‪$repository = $this->getMockBuilder(Repository::class)
234  ->onlyMethods(['createQuery'])
235  ->getMock();
236  ‪$repository->‪__call('foo', []);
237  }
238 
242  public function ‪addChecksObjectType(): void
243  {
244  $this->expectException(IllegalObjectTypeException::class);
245  $this->expectExceptionCode(1248363335);
246  $this->repository->_set('objectType', 'ExpectedObjectType');
247  $this->repository->add(new \stdClass());
248  }
249 
253  public function ‪removeChecksObjectType(): void
254  {
255  $this->expectException(IllegalObjectTypeException::class);
256  $this->expectExceptionCode(1248363336);
257  $this->repository->_set('objectType', 'ExpectedObjectType');
258  $this->repository->remove(new \stdClass());
259  }
260 
264  public function ‪updateChecksObjectType(): void
265  {
266  $this->expectException(IllegalObjectTypeException::class);
267  $this->expectExceptionCode(1249479625);
268  ‪$repository = $this->getAccessibleMock(Repository::class, null);
269  ‪$repository->_set('objectType', 'ExpectedObjectType');
270 
271  ‪$repository->‪update(new \stdClass());
272  }
273 
277  public function ‪constructSetsObjectTypeFromClassName(): void
278  {
280  $reflectionClass = new \ReflectionClass(‪$repository);
281  $reflectionProperty = $reflectionClass->getProperty('objectType');
282  $objectType = $reflectionProperty->getValue(‪$repository);
283 
284  self::assertEquals(Entity::class, $objectType);
285  }
286 
291  {
292  $this->mockQuery = $this->createMock(Query::class);
293  $mockDefaultQuerySettings = $this->createMock(QuerySettingsInterface::class);
294  $this->repository->setDefaultQuerySettings($mockDefaultQuerySettings);
295  $query = $this->repository->createQuery();
296  $instanceQuerySettings = $query->getQuerySettings();
297  self::assertEquals($mockDefaultQuerySettings, $instanceQuerySettings);
298  self::assertNotSame($mockDefaultQuerySettings, $instanceQuerySettings);
299  }
300 
305  {
306  $fakeUid = '123';
307  $object = new \stdClass();
308  ‪$repository = $this->getMockBuilder(Repository::class)
309  ->onlyMethods(['findByIdentifier'])
310  ->getMock();
311  $expectedResult = $object;
312  ‪$repository->expects(self::once())->method('findByIdentifier')->willReturn($object);
313  $actualResult = ‪$repository->‪findByUid($fakeUid);
314  self::assertSame($expectedResult, $actualResult);
315  }
316 
320  public function ‪updateRejectsObjectsOfWrongType(): void
321  {
322  $this->expectException(IllegalObjectTypeException::class);
323  $this->expectExceptionCode(1249479625);
324  $this->repository->_set('objectType', 'Foo');
325  $this->repository->update(new \stdClass());
326  }
327 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Backend
Definition: Backend.php:53
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\removeDelegatesToPersistenceManager
‪removeDelegatesToPersistenceManager()
Definition: RepositoryTest.php:196
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\abstractRepositoryImplementsRepositoryInterface
‪abstractRepositoryImplementsRepositoryInterface()
Definition: RepositoryTest.php:103
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\removeChecksObjectType
‪removeChecksObjectType()
Definition: RepositoryTest.php:246
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory
Definition: QueryFactory.php:32
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled
‪magicCallMethodTriggersAnErrorIfUnknownMethodsAreCalled()
Definition: RepositoryTest.php:222
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$repository
‪Repository &MockObject &AccessibleObjectInterface $repository
Definition: RepositoryTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\updateChecksObjectType
‪updateChecksObjectType()
Definition: RepositoryTest.php:257
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings
‪createQueryReturnsQueryWithUnmodifiedDefaultQuerySettings()
Definition: RepositoryTest.php:283
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockQueryFactory
‪QueryFactory $mockQueryFactory
Definition: RepositoryTest.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Domain\Repository\EntityRepository
Definition: EntityRepository.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\findByUid
‪object null findByUid($uid)
Definition: Repository.php:149
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockQuery
‪QueryInterface $mockQuery
Definition: RepositoryTest.php:63
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockBackend
‪BackendInterface $mockBackend
Definition: RepositoryTest.php:51
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\findByUidReturnsResultOfGetObjectByIdentifierCall
‪findByUidReturnsResultOfGetObjectByIdentifierCall()
Definition: RepositoryTest.php:297
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
Definition: UnsupportedMethodException.php:25
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\constructSetsObjectTypeFromClassName
‪constructSetsObjectTypeFromClassName()
Definition: RepositoryTest.php:270
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: QueryInterface.php:99
‪TYPO3\CMS\Extbase\Persistence\RepositoryInterface
Definition: RepositoryInterface.php:25
‪TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
Definition: BackendInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
Definition: PersistenceManager.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockSession
‪Session $mockSession
Definition: RepositoryTest.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\Session
Definition: Session.php:27
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:30
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\setUp
‪setUp()
Definition: RepositoryTest.php:73
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\findAllCreatesQueryAndReturnsResultOfExecuteCall
‪findAllCreatesQueryAndReturnsResultOfExecuteCall()
Definition: RepositoryTest.php:145
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\updateRejectsObjectsOfWrongType
‪updateRejectsObjectsOfWrongType()
Definition: RepositoryTest.php:313
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\createQuerySetsDefaultOrderingIfDefined
‪createQuerySetsDefaultOrderingIfDefined()
Definition: RepositoryTest.php:125
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\addDelegatesToPersistenceManager
‪addDelegatesToPersistenceManager()
Definition: RepositoryTest.php:183
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\createQueryCallsPersistenceManagerWithExpectedClassName
‪createQueryCallsPersistenceManagerWithExpectedClassName()
Definition: RepositoryTest.php:111
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockPersistenceManager
‪PersistenceManagerInterface $mockPersistenceManager
Definition: RepositoryTest.php:59
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\addChecksObjectType
‪addChecksObjectType()
Definition: RepositoryTest.php:235
‪TYPO3\CMS\Extbase\Persistence\Repository\__call
‪mixed __call($methodName, $arguments)
Definition: Repository.php:224
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockConfigurationManager
‪ConfigurationManager $mockConfigurationManager
Definition: RepositoryTest.php:71
‪TYPO3\CMS\Extbase\Persistence\Repository\update
‪update($modifiedObject)
Definition: Repository.php:102
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence
Definition: ClassesConfigurationFactoryTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Repository\findAll
‪QueryResultInterface array findAll()
Definition: Repository.php:116
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:41
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface\add
‪add(object $object)
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\updateDelegatesToPersistenceManager
‪updateDelegatesToPersistenceManager()
Definition: RepositoryTest.php:209
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\$mockQuerySettings
‪QuerySettingsInterface $mockQuerySettings
Definition: RepositoryTest.php:67
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Domain\Model\Entity
Definition: Entity.php:22
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface\update
‪update(object $object)
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest\findByIdentifierReturnsResultOfGetObjectByIdentifierCallFromBackend
‪findByIdentifierReturnsResultOfGetObjectByIdentifierCallFromBackend()
Definition: RepositoryTest.php:163
‪TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
Definition: IllegalObjectTypeException.php:25
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface\remove
‪remove(object $object)
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\RepositoryTest
Definition: RepositoryTest.php:42