CacheLoaderTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\TemplateReferenceInterface;
  15. use Symfony\Component\Templating\TemplateReference;
  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.mt_rand(111111, 999999);
  27. mkdir($dir, 0777, true);
  28. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir);
  29. $this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
  30. $logger = $this->getMock('Psr\Log\LoggerInterface');
  31. $logger
  32. ->expects($this->once())
  33. ->method('debug')
  34. ->with('Storing template in cache.', array('name' => 'index'));
  35. $loader->setLogger($logger);
  36. $loader->load(new TemplateReference('index'));
  37. $logger = $this->getMock('Psr\Log\LoggerInterface');
  38. $logger
  39. ->expects($this->once())
  40. ->method('debug')
  41. ->with('Fetching template from cache.', array('name' => 'index'));
  42. $loader->setLogger($logger);
  43. $loader->load(new TemplateReference('index'));
  44. }
  45. }
  46. class ProjectTemplateLoader extends CacheLoader
  47. {
  48. public function getDir()
  49. {
  50. return $this->dir;
  51. }
  52. public function getLoader()
  53. {
  54. return $this->loader;
  55. }
  56. }
  57. class ProjectTemplateLoaderVar extends Loader
  58. {
  59. public function getIndexTemplate()
  60. {
  61. return 'Hello World';
  62. }
  63. public function getSpecialTemplate()
  64. {
  65. return 'Hello {{ name }}';
  66. }
  67. public function load(TemplateReferenceInterface $template)
  68. {
  69. if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
  70. return new StringStorage($this->$method());
  71. }
  72. return false;
  73. }
  74. public function isFresh(TemplateReferenceInterface $template, $time)
  75. {
  76. return false;
  77. }
  78. }