2010-03-18 14:26:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-03-18 14:26:21 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-07 02:07:59 +01:00
|
|
|
*
|
2010-03-18 14:26:21 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Templating\Loader;
|
2010-03-18 14:26:21 +00:00
|
|
|
|
2010-06-28 08:31:54 +01:00
|
|
|
require_once __DIR__.'/../Fixtures/ProjectTemplateDebugger.php';
|
2010-03-18 14:26:21 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Templating\Loader\Loader;
|
|
|
|
use Symfony\Component\Templating\Loader\CacheLoader;
|
|
|
|
use Symfony\Component\Templating\Storage\StringStorage;
|
2011-01-26 13:53:12 +00:00
|
|
|
use Symfony\Component\Templating\TemplateNameParser;
|
2011-02-10 17:20:44 +00:00
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface;
|
|
|
|
use Symfony\Component\Templating\TemplateReference;
|
2010-03-18 14:26:21 +00:00
|
|
|
|
|
|
|
class CacheLoaderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testConstructor()
|
|
|
|
{
|
2011-01-13 13:58:17 +00:00
|
|
|
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), sys_get_temp_dir());
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertTrue($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument');
|
|
|
|
$this->assertEquals(sys_get_temp_dir(), $loader->getDir(), '__construct() takes a directory where to store the cache as its second argument');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoad()
|
|
|
|
{
|
|
|
|
$dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
|
|
|
|
mkdir($dir, 0777, true);
|
2011-01-13 13:58:17 +00:00
|
|
|
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), $dir);
|
2010-05-06 12:25:53 +01:00
|
|
|
$loader->setDebugger($debugger = new \ProjectTemplateDebugger());
|
2011-02-10 17:20:44 +00:00
|
|
|
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
|
|
|
|
$loader->load(new TemplateReference('index'));
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
|
2011-02-10 17:20:44 +00:00
|
|
|
$loader->load(new TemplateReference('index'));
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
|
|
|
|
}
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ProjectTemplateLoader extends CacheLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getDir()
|
|
|
|
{
|
|
|
|
return $this->dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLoader()
|
|
|
|
{
|
|
|
|
return $this->loader;
|
|
|
|
}
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ProjectTemplateLoaderVar extends Loader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getIndexTemplate()
|
2010-03-18 14:26:21 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return 'Hello World';
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getSpecialTemplate()
|
|
|
|
{
|
|
|
|
return 'Hello {{ name }}';
|
|
|
|
}
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
public function load(TemplateReferenceInterface $template)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-02-10 17:20:44 +00:00
|
|
|
if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
|
2011-01-13 13:58:17 +00:00
|
|
|
return new StringStorage($this->$method());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-18 14:26:21 +00:00
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
public function isFresh(TemplateReferenceInterface $template, $time)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-05-20 19:57:25 +01:00
|
|
|
return false;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-03-18 14:26:21 +00:00
|
|
|
}
|