Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Render/ContentVersionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function render(ContentVersionInterface $version, Request $request, ?Rend
} catch (Exception $e) {
throw new RenderException(sprintf('Error rendering content version v%s', $version->getVersionNumber()), 0, $e);
}
});
}, true);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Render/Isolated/IsolatedRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
/**
* @throws RenderException
*/
public function isolateEsiCapableRequestRender(callable $renderFunction): mixed
public function isolateEsiCapableRequestRender(callable $renderFunction, bool $resetEntrypoints = false): mixed
{
$currentRequest = $this->requestStack->getCurrentRequest();

Expand All @@ -49,7 +49,7 @@ public function isolateEsiCapableRequestRender(callable $renderFunction): mixed
}

// do the render
$result = $this->isolateRequestRender($currentRequest, $renderFunction);
$result = $this->isolateRequestRender($currentRequest, $renderFunction, $resetEntrypoints);

// Restore the original Surrogate-Capability header if it was set
isset($originalSurrogateCapability) ?
Expand All @@ -62,10 +62,11 @@ public function isolateEsiCapableRequestRender(callable $renderFunction): mixed
/**
* @throws RenderException
*/
public function isolateRequestRender(IsolatedRequest|Request $request, callable $renderFunction): mixed
public function isolateRequestRender(IsolatedRequest|Request $request, callable $renderFunction, bool $resetEntrypoints = false): mixed
{
// reset webpack encore entrypoint lookup to avoid cache issues
$this->entrypointLookup && $this->entrypointLookup->reset();
if ($resetEntrypoints) {
$this->entrypointLookup && $this->entrypointLookup->reset();
}

// inject the current request into the request stack
// this is necessary to ensure that the request is available
Expand Down
73 changes: 73 additions & 0 deletions tests/Unit/Render/IsolatedRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Softspring\CmsBundle\Test\Unit\Render;

use PHPUnit\Framework\TestCase;
use Softspring\CmsBundle\Render\Isolated\IsolatedRunner;
use Softspring\CmsBundle\Render\Module\ModuleRenderer;
use Softspring\CmsBundle\Render\Module\ModuleRendererFactory;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Twig\Environment;

class IsolatedRunnerTest extends TestCase
{
public function testDoesNotResetEncoreEntrypointsByDefault(): void
{
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
$entrypointLookup->expects(self::never())->method('reset');

$runner = $this->createRunner($entrypointLookup);

self::assertSame('rendered', $runner->isolateRequestRender(Request::create('/fragment'), fn (): string => 'rendered'));
}

public function testCanResetEncoreEntrypointsForFullIsolatedRenders(): void
{
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
$entrypointLookup->expects(self::once())->method('reset');

$runner = $this->createRunner($entrypointLookup);

self::assertSame('rendered', $runner->isolateRequestRender(Request::create('/page'), fn (): string => 'rendered', true));
}

public function testEsiCapableRenderDoesNotResetEncoreEntrypointsByDefault(): void
{
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
$entrypointLookup->expects(self::never())->method('reset');

$runner = $this->createRunner($entrypointLookup, Request::create('/current'));

self::assertSame('rendered', $runner->isolateEsiCapableRequestRender(fn (): string => 'rendered'));
}

private function createRunner(EntrypointLookupInterface $entrypointLookup, ?Request $currentRequest = null): IsolatedRunner
{
$requestStack = new RequestStack();
$requestStack->push($currentRequest ?? Request::create('/current'));

$app = new AppVariable();
$app->setRequestStack($requestStack);

$twig = $this->createMock(Environment::class);
$twig->method('getGlobals')->willReturn(['app' => $app]);
$twig->expects(self::atLeastOnce())->method('addGlobal');

$moduleRendererFactory = $this->createMock(ModuleRendererFactory::class);
$moduleRendererFactory->method('create')->willReturn($this->createMock(ModuleRenderer::class));

return new IsolatedRunner(
$requestStack,
$this->createMock(RouterInterface::class),
$twig,
$moduleRendererFactory,
$entrypointLookup,
);
}
}
Loading