bug #2384 Fix undef. source in exception. (SpacePossum)

This PR was merged into the 2.x branch.

Discussion
----------

Fix undef. source in exception.

closes https://github.com/twigphp/Twig/issues/2383

Two things:
- the tests fails without a corrupt cache, so maybe the message is of or the cache generation is off (i.e. not respecting the `$index`)
- passing the `$source` seems superfluous (but maybe that is only the case in the exception message)

Commits
-------

772373cf Fix undef. source in exception.
This commit is contained in:
Fabien Potencier
2017-03-05 13:51:23 -08:00
2 changed files with 38 additions and 4 deletions
+5 -4
View File
@@ -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);
}
}
}
+33
View File
@@ -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()