CacheLoaderTest.php 2.6 KB

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