From: Fabien Potencier Date: Wed, 24 Aug 2011 10:16:12 +0000 (+0200) Subject: added Twig_LoaderArray::setTemplate() X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d6ebae56b7f203f36ed2c58ef53ca9061d742c63;p=web%2Fkonrad%2Ftwig.git added Twig_LoaderArray::setTemplate() --- diff --git a/CHANGELOG b/CHANGELOG index 43083f8..72b120f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.2.0 + * added Twig_Loader_Array::setTemplate() * added an optimization for the set tag when used to capture a large chunk of static text * changed name regex to match PHP one "[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*" (works for blocks, tags, functions, filters, and macros) * removed the possibility to use the "extends" tag from a block diff --git a/lib/Twig/Loader/Array.php b/lib/Twig/Loader/Array.php index 35263dd..e8dc160 100644 --- a/lib/Twig/Loader/Array.php +++ b/lib/Twig/Loader/Array.php @@ -40,6 +40,17 @@ class Twig_Loader_Array implements Twig_LoaderInterface } /** + * Adds or overrides a template. + * + * @param string $name The template name + * @param string $template The template source + */ + public function setTemplate($name, $template) + { + $this->templates[$name] = $template; + } + + /** * Gets the source code of a template, given its name. * * @param string $name The name of the template to load diff --git a/test/Twig/Tests/Loader/ArrayLoaderTest.php b/test/Twig/Tests/Loader/ArrayLoaderTest.php new file mode 100644 index 0000000..38a8f89 --- /dev/null +++ b/test/Twig/Tests/Loader/ArrayLoaderTest.php @@ -0,0 +1,61 @@ + 'bar')); + + $this->assertEquals('bar', $loader->getSource('foo')); + } + + /** + * @expectedException Twig_Error_Loader + */ + public function testGetSourceWhenTemplateDoesNotExist() + { + $loader = new Twig_Loader_Array(array()); + + $loader->getSource('foo'); + } + + public function testGetCacheKey() + { + $loader = new Twig_Loader_Array(array('foo' => 'bar')); + + $this->assertEquals('bar', $loader->getCacheKey('foo')); + } + + /** + * @expectedException Twig_Error_Loader + */ + public function testGetCacheKeyWhenTemplateDoesNotExist() + { + $loader = new Twig_Loader_Array(array()); + + $loader->getCacheKey('foo'); + } + + public function testSetTemplate() + { + $loader = new Twig_Loader_Array(array()); + $loader->setTemplate('foo', 'bar'); + + $this->assertEquals('bar', $loader->getSource('foo')); + } + + public function testIsFresh() + { + $loader = new Twig_Loader_Array(array('foo' => 'bar')); + $this->assertTrue($loader->isFresh('foo', time())); + } +}