CacheLoaderTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../../bootstrap.php';
  11. require_once __DIR__.'/../../../../../lib/SymfonyTests/Components/Templating/ProjectTemplateDebugger.php';
  12. use Symfony\Components\Templating\Loader\Loader;
  13. use Symfony\Components\Templating\Loader\CacheLoader;
  14. use Symfony\Components\Templating\Loader\CompilableLoaderInterface;
  15. $t = new LimeTest(9);
  16. class ProjectTemplateLoader extends CacheLoader
  17. {
  18. public function getDir()
  19. {
  20. return $this->dir;
  21. }
  22. public function getLoader()
  23. {
  24. return $this->loader;
  25. }
  26. }
  27. class ProjectTemplateLoaderVar extends Loader
  28. {
  29. public function getIndexTemplate()
  30. {
  31. return 'Hello World';
  32. }
  33. public function getSpecialTemplate()
  34. {
  35. return 'Hello {{ name }}';
  36. }
  37. public function load($template, array $options = array())
  38. {
  39. if (method_exists($this, $method = 'get'.ucfirst($template).'Template'))
  40. {
  41. return $this->$method();
  42. }
  43. return false;
  44. }
  45. }
  46. class CompilableTemplateLoader extends ProjectTemplateLoaderVar implements CompilableLoaderInterface
  47. {
  48. public function compile($template)
  49. {
  50. return preg_replace('/{{\s*([a-zA-Z0-9_]+)\s*}}/', '<?php echo $$1 ?>', $template);
  51. }
  52. }
  53. // __construct()
  54. $t->diag('__construct()');
  55. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), sys_get_temp_dir());
  56. $t->ok($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument');
  57. $t->is($loader->getDir(), sys_get_temp_dir(), '__construct() takes a directory where to store the cache as its second argument');
  58. // ->load()
  59. $t->diag('->load()');
  60. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  61. mkdir($dir, 0777, true);
  62. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir);
  63. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  64. $t->ok($loader->load('foo') === false, '->load() returns false if the embed loader is not able to load the template');
  65. $loader->load('index');
  66. $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  67. $loader->load('index');
  68. $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  69. $t->diag('load() template compilation');
  70. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  71. mkdir($dir, 0777, true);
  72. $loader = new ProjectTemplateLoader(new CompilableTemplateLoader(), $dir);
  73. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  74. $template = $loader->load('special', array('renderer' => 'comp'));
  75. $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  76. $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');
  77. $template = $loader->load('special', array('renderer' => 'comp'));
  78. $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  79. $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');