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