diff --git a/src/Twig/Loader/ThemedTemplateLoader.php b/src/Twig/Loader/ThemedTemplateLoader.php index a37f4eb..4587a78 100644 --- a/src/Twig/Loader/ThemedTemplateLoader.php +++ b/src/Twig/Loader/ThemedTemplateLoader.php @@ -53,7 +53,14 @@ public function getCacheKey($name): string try { return $this->locateTemplate($name); } catch (TemplateNotFoundException $exception) { - return $this->decoratedLoader->getCacheKey($name); + $cacheKey = $this->decoratedLoader->getCacheKey($name); + $theme = $this->themeContext->getTheme(); + + if (null !== $theme) { + $cacheKey .= '|' . $theme->getName(); + } + + return $cacheKey; } } diff --git a/tests/Twig/Loader/ThemedTemplateLoaderTest.php b/tests/Twig/Loader/ThemedTemplateLoaderTest.php new file mode 100644 index 0000000..f95f1c2 --- /dev/null +++ b/tests/Twig/Loader/ThemedTemplateLoaderTest.php @@ -0,0 +1,114 @@ +createMock(TwigLoaderInterface::class); + $templateLocator = $this->createMock(TemplateLocatorInterface::class); + $themeContext = $this->createMock(ThemeContextInterface::class); + $theme = $this->createMock(ThemeInterface::class); + + $themeContext->method('getTheme')->willReturn($theme); + $templateLocator->method('locate')->willReturn('/path/to/theme-a/template.html.twig'); + + $loader = new ThemedTemplateLoader($decoratedLoader, $templateLocator, $themeContext); + + $this->assertSame('/path/to/theme-a/template.html.twig', $loader->getCacheKey('template.html.twig')); + } + + /** + * @test + */ + public function it_appends_theme_name_to_cache_key_when_template_is_not_in_active_theme(): void + { + $decoratedLoader = $this->createMock(TwigLoaderInterface::class); + $templateLocator = $this->createMock(TemplateLocatorInterface::class); + $themeContext = $this->createMock(ThemeContextInterface::class); + $theme = $this->createMock(ThemeInterface::class); + + $themeContext->method('getTheme')->willReturn($theme); + $theme->method('getName')->willReturn('sylius/theme-a'); + $templateLocator->method('locate')->willThrowException(new TemplateNotFoundException('template.html.twig', [])); + $decoratedLoader->method('getCacheKey')->willReturn('/path/to/templates/template.html.twig'); + + $loader = new ThemedTemplateLoader($decoratedLoader, $templateLocator, $themeContext); + + $this->assertSame( + '/path/to/templates/template.html.twig|sylius/theme-a', + $loader->getCacheKey('template.html.twig'), + ); + } + + /** + * @test + */ + public function it_returns_plain_cache_key_when_no_theme_is_active(): void + { + $decoratedLoader = $this->createMock(TwigLoaderInterface::class); + $templateLocator = $this->createMock(TemplateLocatorInterface::class); + $themeContext = $this->createMock(ThemeContextInterface::class); + + $themeContext->method('getTheme')->willReturn(null); + $decoratedLoader->method('getCacheKey')->willReturn('/path/to/templates/template.html.twig'); + + $loader = new ThemedTemplateLoader($decoratedLoader, $templateLocator, $themeContext); + + $this->assertSame( + '/path/to/templates/template.html.twig', + $loader->getCacheKey('template.html.twig'), + ); + } + + /** + * @test + */ + public function it_returns_different_cache_keys_for_same_template_under_different_themes(): void + { + $decoratedLoader = $this->createMock(TwigLoaderInterface::class); + $templateLocator = $this->createMock(TemplateLocatorInterface::class); + $themeContext = $this->createMock(ThemeContextInterface::class); + $themeA = $this->createMock(ThemeInterface::class); + $themeB = $this->createMock(ThemeInterface::class); + + $themeA->method('getName')->willReturn('sylius/theme-a'); + $themeB->method('getName')->willReturn('sylius/theme-b'); + $templateLocator->method('locate')->willThrowException(new TemplateNotFoundException('template.html.twig', [])); + $decoratedLoader->method('getCacheKey')->willReturn('/path/to/templates/template.html.twig'); + + $themeContext->method('getTheme')->willReturnOnConsecutiveCalls($themeA, $themeA, $themeB, $themeB); + + $loader = new ThemedTemplateLoader($decoratedLoader, $templateLocator, $themeContext); + + $cacheKeyA = $loader->getCacheKey('template.html.twig'); + $cacheKeyB = $loader->getCacheKey('template.html.twig'); + + $this->assertNotSame($cacheKeyA, $cacheKeyB); + $this->assertSame('/path/to/templates/template.html.twig|sylius/theme-a', $cacheKeyA); + $this->assertSame('/path/to/templates/template.html.twig|sylius/theme-b', $cacheKeyB); + } +}