CacheLoaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. use Symfony\Components\Templating\Storage\StringStorage;
  16. $t = new LimeTest(9);
  17. class ProjectTemplateLoader extends CacheLoader
  18. {
  19. public function getDir()
  20. {
  21. return $this->dir;
  22. }
  23. public function getLoader()
  24. {
  25. return $this->loader;
  26. }
  27. }
  28. class ProjectTemplateLoaderVar extends Loader
  29. {
  30. public function getIndexTemplate()
  31. {
  32. return 'Hello World';
  33. }
  34. public function getSpecialTemplate()
  35. {
  36. return 'Hello {{ name }}';
  37. }
  38. public function load($template, array $options = array())
  39. {
  40. if (method_exists($this, $method = 'get'.ucfirst($template).'Template'))
  41. {
  42. return new StringStorage($this->$method());
  43. }
  44. return false;
  45. }
  46. }
  47. class CompilableTemplateLoader extends ProjectTemplateLoaderVar implements CompilableLoaderInterface
  48. {
  49. public function compile($template)
  50. {
  51. return preg_replace('/{{\s*([a-zA-Z0-9_]+)\s*}}/', '<?php echo $$1 ?>', $template);
  52. }
  53. }
  54. // __construct()
  55. $t->diag('__construct()');
  56. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), sys_get_temp_dir());
  57. $t->ok($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument');
  58. $t->is($loader->getDir(), sys_get_temp_dir(), '__construct() takes a directory where to store the cache as its second argument');
  59. // ->load()
  60. $t->diag('->load()');
  61. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  62. mkdir($dir, 0777, true);
  63. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir);
  64. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  65. $t->ok($loader->load('foo') === false, '->load() returns false if the embed loader is not able to load the template');
  66. $loader->load('index');
  67. $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  68. $loader->load('index');
  69. $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  70. $t->diag('load() template compilation');
  71. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  72. mkdir($dir, 0777, true);
  73. $loader = new ProjectTemplateLoader(new CompilableTemplateLoader(), $dir);
  74. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  75. $template = $loader->load('special', array('renderer' => 'comp'));
  76. $t->ok($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  77. $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');
  78. $template = $loader->load('special', array('renderer' => 'comp'));
  79. $t->ok($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  80. $t->is($template->getRenderer(), 'php', '->load() changes the renderer to php if the template is compilable');