diff --git a/Classes/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepository.php b/Classes/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepository.php index 092d8342d..5f6bb6ad8 100644 --- a/Classes/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepository.php +++ b/Classes/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepository.php @@ -19,6 +19,13 @@ class RecordBasedContentTypeDefinitionRepository implements SingletonInterface { + private ConnectionPool $connectionPool; + + public function __construct(ConnectionPool $connectionPool) + { + $this->connectionPool = $connectionPool; + } + /** * @return RecordBasedContentTypeDefinition[]|array */ @@ -26,7 +33,7 @@ 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 */ @@ -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'); - } } diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index a4ad71085..6cb126b37 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -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 diff --git a/Tests/Unit/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepositoryTest.php b/Tests/Unit/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepositoryTest.php index 8255c10cb..b6b3460ed 100644 --- a/Tests/Unit/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepositoryTest.php +++ b/Tests/Unit/Content/TypeDefinition/RecordBased/RecordBasedContentTypeDefinitionRepositoryTest.php @@ -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) @@ -30,6 +34,10 @@ 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(); @@ -37,15 +45,10 @@ protected function setUp(): void 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()); } @@ -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);