PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/tests/Symfony/Tests/Component/Templating/Loader/CacheLoaderTest.php

https://bitbucket.org/ingsol/music_sonata
PHP | 84 lines | 63 code | 13 blank | 8 comment | 1 complexity | cb5b6a0305d5ce508960d714321fd607 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  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\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. use Symfony\Component\Templating\TemplateReferenceInterface;
  17. use Symfony\Component\Templating\TemplateReference;
  18. class CacheLoaderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function testConstructor()
  21. {
  22. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), sys_get_temp_dir());
  23. $this->assertTrue($loader->getLoader() === $varLoader, '__construct() takes a template loader as its first argument');
  24. $this->assertEquals(sys_get_temp_dir(), $loader->getDir(), '__construct() takes a directory where to store the cache as its second argument');
  25. }
  26. public function testLoad()
  27. {
  28. $dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
  29. mkdir($dir, 0777, true);
  30. $loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(new TemplateNameParser()), $dir);
  31. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  32. $this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
  33. $loader->load(new TemplateReference('index'));
  34. $this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
  35. $loader->load(new TemplateReference('index'));
  36. $this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
  37. }
  38. }
  39. class ProjectTemplateLoader extends CacheLoader
  40. {
  41. public function getDir()
  42. {
  43. return $this->dir;
  44. }
  45. public function getLoader()
  46. {
  47. return $this->loader;
  48. }
  49. }
  50. class ProjectTemplateLoaderVar extends Loader
  51. {
  52. public function getIndexTemplate()
  53. {
  54. return 'Hello World';
  55. }
  56. public function getSpecialTemplate()
  57. {
  58. return 'Hello {{ name }}';
  59. }
  60. public function load(TemplateReferenceInterface $template)
  61. {
  62. if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
  63. return new StringStorage($this->$method());
  64. }
  65. return false;
  66. }
  67. public function isFresh(TemplateReferenceInterface $template, $time)
  68. {
  69. return false;
  70. }
  71. }