From f217022ad5ffd179513f0c6ecadf086af4b95c69 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Wed, 2 Feb 2011 23:27:13 +1100 Subject: [PATCH] [TwigBundle] fixed Twig template throwing InvalidArgumentException rather than returning false --- .../TwigBundle/Loader/FilesystemLoader.php | 9 ++- .../Tests/Loader/FilesystemLoaderTest.php | 58 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index ff1d0be46c..69eb54858a 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -84,7 +84,14 @@ class FilesystemLoader implements \Twig_LoaderInterface return $this->cache[$key]; } - if (false === $file = $this->locator->locate($name)) { + $file = null; + try { + $file = $this->locator->locate($name); + } catch (\InvalidArgumentException $e) { + // File wasn't found, will throw twig load error + } + + if ($file === false || $file === null) { throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $name)); } diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php new file mode 100644 index 0000000000..4f935f61e9 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\TwigBundle\Tests\Loader; + +use Symfony\Bundle\TwigBundle\Tests\TestCase; +use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader; +use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocatorInterface; +use Symfony\Component\Templating\TemplateNameParserInterface; +use InvalidArgumentException; + +class FilesystemLoaderTest extends TestCase +{ + /** @var TemplateLocatorInterface */ + private $locator; + /** @var TemplateNameParserInterface */ + private $parser; + /** @var FilesystemLoader */ + private $loader; + + protected function setUp() + { + parent::setUp(); + + $this->locator = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocatorInterface'); + $this->parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface'); + $this->loader = new FilesystemLoader($this->locator, $this->parser); + } + + public function testTwigErrorIfLocatorThrowsInvalid() + { + $this->setExpectedException('Twig_Error_Loader'); + $invalidException = new InvalidArgumentException('Unable to find template "NonExistent".'); + $this->locator->expects($this->once()) + ->method('locate') + ->will($this->throwException($invalidException)); + + $this->loader->getCacheKey('NonExistent'); + } + + public function testTwigErrorIfLocatorReturnsFalse() + { + $this->setExpectedException('Twig_Error_Loader'); + $this->locator->expects($this->once()) + ->method('locate') + ->will($this->returnValue(false)); + + $this->loader->getCacheKey('NonExistent'); + } +}