public function testLoad() { $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . rand(111111, 999999); mkdir($dir, 0777, true); $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir); $this->assertFalse($loader->load(new ehough_templating_TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template'); $logger = $this->getMock('ehough_templating_test_MockLoggerInterface'); $logger->expects($this->once())->method('debug')->with('Storing template "index" in cache'); $loader->setLogger($logger); $loader->load(new ehough_templating_TemplateReference('index')); $logger = $this->getMock('ehough_templating_test_MockLoggerInterface'); $logger->expects($this->once())->method('debug')->with('Fetching template "index" from cache'); $loader->setLogger($logger); $loader->load(new ehough_templating_TemplateReference('index')); }
$t->diag('__construct()'); $pathPattern = $fixturesPath.'/templates/%name%.%renderer%'; $path = $fixturesPath.'/templates'; $loader = new ProjectTemplateLoader($pathPattern); $t->is($loader->getTemplatePathPatterns(), array($pathPattern), '__construct() takes a path as its second argument'); $loader = new ProjectTemplateLoader(array($pathPattern)); $t->is($loader->getTemplatePathPatterns(), array($pathPattern), '__construct() takes an array of paths as its second argument'); // ->load() $t->diag('->load()'); $loader = new ProjectTemplateLoader($pathPattern); $storage = $loader->load($path.'/foo.php'); $t->ok($storage instanceof FileStorage, '->load() returns a FileStorage if you pass an absolute path'); $t->is((string) $storage, $path.'/foo.php', '->load() returns a FileStorage pointing to the passed absolute path'); $t->ok($loader->load('bar') === false, '->load() returns false if the template is not found'); $storage = $loader->load('foo'); $t->ok($storage instanceof FileStorage, '->load() returns a FileStorage if you pass a relative template that exists'); $t->is((string) $storage, $path.'/foo.php', '->load() returns a FileStorage pointing to the absolute path of the template'); $loader = new ProjectTemplateLoader($pathPattern); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $t->ok($loader->load('foo', array('renderer' => 'xml')) === false, '->load() returns false if the template does not exists for the given renderer'); $t->ok($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found'); $loader = new ProjectTemplateLoader(array($fixturesPath.'/null/%name%', $pathPattern)); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $loader->load('foo'); $t->ok($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');
$t->ok($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument'); $t->is($loader->getDir(), sys_get_temp_dir(), '__construct() takes a directory where to store the cache as its second argument'); // ->load() $t->diag('->load()'); $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999); mkdir($dir, 0777, true); $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $t->ok($loader->load('foo') === false, '->load() returns false if the embed loader is not able to load the template'); $loader->load('index'); $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found'); $loader->load('index'); $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache'); $t->diag('load() template compilation'); $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999); mkdir($dir, 0777, true); $loader = new ProjectTemplateLoader(new CompilableTemplateLoader(), $dir); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $template = $loader->load('special', array('renderer' => 'comp')); $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found'); $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable'); $template = $loader->load('special', array('renderer' => 'comp')); $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache'); $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');
{ return preg_replace('/{{\\s*([a-zA-Z0-9_]+)\\s*}}/', '<?php echo $$1 ?>', $template); } } // __construct() $t->diag('__construct()'); $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), sys_get_temp_dir()); $t->ok($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument'); $t->is($loader->getDir(), sys_get_temp_dir(), '__construct() takes a directory where to store the cache as its second argument'); // ->load() $t->diag('->load()'); $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . rand(111111, 999999); mkdir($dir, 0777, true); $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $t->ok($loader->load('foo') === false, '->load() returns false if the embed loader is not able to load the template'); $loader->load('index'); $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found'); $loader->load('index'); $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache'); $t->diag('load() template compilation'); $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . rand(111111, 999999); mkdir($dir, 0777, true); $loader = new ProjectTemplateLoader(new CompilableTemplateLoader(), $dir); $loader->setDebugger($debugger = new ProjectTemplateDebugger()); $template = $loader->load('special', 'comp'); $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found'); $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable'); $template = $loader->load('special', 'comp'); $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache'); $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');
$t = new LimeTest(5); class ProjectTemplateLoader extends ChainLoader { public function getLoaders() { return $this->loaders; } } $loader1 = new FilesystemLoader($fixturesPath.'/null/%name%'); $loader2 = new FilesystemLoader($fixturesPath.'/templates/%name%.%renderer%'); // __construct() $t->diag('__construct()'); $loader = new ProjectTemplateLoader(array($loader1, $loader2)); $t->is($loader->getLoaders(), array($loader1, $loader2), '__construct() takes an array of template loaders as its second argument'); // ->addLoader() $t->diag('->addLoader()'); $loader = new ProjectTemplateLoader(array($loader1)); $loader->addLoader($loader2); $t->is($loader->getLoaders(), array($loader1, $loader2), '->addLoader() adds a template loader at the end of the loaders'); // ->load() $t->diag('->load()'); $loader = new ProjectTemplateLoader(array($loader1, $loader2)); $t->ok($loader->load('bar') === false, '->load() returns false if the template is not found'); $t->ok($loader->load('foo', array('renderer' => 'xml')) === false, '->load() returns false if the template does not exists for the given renderer'); $t->ok($loader->load('foo') instanceof FileStorage, '->load() returns a FileStorage if the template exists');
* For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../lib/lime/lime.php'; require_once dirname(__FILE__) . '/../../lib/sfTemplateAutoloader.php'; sfTemplateAutoloader::register(); $t = new lime_test(5); class ProjectTemplateLoader extends sfTemplateLoaderChain { public function getLoaders() { return $this->loaders; } } $loader1 = new sfTemplateLoaderFilesystem(dirname(__FILE__) . '/null/%name%'); $loader2 = new sfTemplateLoaderFilesystem(dirname(__FILE__) . '/fixtures/templates/%name%.%renderer%'); // __construct() $t->diag('__construct()'); $loader = new ProjectTemplateLoader(array($loader1, $loader2)); $t->is($loader->getLoaders(), array($loader1, $loader2), '__construct() takes an array of template loaders as its second argument'); // ->addLoader() $t->diag('->addLoader()'); $loader = new ProjectTemplateLoader(array($loader1)); $loader->addLoader($loader2); $t->is($loader->getLoaders(), array($loader1, $loader2), '->addLoader() adds a template loader at the end of the loaders'); // ->load() $t->diag('->load()'); $loader = new ProjectTemplateLoader(array($loader1, $loader2)); $t->ok($loader->load('bar') === false, '->load() returns false if the template is not found'); $t->ok($loader->load('foo', 'xml') === false, '->load() returns false if the template does not exists for the given renderer'); $t->ok($loader->load('foo') instanceof sfTemplateStorageFile, '->load() returns a sfTemplateStorageFile if the template exists');