From 5880a79316667429fd1d5ef7289e6e68bc9896ed Mon Sep 17 00:00:00 2001 From: DDEV User Date: Thu, 26 Sep 2024 14:18:17 +0000 Subject: [PATCH] test template cache --- src/Filesystem/TwigTemplateLocator.php | 7 +- tests/Filesystem/TwigTemplateLocatorTest.php | 86 +++++--------------- 2 files changed, 23 insertions(+), 70 deletions(-) diff --git a/src/Filesystem/TwigTemplateLocator.php b/src/Filesystem/TwigTemplateLocator.php index 1feda0b..6e1082e 100644 --- a/src/Filesystem/TwigTemplateLocator.php +++ b/src/Filesystem/TwigTemplateLocator.php @@ -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]); } /** @@ -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 diff --git a/tests/Filesystem/TwigTemplateLocatorTest.php b/tests/Filesystem/TwigTemplateLocatorTest.php index 5f7121e..7d6c57d 100644 --- a/tests/Filesystem/TwigTemplateLocatorTest.php +++ b/tests/Filesystem/TwigTemplateLocatorTest.php @@ -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; @@ -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])); @@ -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';