Skip to content

Commit

Permalink
Fix adding mock extension to Twig environment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GromNaN committed Oct 23, 2015
1 parent 53aa70a commit a57b1cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Twig/Environment.php
Expand Up @@ -462,7 +462,7 @@ public function isTemplateFresh($name, $time)
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;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/Twig/Tests/EnvironmentTest.php
Expand Up @@ -292,6 +292,22 @@ public function testRemoveExtension()
$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');
Expand Down

0 comments on commit a57b1cc

Please sign in to comment.