Skip to content

Commit

Permalink
[TASK] Conver PageController to constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Aug 1, 2023
1 parent e4129b5 commit ea9320e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
24 changes: 10 additions & 14 deletions Classes/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Builder\RenderingContextBuilder;
use FluidTYPO3\Flux\Builder\RequestBuilder;
use FluidTYPO3\Flux\Provider\Interfaces\BasicProviderInterface;
use FluidTYPO3\Flux\Service\FluxService;
use FluidTYPO3\Flux\Service\PageService;
Expand All @@ -20,27 +22,21 @@ class PageController extends AbstractFluxController implements PageControllerInt
protected ?string $fluxTableName = 'pages';

protected PageService $pageService;
protected FluxService $pageConfigurationService;

/**
* @var Response
*/
protected $response;

public function injectPageService(PageService $pageService): void
{
public function __construct(
FluxService $fluxService,
RenderingContextBuilder $renderingContextBuilder,
RequestBuilder $requestBuilder,
PageService $pageService
) {
parent::__construct($fluxService, $renderingContextBuilder, $requestBuilder);
$this->pageService = $pageService;
}

public function injectPageConfigurationService(FluxService $pageConfigurationService): void
{
$this->pageConfigurationService = $pageConfigurationService;
}

protected function initializeProvider(): void
{
$record = $this->getRecord();
$provider = $this->pageConfigurationService->resolvePageProvider($record);
$provider = $this->configurationService->resolvePageProvider($record);
if ($provider instanceof BasicProviderInterface) {
$this->provider = $provider;
}
Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/Classes/DummyPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Builder\RenderingContextBuilder;
use FluidTYPO3\Flux\Builder\RequestBuilder;
use FluidTYPO3\Flux\Controller\PageController;
use FluidTYPO3\Flux\Provider\Interfaces\ControllerProviderInterface;
use FluidTYPO3\Flux\Service\FluxService;
use FluidTYPO3\Flux\Service\PageService;
use PHPUnit\Framework\MockObject\Generator;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;

class DummyPageController extends PageController
{
protected array $record = [];

public function __construct()
{
$fluxService = $this->createMock(FluxService::class);
$renderingContextBuilder = $this->createMock(RenderingContextBuilder::class);
$requestBuilder = $this->createMock(RequestBuilder::class);
$pageService = $this->createMock(PageService::class);
parent::__construct($fluxService, $renderingContextBuilder, $requestBuilder, $pageService);
}

public function setView(ViewInterface $view): void
{
$this->view = $view;
Expand All @@ -40,4 +54,9 @@ public function setProvider(ControllerProviderInterface $provider): void
{
$this->provider = $provider;
}

private function createMock(string $className): object
{
return (new Generator())->getMock($className, [], [], '', false);
}
}
16 changes: 13 additions & 3 deletions Tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Builder\RenderingContextBuilder;
use FluidTYPO3\Flux\Builder\RequestBuilder;
use FluidTYPO3\Flux\Controller\PageController;
use FluidTYPO3\Flux\Service\FluxService;
use FluidTYPO3\Flux\Service\PageService;
Expand Down Expand Up @@ -49,10 +51,18 @@ public function testInitializeProvider()
->getMock();
$pageConfigurationService->expects($this->once())->method('resolvePrimaryConfigurationProvider');
/** @var PageController|MockObject $instance */
$instance = $this->getMockBuilder(PageController::class)->setMethods(['getRecord'])->disableOriginalConstructor()->getMock();
$instance = $this->getMockBuilder(PageController::class)
->setMethods(['getRecord'])
->setConstructorArgs(
[
$pageConfigurationService,
$this->getMockBuilder(RenderingContextBuilder::class)->disableOriginalConstructor()->getMock(),
$this->getMockBuilder(RequestBuilder::class)->disableOriginalConstructor()->getMock(),
$pageService
]
)
->getMock();
$instance->expects($this->once())->method('getRecord')->willReturn([]);
$instance->injectpageConfigurationService($pageConfigurationService);
$instance->injectPageService($pageService);
$this->callInaccessibleMethod($instance, 'initializeProvider');
}
}

0 comments on commit ea9320e

Please sign in to comment.