From 772373cfd3ef1295e393762a4165d908a466576b Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Tue, 31 Jan 2017 10:46:11 +0100 Subject: [PATCH] Fix undef. source in exception. --- lib/Twig/Environment.php | 9 ++++---- test/Twig/Tests/EnvironmentTest.php | 33 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index a31f0dbe4..adec90d35 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -359,7 +359,8 @@ class Twig_Environment } if (!class_exists($cls, false)) { - $content = $this->compileSource($this->getLoader()->getSourceContext($name)); + $source = $this->getLoader()->getSourceContext($name); + $content = $this->compileSource($source); $this->cache->write($key, $content); $this->cache->load($key); @@ -371,10 +372,10 @@ class Twig_Environment */ eval('?>'.$content); } - } - if (!class_exists($cls, false)) { - throw new Twig_Error_Runtime(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source); + if (!class_exists($cls, false)) { + throw new Twig_Error_Runtime(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source); + } } } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 4fce28fe6..ea9f46037 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -334,6 +334,18 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertEquals('foo', $twig->render('func_string_named_args')); } + /** + * @expectedException Twig_Error_Runtime + * @expectedExceptionMessage Failed to load Twig template "testFailLoadTemplate.twig", index "abc": cache is corrupted in "testFailLoadTemplate.twig". + */ + public function testFailLoadTemplate() + { + $template = 'testFailLoadTemplate.twig'; + $twig = new Twig_Environment(new Twig_Loader_Array(array($template => false))); + //$twig->setCache(new CorruptCache()); + $twig->loadTemplate($template, 'abc'); + } + protected function getMockLoader($templateName, $templateContent) { $loader = $this->getMockBuilder('Twig_LoaderInterface')->getMock(); @@ -350,6 +362,27 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } } +class CorruptCache implements Twig_CacheInterface +{ + public function generateKey($name, $className) + { + return $name.':'.$className; + } + + public function write($key, $content) + { + } + + public function load($key) + { + } + + public function getTimestamp($key) + { + time(); + } +} + class Twig_Tests_EnvironmentTest_Extension_WithGlobals extends Twig_Extension { public function getGlobals()