CacheLoaderTest.php 3.6 KB

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