From a57b1cc3be61c9751cae05c418e9cf814e043919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 13 Oct 2015 20:33:02 +0200 Subject: [PATCH] Fix adding mock extension to Twig environment When the extension class is not defined in a file but in eval'd code we cannot assume ReflectionObject::getFileName will return a valid file path. https://bugs.php.net/bug.php?id=63901 --- lib/Twig/Environment.php | 2 +- test/Twig/Tests/EnvironmentTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 8e05c1b79..db04dcd36 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -462,7 +462,7 @@ class Twig_Environment if (0 === $this->lastModifiedExtension) { foreach ($this->extensions as $extension) { $r = new ReflectionObject($extension); - if (($extensionTime = filemtime($r->getFileName())) > $this->lastModifiedExtension) { + if (file_exists($r->getFileName()) && ($extensionTime = filemtime($r->getFileName())) > $this->lastModifiedExtension) { $this->lastModifiedExtension = $extensionTime; } } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 57b91ae50..a43e16e2b 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -292,6 +292,22 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertCount(2, $twig->getNodeVisitors()); } + public function testAddMockExtension() + { + $extension = $this->getMock('Twig_ExtensionInterface'); + $extension->expects($this->once()) + ->method('getName') + ->will($this->returnValue('mock')); + + $loader = new Twig_Loader_Array(array('page' => 'hey')); + + $twig = new Twig_Environment($loader); + $twig->addExtension($extension); + + $this->assertInstanceOf('Twig_ExtensionInterface', $twig->getExtension('mock')); + $this->assertTrue($twig->isTemplateFresh('page', time())); + } + protected function getMockLoader($templateName, $templateContent) { $loader = $this->getMock('Twig_LoaderInterface');