FilesystemLoaderTest.php 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Component\Templating\Tests\Loader;
  11. use Symfony\Component\Templating\Loader\FilesystemLoader;
  12. use Symfony\Component\Templating\Storage\FileStorage;
  13. use Symfony\Component\Templating\TemplateReference;
  14. class FilesystemLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected static $fixturesPath;
  17. public static function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  20. }
  21. public function testConstructor()
  22. {
  23. $pathPattern = self::$fixturesPath.'/templates/%name%.%engine%';
  24. $path = self::$fixturesPath.'/templates';
  25. $loader = new ProjectTemplateLoader2($pathPattern);
  26. $this->assertEquals(array($pathPattern), $loader->getTemplatePathPatterns(), '__construct() takes a path as its second argument');
  27. $loader = new ProjectTemplateLoader2(array($pathPattern));
  28. $this->assertEquals(array($pathPattern), $loader->getTemplatePathPatterns(), '__construct() takes an array of paths as its second argument');
  29. }
  30. public function testIsAbsolutePath()
  31. {
  32. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  33. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:\\\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  34. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  35. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('\\server\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  36. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('https://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  37. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('phar://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  38. }
  39. public function testLoad()
  40. {
  41. $pathPattern = self::$fixturesPath.'/templates/%name%';
  42. $path = self::$fixturesPath.'/templates';
  43. $loader = new ProjectTemplateLoader2($pathPattern);
  44. $storage = $loader->load(new TemplateReference($path.'/foo.php', 'php'));
  45. $this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass an absolute path');
  46. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the passed absolute path');
  47. $this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
  48. $storage = $loader->load(new TemplateReference('foo.php', 'php'));
  49. $this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
  50. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
  51. $loader = new ProjectTemplateLoader2($pathPattern);
  52. $loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
  53. $this->assertFalse($loader->load(new TemplateReference('foo.xml', 'php')), '->load() returns false if the template does not exist for the given engine');
  54. $this->assertTrue($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
  55. $loader = new ProjectTemplateLoader2(array(self::$fixturesPath.'/null/%name%', $pathPattern));
  56. $loader->setDebugger($debugger = new \Symfony\Component\Templating\Tests\Fixtures\ProjectTemplateDebugger());
  57. $loader->load(new TemplateReference('foo.php', 'php'));
  58. $this->assertTrue($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');
  59. }
  60. }
  61. class ProjectTemplateLoader2 extends FilesystemLoader
  62. {
  63. public function getTemplatePathPatterns()
  64. {
  65. return $this->templatePathPatterns;
  66. }
  67. public static function isAbsolutePath($path)
  68. {
  69. return parent::isAbsolutePath($path);
  70. }
  71. }