From 87a855c7800b6da84b815bc5b8c882edac2fddba Mon Sep 17 00:00:00 2001 From: Claus Due Date: Sat, 12 Aug 2023 14:48:32 +0200 Subject: [PATCH] [TASK] Switch SiteConfigurationProviderItems to constructor injection --- .../SiteConfigurationProviderItems.php | 17 +++++++---- .../SiteConfigurationProviderItemsTest.php | 29 +++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Classes/Integration/FormEngine/SiteConfigurationProviderItems.php b/Classes/Integration/FormEngine/SiteConfigurationProviderItems.php index 04745294c..663430e22 100644 --- a/Classes/Integration/FormEngine/SiteConfigurationProviderItems.php +++ b/Classes/Integration/FormEngine/SiteConfigurationProviderItems.php @@ -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, @@ -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); diff --git a/Tests/Unit/Integration/FormEngine/SiteConfigurationProviderItemsTest.php b/Tests/Unit/Integration/FormEngine/SiteConfigurationProviderItemsTest.php index b0b223b84..be018204c 100644 --- a/Tests/Unit/Integration/FormEngine/SiteConfigurationProviderItemsTest.php +++ b/Tests/Unit/Integration/FormEngine/SiteConfigurationProviderItemsTest.php @@ -18,31 +18,26 @@ 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' => []]; @@ -50,7 +45,7 @@ public function testProcessContentTypeItems(): void $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() @@ -60,8 +55,6 @@ public function testProcessContentTypeItems(): void $expected, $output ); - - GeneralUtility::resetSingletonInstances($singletons); } public function testProcessPageTemplateItems(): void @@ -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');