[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Repository / AbstractRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Repository;
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 use Doctrine\DBAL\Driver\Statement;
17 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
21 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
22 use TYPO3\CMS\Core\Resource\AbstractRepository;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Test case
27 */
28 class AbstractRepositoryTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
29 {
30 /**
31 * @var AbstractRepository
32 */
33 protected $subject;
34
35 protected function createDatabaseMock()
36 {
37 $connectionProphet = $this->prophesize(Connection::class);
38 $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
39
40 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
41 $queryBuilderProphet->expr()->willReturn(
42 GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
43 );
44
45 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
46 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
47 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
48
49 return $queryBuilderProphet;
50 }
51
52 protected function setUp()
53 {
54 $this->subject = $this->getMockForAbstractClass(AbstractRepository::class, [], '', false);
55 }
56
57 /**
58 * @test
59 */
60 public function findByUidFailsIfUidIsString()
61 {
62 $this->expectException(\InvalidArgumentException::class);
63 $this->expectExceptionCode(1316779798);
64 $this->subject->findByUid('asdf');
65 }
66
67 /**
68 * @test
69 */
70 public function findByUidAcceptsNumericUidInString()
71 {
72 $statementProphet = $this->prophesize(Statement::class);
73 $statementProphet->fetch()->shouldBeCalled()->willReturn(['uid' => 123]);
74
75 $queryBuilderProphet = $this->createDatabaseMock();
76 $queryBuilderProphet->select('*')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
77 $queryBuilderProphet->from('')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
78 $queryBuilderProphet->where(Argument::cetera())->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
79 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
80 $queryBuilderProphet->execute()->shouldBeCalled()->willReturn($statementProphet->reveal());
81
82 $this->subject->findByUid('123');
83 }
84 }