/vendor/symfony/tests/Symfony/Tests/Component/Templating/Loader/FilesystemLoaderTest.php

https://github.com/israelnoguera/parejas · PHP · 87 lines · 64 code · 15 blank · 8 comment · 0 complexity · 9f133d320b4109155ed8ec1c9361211c MD5 · raw file

  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. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('https://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  38. $this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('phar://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  39. }
  40. public function testLoad()
  41. {
  42. $pathPattern = self::$fixturesPath.'/templates/%name%';
  43. $path = self::$fixturesPath.'/templates';
  44. $loader = new ProjectTemplateLoader2($pathPattern);
  45. $storage = $loader->load(new TemplateReference($path.'/foo.php', 'php'));
  46. $this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass an absolute path');
  47. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the passed absolute path');
  48. $this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
  49. $storage = $loader->load(new TemplateReference('foo.php', 'php'));
  50. $this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
  51. $this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
  52. $loader = new ProjectTemplateLoader2($pathPattern);
  53. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  54. $this->assertFalse($loader->load(new TemplateReference('foo.xml', 'php')), '->load() returns false if the template does not exists for the given engine');
  55. $this->assertTrue($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
  56. $loader = new ProjectTemplateLoader2(array(self::$fixturesPath.'/null/%name%', $pathPattern));
  57. $loader->setDebugger($debugger = new \ProjectTemplateDebugger());
  58. $loader->load(new TemplateReference('foo.php', 'php'));
  59. $this->assertTrue($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');
  60. }
  61. }
  62. class ProjectTemplateLoader2 extends FilesystemLoader
  63. {
  64. public function getTemplatePathPatterns()
  65. {
  66. return $this->templatePathPatterns;
  67. }
  68. static public function isAbsolutePath($path)
  69. {
  70. return parent::isAbsolutePath($path);
  71. }
  72. }