Skip to content

Commit

Permalink
[TASK] Switch SiteConfigurationProviderItems to constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Aug 12, 2023
1 parent c19925d commit 87a855c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
17 changes: 11 additions & 6 deletions Classes/Integration/FormEngine/SiteConfigurationProviderItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

class SiteConfigurationProviderItems
{
private ContentTypeManager $contentTypeManager;
private PageService $pageService;

public function __construct(ContentTypeManager $contentTypeManager, PageService $pageService)
{
$this->contentTypeManager = $contentTypeManager;
$this->pageService = $pageService;
}

public function processContentTypeItems(array $tca, TcaSelectItems $bar): array
{
/** @var ContentTypeManager $contentTypeManager */
$contentTypeManager = GeneralUtility::makeInstance(ContentTypeManager::class);
foreach ($contentTypeManager->fetchContentTypeNames() as $contentTypeName) {
foreach ($this->contentTypeManager->fetchContentTypeNames() as $contentTypeName) {
$tca['items'][] = [
$contentTypeName,
$contentTypeName,
Expand All @@ -26,9 +33,7 @@ public function processContentTypeItems(array $tca, TcaSelectItems $bar): array

public function processPageTemplateItems(array $tca, TcaSelectItems $bar): array
{
/** @var PageService $pageService */
$pageService = GeneralUtility::makeInstance(PageService::class);
foreach ($pageService->getAvailablePageTemplateFiles() as $extensionName => $templateGroup) {
foreach ($this->pageService->getAvailablePageTemplateFiles() as $extensionName => $templateGroup) {
foreach ($templateGroup as $form) {
/** @var string|null $templateFilename */
$templateFilename = $form->getOption(Form::OPTION_TEMPLATEFILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,34 @@

class SiteConfigurationProviderItemsTest extends AbstractTestCase
{
private ?PageService $pageService;
private PageService $pageService;
private ContentTypeManager $contentTypeManager;

protected function setUp(): void
{
$this->pageService = $this->getMockBuilder(PageService::class)
->setMethods(['getAvailablePageTemplateFiles'])
->onlyMethods(['getAvailablePageTemplateFiles'])
->disableOriginalConstructor()
->getMock();
$this->contentTypeManager = $this->getMockBuilder(ContentTypeManager::class)
->onlyMethods(['fetchContentTypeNames'])
->disableOriginalConstructor()
->getMock();

$this->singletonInstances[PageService::class] = $this->pageService;

parent::setUp();
}

public function testProcessContentTypeItems(): void
{
$singletons = GeneralUtility::getSingletonInstances();

$contentTypeManager = $this->getMockBuilder(ContentTypeManager::class)
->setMethods(['fetchContentTypeNames'])
->disableOriginalConstructor()
->getMock();
$contentTypeManager->method('fetchContentTypeNames')->willReturn(['flux_test', 'flux_test2']);

GeneralUtility::setSingletonInstance(ContentTypeManager::class, $contentTypeManager);
$this->contentTypeManager->method('fetchContentTypeNames')->willReturn(['flux_test', 'flux_test2']);

$tca = ['items' => []];

$expected = $tca;
$expected['items'][] = ['flux_test', 'flux_test'];
$expected['items'][] = ['flux_test2', 'flux_test2'];

$subject = new SiteConfigurationProviderItems();
$subject = new SiteConfigurationProviderItems($this->contentTypeManager, $this->pageService);
$output = $subject->processContentTypeItems(
$tca,
$this->getMockBuilder(TcaSelectItems::class)->disableOriginalConstructor()->getMock()
Expand All @@ -60,8 +55,6 @@ public function testProcessContentTypeItems(): void
$expected,
$output
);

GeneralUtility::resetSingletonInstances($singletons);
}

public function testProcessPageTemplateItems(): void
Expand All @@ -80,8 +73,8 @@ public function testProcessPageTemplateItems(): void
);

$subject = $this->getMockBuilder(SiteConfigurationProviderItems::class)
->setMethods(['translate'])
->disableOriginalConstructor()
->onlyMethods(['translate'])
->setConstructorArgs([$this->contentTypeManager, $this->pageService])
->getMock();
$subject->method('translate')->willReturn('test');

Expand Down

0 comments on commit 87a855c

Please sign in to comment.