Skip to content

Commit

Permalink
[TASK] Replace prophecy in EXT:core AbstractRepositoryTest
Browse files Browse the repository at this point in the history
Resolves: #98741
Releases: main
Change-Id: I2d9499084ca7b1cecab6b05b4edbe69dd6029fcd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76353
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
nhovratov authored and sbuerk committed Nov 2, 2022
1 parent 110f127 commit 74784c0
Showing 1 changed file with 23 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
namespace TYPO3\CMS\Core\Tests\Unit\Resource\Repository;

use Doctrine\DBAL\Result;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use PHPUnit\Framework\MockObject\MockObject;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
Expand All @@ -30,34 +29,21 @@

class AbstractRepositoryTest extends UnitTestCase
{
use ProphecyTrait;

/**
* @var AbstractRepository
*/
protected $subject;

protected function createDatabaseMock(): \Prophecy\Prophecy\ObjectProphecy
protected function createDatabaseMock(): QueryBuilder&MockObject
{
$connectionProphet = $this->prophesize(Connection::class);
$connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
$connectionMock = $this->createMock(Connection::class);
$connectionMock->method('quoteIdentifier')->with(self::anything())->willReturnArgument(0);

$queryBuilderProphet = $this->prophesize(QueryBuilder::class);
$queryBuilderProphet->expr()->willReturn(
GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
$queryBuilderMock = $this->createMock(QueryBuilder::class);
$queryBuilderMock->method('expr')->willReturn(
GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionMock)
);

$connectionPoolProphet = $this->prophesize(ConnectionPool::class);
$connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
$connectionPoolMock = $this->createMock(ConnectionPool::class);
$connectionPoolMock->method('getQueryBuilderForTable')->with(self::anything())->willReturn($queryBuilderMock);
GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);

return $queryBuilderProphet;
}

protected function setUp(): void
{
parent::setUp();
$this->subject = $this->getMockForAbstractClass(AbstractRepository::class, [], '', false);
return $queryBuilderMock;
}

/**
Expand All @@ -67,24 +53,26 @@ public function findByUidFailsIfUidIsString(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1316779798);
$this->subject->findByUid('asdf');
$subject = $this->getMockForAbstractClass(AbstractRepository::class, [], '', false);
$subject->findByUid('asdf');
}

/**
* @test
*/
public function findByUidAcceptsNumericUidInString(): void
{
$statementProphet = $this->prophesize(Result::class);
$statementProphet->fetchAssociative()->shouldBeCalled()->willReturn(['uid' => 123]);
$statementMock = $this->createMock(Result::class);
$statementMock->expects(self::once())->method('fetchAssociative')->willReturn(['uid' => 123]);

$queryBuilderProphet = $this->createDatabaseMock();
$queryBuilderProphet->select('*')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
$queryBuilderProphet->from('')->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
$queryBuilderProphet->where(Argument::cetera())->shouldBeCalled()->willReturn($queryBuilderProphet->reveal());
$queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
$queryBuilderProphet->executeQuery()->shouldBeCalled()->willReturn($statementProphet->reveal());
$queryBuilderMock = $this->createDatabaseMock();
$queryBuilderMock->expects(self::once())->method('select')->with('*')->willReturn($queryBuilderMock);
$queryBuilderMock->expects(self::once())->method('from')->with('')->willReturn($queryBuilderMock);
$queryBuilderMock->expects(self::once())->method('where')->with(self::anything())->willReturn($queryBuilderMock);
$queryBuilderMock->method('createNamedParameter')->with(self::anything())->willReturnArgument(0);
$queryBuilderMock->expects(self::once())->method('executeQuery')->willReturn($statementMock);

$this->subject->findByUid('123');
$subject = $this->getMockForAbstractClass(AbstractRepository::class, [], '', false);
$subject->findByUid('123');
}
}

0 comments on commit 74784c0

Please sign in to comment.