TemplateLocatorTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\FrameworkBundle\Tests\Templating\Loader;
  11. use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
  12. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  13. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  14. class TemplateLocatorTest extends TestCase
  15. {
  16. public function testLocateATemplate()
  17. {
  18. $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
  19. $fileLocator = $this->getFileLocator();
  20. $fileLocator
  21. ->expects($this->once())
  22. ->method('locate')
  23. ->with($template->getPath())
  24. ->will($this->returnValue('/path/to/template'))
  25. ;
  26. $locator = new TemplateLocator($fileLocator);
  27. $this->assertEquals('/path/to/template', $locator->locate($template));
  28. }
  29. public function testLocateATemplateFromCacheDir()
  30. {
  31. $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
  32. $fileLocator = $this->getFileLocator();
  33. $locator = new TemplateLocator($fileLocator, __DIR__.'/../../Fixtures');
  34. $this->assertEquals(realpath(__DIR__.'/../../Fixtures/Resources/views/this.is.a.template.format.engine'), $locator->locate($template));
  35. }
  36. public function testThrowsExceptionWhenTemplateNotFound()
  37. {
  38. $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
  39. $fileLocator = $this->getFileLocator();
  40. $errorMessage = 'FileLocator exception message';
  41. $fileLocator
  42. ->expects($this->once())
  43. ->method('locate')
  44. ->will($this->throwException(new \InvalidArgumentException($errorMessage)))
  45. ;
  46. $locator = new TemplateLocator($fileLocator);
  47. try {
  48. $locator->locate($template);
  49. $this->fail('->locate() should throw an exception when the file is not found.');
  50. } catch (\InvalidArgumentException $e) {
  51. $this->assertContains(
  52. $errorMessage,
  53. $e->getMessage(),
  54. 'TemplateLocator exception should propagate the FileLocator exception message'
  55. );
  56. }
  57. }
  58. /**
  59. * @expectedException \InvalidArgumentException
  60. */
  61. public function testThrowsAnExceptionWhenTemplateIsNotATemplateReferenceInterface()
  62. {
  63. $locator = new TemplateLocator($this->getFileLocator());
  64. $locator->locate('template');
  65. }
  66. protected function getFileLocator()
  67. {
  68. return $this
  69. ->getMockBuilder('Symfony\Component\Config\FileLocator')
  70. ->setMethods(array('locate'))
  71. ->setConstructorArgs(array('/path/to/fallback'))
  72. ->getMock()
  73. ;
  74. }
  75. }