Skip to content

Commit

Permalink
test template cache
Browse files Browse the repository at this point in the history
  • Loading branch information
DDEV User committed Sep 26, 2024
1 parent 272393a commit 5880a79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 70 deletions.
7 changes: 2 additions & 5 deletions src/Filesystem/TwigTemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ public function getTemplateContext(string $templateName, array $options = []): T
}

throw new TemplateNotFoundException(sprintf('Unable to find template "%s".', $templateName));

$path = end($template['paths']);

return new TemplateContext($templateName, $path, $template['pathInfo'][$path]);
}

/**
Expand Down Expand Up @@ -298,7 +294,8 @@ public function getTemplates(bool $extension = false, bool $disableCache = false
$cacheItem->set($this->generateContaoTwigTemplatePaths($extension));
$this->templateCache->save($cacheItem);
}
$cachedTemplates = $this->templateCache->getItem($cacheKey)->get();

$cachedTemplates = $cacheItem->get();

if (!\is_array($cachedTemplates)) {
// clean invalid cache entry
Expand Down
86 changes: 21 additions & 65 deletions tests/Filesystem/TwigTemplateLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use HeimrichHannot\TestUtilitiesBundle\Mock\ModelMockTrait;
use HeimrichHannot\TwigSupportBundle\Filesystem\TwigTemplateLocator;
use PHPUnit\Framework\MockObject\MockBuilder;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -191,72 +192,7 @@ public function testGetTemplateGroup()
], $instance->getTemplateGroup('prefix'));
}

public function testGenerateContaoTwigTemplatePathsEmpty()
{
$kernel = $this->createMock(Kernel::class);
$kernel->method('getBundles')->willReturn([]);
$kernel->method('getProjectDir')->willReturn(__DIR__.'/../Fixtures/templateLocator/empty');

$resourceFinder = $this->getMockBuilder(ResourceFinderInterface::class)->setMethods(['find', 'findIn', 'name', 'getIterator'])->getMock();
$resourceFinder->method('findIn')->willReturnSelf();
$resourceFinder->method('name')->willReturnSelf();
$resourceFinder->method('getIterator')->willReturn([]);

$instance = $this->createTestInstance([
'kernel' => $kernel,
'resource_finder' => $resourceFinder,
]);
$this->assertEmpty($instance->getTemplates(false, true));
}

public function testGenerateContaoTwigTemplatePathsBundles()
{
[$kernel, $resourceFinder, $templateLocator] = $this->buildKernelAndResourceFinderForBundlesAndPath(['dolarBundle', 'ipsumBundle'], 'bundles');

$instance = $this->createTestInstance([
'kernel' => $kernel,
'resource_finder' => $resourceFinder,
'locator' => $templateLocator,
]);
$templates = $instance->getTemplates(false, true);
$this->assertNotEmpty($templates);
$this->assertArrayHasKey('ce_text', $templates);

$templates = $instance->getTemplates(true, true);
$this->assertNotEmpty($templates);
$this->assertArrayHasKey('ce_text.html.twig', $templates);
}

public function testGetTemplatePath()
{
[$kernel, $resourceFinder, $templateLocator] = $this->buildKernelAndResourceFinderForBundlesAndPath(['dolarBundle', 'ipsumBundle'], 'project');
$scopeMather = $this->createMock(ScopeMatcher::class);
$scopeMather->method('isFrontendRequest')->willReturn(false);

$instance = $this->createTestInstance([
'kernel' => $kernel,
'resource_finder' => $resourceFinder,
'scope_matcher' => $scopeMather,
'locator' => $templateLocator,
]);
$this->assertSame('ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('content_element/text', ['disableCache' => true]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('text', ['disableCache' => true]));

[$kernel, $resourceFinder, $templateLocator] = $this->buildKernelAndResourceFinderForBundlesAndPath(['dolarBundle', 'ipsumBundle'], 'mixed');
$scopeMather = $this->createMock(ScopeMatcher::class);
$scopeMather->method('isFrontendRequest')->willReturn(false);

$instance = $this->createTestInstance([
'kernel' => $kernel,
'resource_finder' => $resourceFinder,
'scope_matcher' => $scopeMather,
'locator' => $templateLocator,
]);
$this->assertSame('@ipsum/ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
}

public function testGetTemplatePathNews()
{
$instance = $this->createTestInstance($this->prepareTemplateLoader([]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('text', ['disableCache' => true]));
Expand Down Expand Up @@ -287,6 +223,26 @@ public function testGetTemplatePathNews()
unset($GLOBALS['objPage']);
}

public function testGetTemplates()
{
$mock = $this->getMockBuilder(TwigTemplateLocator::class)->onlyMethods(['generateContaoTwigTemplatePaths']);
$parameters = $this->prepareTemplateLoader([]);

$cacheItem = $this->createMock(CacheItemInterface::class);
$cacheItem->method('isHit')->willReturnOnConsecutiveCalls(false, true);
$cacheItem->method('get')->willReturn([]);

$cache = $this->createMock(FilesystemAdapter::class);
$cache->method('getItem')->willReturn($cacheItem);
$parameters['cache'] = $cache;

$instance = $this->createTestInstance($parameters, $mock);
$instance->expects($this->once())->method('generateContaoTwigTemplatePaths')->willReturn([]);

$instance->getTemplates();
$instance->getTemplates();
}

private function prepareTemplateLoader(array $parameters): array
{
$projectDir = __DIR__.'/../Fixtures/TwigTemplateLocator';
Expand Down

0 comments on commit 5880a79

Please sign in to comment.