CacheLoaderTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Component\Templating\Loader;
  11. require_once __DIR__.'/../Fixtures/ProjectTemplateDebugger.php';
  12. use Symfony\Component\Templating\Loader\Loader;
  13. use Symfony\Component\Templating\Loader\CacheLoader;
  14. use Symfony\Component\Templating\Storage\StringStorage;
  15. use Symfony\Component\Templating\TemplateNameParser;
  16. class CacheLoaderTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), 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(new TemplateNameParser()), $dir);
  29. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  30. $this->assertFalse($loader->load('foo'), '->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. }
  36. }
  37. class ProjectTemplateLoader extends CacheLoader
  38. {
  39. public function getDir()
  40. {
  41. return $this->dir;
  42. }
  43. public function getLoader()
  44. {
  45. return $this->loader;
  46. }
  47. }
  48. class ProjectTemplateLoaderVar extends Loader
  49. {
  50. public function getIndexTemplate()
  51. {
  52. return 'Hello World';
  53. }
  54. public function getSpecialTemplate()
  55. {
  56. return 'Hello {{ name }}';
  57. }
  58. public function load($template)
  59. {
  60. if (method_exists($this, $method = 'get'.ucfirst($template).'Template')) {
  61. return new StringStorage($this->$method());
  62. }
  63. return false;
  64. }
  65. public function isFresh($template, $time)
  66. {
  67. return false;
  68. }
  69. }