Skip to content

Commit

Permalink
[TASK] Convert RecordBasedContentTypeDefinitionRepository to construc…
Browse files Browse the repository at this point in the history
…tor injection
  • Loading branch information
NamelessCoder committed Jul 29, 2023
1 parent 84b6551 commit de92014
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@

class RecordBasedContentTypeDefinitionRepository implements SingletonInterface
{
private ConnectionPool $connectionPool;

public function __construct(ConnectionPool $connectionPool)
{
$this->connectionPool = $connectionPool;
}

/**
* @return RecordBasedContentTypeDefinition[]|array
*/
public function fetchContentTypeDefinitions(): array
{
$definitions = [];
try {
$queryBuilder = $this->createQueryBuilder();
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('content_types');
/** @var string[] $keys */
$keys = array_keys($GLOBALS['TCA']['content_types']['columns'] ?? ['*' => '']);
/** @var array[] $typeRecords */
Expand Down Expand Up @@ -58,14 +65,4 @@ public function fetchContentTypeDefinitions(): array
}
return $definitions;
}

/**
* @codeCoverageIgnore
*/
protected function createQueryBuilder(): QueryBuilder
{
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
return $connectionPool->getQueryBuilderForTable('content_types');
}
}
2 changes: 2 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ services:
FluidTYPO3\Flux\Content\TypeDefinition\RecordBased\RecordBasedContentGridProvider:
shared: false
public: true
FluidTYPO3\Flux\Content\TypeDefinition\RecordBased\RecordBasedContentTypeDefinitionRepository:
public: true
FluidTYPO3\Flux\Form\Transformation\FormDataTransformer:
public: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
use FluidTYPO3\Flux\Tests\Fixtures\Classes\AccessibleExtensionManagementUtility;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Package\PackageManager;

class RecordBasedContentTypeDefinitionRepositoryTest extends AbstractTestCase
{
protected QueryBuilder $queryBuilder;
protected ConnectionPool $connectionPool;

protected function setUp(): void
{
$packageManager = $this->getMockBuilder(PackageManager::class)
Expand All @@ -30,22 +34,21 @@ protected function setUp(): void
->getMock();
$packageManager->method('isPackageActive')->willReturn(true);

$this->queryBuilder = $this->createQueryBuilderMock();
$this->connectionPool = $this->getMockBuilder(ConnectionPool::class)->onlyMethods(['getQueryBuilderForTable'])->getMock();
$this->connectionPool->method('getQueryBuilderForTable')->willReturn($this->queryBuilder);

AccessibleExtensionManagementUtility::setPackageManager($packageManager);

parent::setUp();
}

public function testReturnsEmptySetOfDefinitionsOnTableNotExists(): void
{
$queryBuilder = $this->createQueryBuilderMock();
$queryBuilder->method('select')->willThrowException(
$this->queryBuilder->method('select')->willThrowException(
$this->getMockBuilder(TableNotFoundException::class)->disableOriginalConstructor()->getMock()
);
$subject = $this->getMockBuilder(RecordBasedContentTypeDefinitionRepository::class)
->setMethods(['createQueryBuilder'])
->disableOriginalConstructor()
->getMock();
$subject->method('createQueryBuilder')->willReturn($queryBuilder);
$subject = new RecordBasedContentTypeDefinitionRepository($this->connectionPool);

self::assertSame([], $subject->fetchContentTypeDefinitions());
}
Expand All @@ -66,14 +69,9 @@ public function testCreatesDefinitionInstancesFromResultSet(): void

];

$queryBuilder = $this->createQueryBuilderMock();
$subject = $this->getMockBuilder(RecordBasedContentTypeDefinitionRepository::class)
->setMethods(['createQueryBuilder'])
->disableOriginalConstructor()
->getMock();
$subject->method('createQueryBuilder')->willReturn($queryBuilder);
$subject = new RecordBasedContentTypeDefinitionRepository($this->connectionPool);

$queryBuilder->execute()->method('fetchAll')->willReturn($definitionRecords);
$this->queryBuilder->execute()->method('fetchAll')->willReturn($definitionRecords);

$definitions = $subject->fetchContentTypeDefinitions();
self::assertCount(2, $definitions);
Expand Down

0 comments on commit de92014

Please sign in to comment.