CacheLoaderTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Templating\Tests\Loader;
  11. use Symfony\Component\Templating\Loader\Loader;
  12. use Symfony\Component\Templating\Loader\CacheLoader;
  13. use Symfony\Component\Templating\Storage\StringStorage;
  14. use Symfony\Component\Templating\TemplateNameParser;
  15. use Symfony\Component\Templating\TemplateReferenceInterface;
  16. use Symfony\Component\Templating\TemplateReference;
  17. class CacheLoaderTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testConstructor()
  20. {
  21. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), sys_get_temp_dir());
  22. $this->assertTrue($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument');
  23. $this->assertEquals(sys_get_temp_dir(), $loader->getDir(), '__construct() takes a directory where to store the cache as its second argument');
  24. }
  25. public function testLoad()
  26. {
  27. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  28. mkdir($dir, 0777, true);
  29. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), $dir);
  30. $loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
  31. $this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
  32. $loader->load(new TemplateReference('index'));
  33. $this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  34. $loader->load(new TemplateReference('index'));
  35. $this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  36. }
  37. }
  38. class ProjectTemplateLoader extends CacheLoader
  39. {
  40. public function getDir()
  41. {
  42. return $this->dir;
  43. }
  44. public function getLoader()
  45. {
  46. return $this->loader;
  47. }
  48. }
  49. class ProjectTemplateLoaderVar extends Loader
  50. {
  51. public function getIndexTemplate()
  52. {
  53. return 'Hello World';
  54. }
  55. public function getSpecialTemplate()
  56. {
  57. return 'Hello {{ name }}';
  58. }
  59. public function load(TemplateReferenceInterface $template)
  60. {
  61. if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
  62. return new StringStorage($this->$method());
  63. }
  64. return false;
  65. }
  66. public function isFresh(TemplateReferenceInterface $template, $time)
  67. {
  68. return false;
  69. }
  70. }