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