FilesystemLoaderTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Components\Templating\Loader;
  11. require_once __DIR__.'/../../../../../lib/SymfonyTests/Components/Templating/ProjectTemplateDebugger.php';
  12. use Symfony\Components\Templating\Loader\FilesystemLoader;
  13. use Symfony\Components\Templating\Storage\FileStorage;
  14. class FilesystemLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. static protected $fixturesPath;
  17. static public function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/Templating/');
  20. }
  21. public function testConstructor()
  22. {
  23. $pathPattern = self::$fixturesPath.'/templates/%name%.%renderer%';
  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. }
  37. public function testLoad()
  38. {
  39. $pathPattern = self::$fixturesPath.'/templates/%name%.%renderer%';
  40. $path = self::$fixturesPath.'/templates';
  41. $loader = new ProjectTemplateLoader2($pathPattern);
  42. $storage = $loader->load($path.'/foo.php');
  43. $this->assertInstanceOf('Symfony\Components\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass an absolute path');
  44. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the passed absolute path');
  45. $this->assertFalse($loader->load('bar'), '->load() returns false if the template is not found');
  46. $storage = $loader->load('foo');
  47. $this->assertInstanceOf('Symfony\Components\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
  48. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
  49. $loader = new ProjectTemplateLoader2($pathPattern);
  50. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  51. $this->assertFalse($loader->load('foo', array('renderer' => 'xml')), '->load() returns false if the template does not exists for the given renderer');
  52. $this->assertTrue($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
  53. $loader = new ProjectTemplateLoader2(array(self::$fixturesPath.'/null/%name%', $pathPattern));
  54. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  55. $loader->load('foo');
  56. $this->assertTrue($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');
  57. }
  58. }
  59. class ProjectTemplateLoader2 extends FilesystemLoader
  60. {
  61. public function getTemplatePathPatterns()
  62. {
  63. return $this->templatePathPatterns;
  64. }
  65. static public function isAbsolutePath($path)
  66. {
  67. return parent::isAbsolutePath($path);
  68. }
  69. }