FilesystemLoaderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Bundle\TwigBundle\Tests\Loader;
  11. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  12. use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
  13. use Symfony\Bundle\TwigBundle\Tests\TestCase;
  14. class FilesystemLoaderTest extends TestCase
  15. {
  16. public function testGetSourceContext()
  17. {
  18. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  19. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  20. $locator
  21. ->expects($this->once())
  22. ->method('locate')
  23. ->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
  24. ;
  25. $loader = new FilesystemLoader($locator, $parser);
  26. $loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace');
  27. // Twig-style
  28. $this->assertEquals("This is a layout\n", $loader->getSourceContext('@namespace/layout.html.twig')->getCode());
  29. // Symfony-style
  30. $this->assertEquals("This is a layout\n", $loader->getSourceContext('TwigBundle::layout.html.twig')->getCode());
  31. }
  32. public function testExists()
  33. {
  34. // should return true for templates that Twig does not find, but Symfony does
  35. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  36. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  37. $locator
  38. ->expects($this->once())
  39. ->method('locate')
  40. ->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
  41. ;
  42. $loader = new FilesystemLoader($locator, $parser);
  43. $this->assertTrue($loader->exists($template));
  44. }
  45. /**
  46. * @expectedException \Twig_Error_Loader
  47. */
  48. public function testTwigErrorIfLocatorThrowsInvalid()
  49. {
  50. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  51. $parser
  52. ->expects($this->once())
  53. ->method('parse')
  54. ->with('name.format.engine')
  55. ->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine')))
  56. ;
  57. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  58. $locator
  59. ->expects($this->once())
  60. ->method('locate')
  61. ->will($this->throwException(new \InvalidArgumentException('Unable to find template "NonExistent".')))
  62. ;
  63. $loader = new FilesystemLoader($locator, $parser);
  64. $loader->getCacheKey('name.format.engine');
  65. }
  66. /**
  67. * @expectedException \Twig_Error_Loader
  68. */
  69. public function testTwigErrorIfLocatorReturnsFalse()
  70. {
  71. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  72. $parser
  73. ->expects($this->once())
  74. ->method('parse')
  75. ->with('name.format.engine')
  76. ->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine')))
  77. ;
  78. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  79. $locator
  80. ->expects($this->once())
  81. ->method('locate')
  82. ->will($this->returnValue(false))
  83. ;
  84. $loader = new FilesystemLoader($locator, $parser);
  85. $loader->getCacheKey('name.format.engine');
  86. }
  87. /**
  88. * @expectedException \Twig_Error_Loader
  89. * @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*Tests.Loader.\.\..DependencyInjection.Fixtures.Resources.views\)/
  90. */
  91. public function testTwigErrorIfTemplateDoesNotExist()
  92. {
  93. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  94. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  95. $loader = new FilesystemLoader($locator, $parser);
  96. $loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
  97. $method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
  98. $method->setAccessible(true);
  99. $method->invoke($loader, 'name.format.engine');
  100. }
  101. public function testTwigSoftErrorIfTemplateDoesNotExist()
  102. {
  103. $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
  104. $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  105. $loader = new FilesystemLoader($locator, $parser);
  106. $loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
  107. $method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
  108. $method->setAccessible(true);
  109. $this->assertFalse($method->invoke($loader, 'name.format.engine', false));
  110. }
  111. }