Skip to content

Commit

Permalink
[TASK] Cleanup UncachedTemplateView and restore test
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Jan 24, 2023
1 parent 9612428 commit f51de0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 59 deletions.
43 changes: 12 additions & 31 deletions Classes/View/UncacheTemplateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,10 @@
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\View\TemplateView;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
* Uncache Template View
*/
class UncacheTemplateView extends TemplateView
{
/**
* @var TemplateParser
*/
protected $templateParser;

/**
* @var TemplateCompiler
*/
protected $templateCompiler;

/**
* @return array
*/
public function __sleep()
{
return ['renderingStack'];
}

public function callUserFunction(string $postUserFunc, array $conf, string $content): string
{
$partial = $conf['partial'] ?? null;
Expand All @@ -56,10 +33,7 @@ public function callUserFunction(string $postUserFunc, array $conf, string $cont
}

if (class_exists(ExtbaseRequestParameters::class)) {
/** @var RenderingContextFactory $renderingContextFactory */
$renderingContextFactory = GeneralUtility::makeInstance(RenderingContextFactory::class);
/** @var RenderingContext $renderingContext */
$renderingContext = $renderingContextFactory->create();
$renderingContext = $this->createRenderingContextWithRenderingContextFactory();

if (method_exists($renderingContext, 'setRequest')) {
$renderingContext->setRequest(
Expand Down Expand Up @@ -110,17 +84,14 @@ public function callUserFunction(string $postUserFunc, array $conf, string $cont

$this->prepareContextsForUncachedRendering($renderingContext);
if (!empty($conf['partialRootPaths'])) {
$templatePaths = $renderingContext->getTemplatePaths();
$templatePaths->setPartialRootPaths($conf['partialRootPaths']);
$renderingContext->getTemplatePaths()->setPartialRootPaths($conf['partialRootPaths']);
}
return $this->renderPartialUncached($renderingContext, $partial, $section, $arguments);
}

protected function prepareContextsForUncachedRendering(RenderingContextInterface $renderingContext): void
{
$this->setRenderingContext($renderingContext);
$this->templateParser = $renderingContext->getTemplateParser();
$this->templateCompiler = $renderingContext->getTemplateCompiler();
}

protected function renderPartialUncached(
Expand All @@ -138,4 +109,14 @@ protected function renderPartialUncached(
array_pop($this->renderingStack);
return $rendered;
}

/**
* @codeCoverageIgnore
*/
protected function createRenderingContextWithRenderingContextFactory(): RenderingContextInterface
{
/** @var RenderingContextFactory $renderingContextFactory */
$renderingContextFactory = GeneralUtility::makeInstance(RenderingContextFactory::class);
return $renderingContextFactory->create();
}
}
68 changes: 40 additions & 28 deletions Tests/Unit/View/UncacheTemplateViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@
*/

use FluidTYPO3\Vhs\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\View\TemplatePaths;

/**
* Class UncacheTemplateViewTest
*/
class UncacheTemplateViewTest extends AbstractTestCase
{
private ?RenderingContext $renderingContext = null;

protected function setUp(): void
{
if (class_exists(RenderingContextFactory::class)) {
self::markTestSkipped('Skipping test with RenderingContextFactory dependency');
}
/*
* [ControllerContext::class, new ControllerContext()],
[Request::class, new Request()],
[UriBuilder::class, new UriBuilder()],
*/
GeneralUtility::addInstance(
RenderingContext::class,
$this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock()
$this->renderingContext = $this->getMockBuilder(RenderingContext::class)
->setMethods(['dummy'])
->disableOriginalConstructor()
->getMock();
$this->renderingContext->setTemplatePaths(
$this->getMockBuilder(TemplatePaths::class)->disableOriginalConstructor()->getMock()
);
GeneralUtility::addInstance(RenderingContext::class, $this->renderingContext);
$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class)
->setMethods(['getAttribute', 'withAttribute'])
->disableOriginalConstructor()
->getMock();
$GLOBALS['TYPO3_REQUEST']->method('withAttribute')->willReturnSelf();
if (class_exists(ExtbaseRequestParameters::class)) {
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')->willReturn(new ExtbaseRequestParameters());
}
parent::setUp();
}

Expand All @@ -42,9 +46,10 @@ protected function setUp(): void
public function callUserFunctionReturnsEarlyIfPartialEmpty()
{
$mock = $this->getMockBuilder($this->getClassName())
->setMethods(['prepareContextsForUncachedRendering'])
->setMethods(['prepareContextsForUncachedRendering', 'createRenderingContextWithRenderingContextFactory'])
->disableOriginalConstructor()
->getMock();
$mock->method('createRenderingContextWithRenderingContextFactory')->willReturn($this->renderingContext);

$configuration = ['partial' => ''];
$mock->expects($this->never())->method('prepareContextsForUncachedRendering');
Expand All @@ -57,12 +62,24 @@ public function callUserFunctionReturnsEarlyIfPartialEmpty()
public function callUserFunctionReturnsCallsExpectedMethodSequence()
{
$mock = $this->getMockBuilder($this->getClassName())
->setMethods(['prepareContextsForUncachedRendering', 'renderPartialUncached'])
->setMethods(
[
'setRenderingContext',
'renderPartialUncached',
'createRenderingContextWithRenderingContextFactory'
]
)
->disableOriginalConstructor()
->getMock();
$mock->method('createRenderingContextWithRenderingContextFactory')->willReturn($this->renderingContext);

$configuration = ['partial' => 'dummy', 'section' => 'dummy', 'controllerContext' => []];
$mock->expects($this->once())->method('prepareContextsForUncachedRendering');
$configuration = [
'partial' => 'dummy',
'section' => 'dummy',
'controllerContext' => [],
'partialRootPaths' => ['foo']
];
$mock->expects($this->once())->method('setRenderingContext');
$mock->expects($this->once())->method('renderPartialUncached');
$mock->callUserFunction('', $configuration, '');
}
Expand All @@ -72,30 +89,25 @@ public function callUserFunctionReturnsCallsExpectedMethodSequence()
*/
public function prepareContextsForUncachedRenderingCallsExpectedMethodSequence()
{
$controllerContext = new ControllerContext();
$renderingContext = $this->getMockBuilder(RenderingContext::class)
->disableOriginalConstructor()
->getMock();
$mock = $this->getMockBuilder($this->getClassName())
->setMethods(['setRenderingContext'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->once())->method('setRenderingContext')->with($renderingContext);
$this->callInaccessibleMethod($mock, 'prepareContextsForUncachedRendering', $renderingContext, $controllerContext);
$mock->expects($this->once())->method('setRenderingContext')->with($this->renderingContext);
$this->callInaccessibleMethod($mock, 'prepareContextsForUncachedRendering', $this->renderingContext);
}

/**
* @test
*/
public function renderPartialUncachedDelegatesToRenderPartial()
{
$renderingContext = $this->getMockBuilder(RenderingContext::class)->disableOriginalConstructor()->getMock();
$mock = $this->getMockBuilder($this->getClassName())
->setMethods(['renderPartial'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->once())->method('renderPartial')->will($this->returnValue('test'));
$result = $this->callInaccessibleMethod($mock, 'renderPartialUncached', $renderingContext, 'dummy');
$result = $this->callInaccessibleMethod($mock, 'renderPartialUncached', $this->renderingContext, 'dummy');
$this->assertEquals('test', $result);
}

Expand Down

0 comments on commit f51de0b

Please sign in to comment.