‪TYPO3CMS  11.5
AbstractRepositoryTest.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 Doctrine\DBAL\Result;
21 use Prophecy\Argument;
22 use Prophecy\PhpUnit\ProphecyTrait;
26 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪AbstractRepositoryTest extends UnitTestCase
35 {
36  use ProphecyTrait;
37 
41  protected ‪$subject;
42 
43  protected function ‪createDatabaseMock(): \Prophecy\Prophecy\ObjectProphecy
44  {
45  $connectionProphet = $this->prophesize(Connection::class);
46  $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
47 
48  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
49  $queryBuilderProphet->expr()->willReturn(
50  GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
51  );
52 
53  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
54  $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
55  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
56 
57  return $queryBuilderProphet;
58  }
59 
60  protected function ‪setUp(): void
61  {
62  parent::setUp();
63  $this->subject = $this->getMockForAbstractClass(AbstractRepository::class, [], '', false);
64  }
65 
69  public function ‪findByUidFailsIfUidIsString(): void
70  {
71  $this->expectException(\InvalidArgumentException::class);
72  $this->expectExceptionCode(1316779798);
73  $this->subject->findByUid('asdf');
74  }
75 
79  public function ‪findByUidAcceptsNumericUidInString(): void
80  {
81  $statementProphet = $this->prophesize(Result::class);
82  $statementProphet->fetchAssociative()->shouldBeCalled()->willReturn(['uid' => 123]);
83 
84  $queryBuilderProphet = $this->‪createDatabaseMock();
85  $queryBuilderProphet->select('*')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
86  $queryBuilderProphet->from('')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
87  $queryBuilderProphet->where(Argument::cetera())->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
88  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
89  $queryBuilderProphet->executeQuery()->shouldBeCalled()->willReturn($statementProphet->reveal());
90 
91  $this->subject->findByUid('123');
92  }
93 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:36
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:32
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository
Definition: AbstractRepositoryTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest\findByUidAcceptsNumericUidInString
‪findByUidAcceptsNumericUidInString()
Definition: AbstractRepositoryTest.php:77
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest\$subject
‪AbstractRepository $subject
Definition: AbstractRepositoryTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest
Definition: AbstractRepositoryTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest\findByUidFailsIfUidIsString
‪findByUidFailsIfUidIsString()
Definition: AbstractRepositoryTest.php:67
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest\setUp
‪setUp()
Definition: AbstractRepositoryTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Resource\Repository\AbstractRepositoryTest\createDatabaseMock
‪createDatabaseMock()
Definition: AbstractRepositoryTest.php:41