added Twig_LoaderArray::setTemplate()

This commit is contained in:
Fabien Potencier
2011-08-24 12:16:12 +02:00
parent bdf8e69ab0
commit d6ebae56b7
3 changed files with 73 additions and 0 deletions
+1
View File
@@ -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
+11
View File
@@ -39,6 +39,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.
*
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_Loader_ArrayTest extends PHPUnit_Framework_TestCase
{
public function testGetSource()
{
$loader = new Twig_Loader_Array(array('foo' => '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()));
}
}