FilesystemLoaderTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. require_once __DIR__.'/../../../../bootstrap.php';
  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. $fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/Templating/');
  15. $t = new LimeTest(15);
  16. class ProjectTemplateLoader extends FilesystemLoader
  17. {
  18. public function getTemplatePathPatterns()
  19. {
  20. return $this->templatePathPatterns;
  21. }
  22. static public function isAbsolutePath($path)
  23. {
  24. return parent::isAbsolutePath($path);
  25. }
  26. }
  27. // ->isAbsolutePath()
  28. $t->diag('->isAbsolutePath()');
  29. $t->ok(ProjectTemplateLoader::isAbsolutePath('/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  30. $t->ok(ProjectTemplateLoader::isAbsolutePath('c:\\\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  31. $t->ok(ProjectTemplateLoader::isAbsolutePath('c:/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  32. $t->ok(ProjectTemplateLoader::isAbsolutePath('\\server\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
  33. // __construct()
  34. $t->diag('__construct()');
  35. $pathPattern = $fixturesPath.'/templates/%name%.%renderer%';
  36. $path = $fixturesPath.'/templates';
  37. $loader = new ProjectTemplateLoader($pathPattern);
  38. $t->is($loader->getTemplatePathPatterns(), array($pathPattern), '__construct() takes a path as its second argument');
  39. $loader = new ProjectTemplateLoader(array($pathPattern));
  40. $t->is($loader->getTemplatePathPatterns(), array($pathPattern), '__construct() takes an array of paths as its second argument');
  41. // ->load()
  42. $t->diag('->load()');
  43. $loader = new ProjectTemplateLoader($pathPattern);
  44. $storage = $loader->load($path.'/foo.php');
  45. $t->ok($storage instanceof FileStorage, '->load() returns a FileStorage if you pass an absolute path');
  46. $t->is((string) $storage, $path.'/foo.php', '->load() returns a FileStorage pointing to the passed absolute path');
  47. $t->ok($loader->load('bar') === false, '->load() returns false if the template is not found');
  48. $storage = $loader->load('foo');
  49. $t->ok($storage instanceof FileStorage, '->load() returns a FileStorage if you pass a relative template that exists');
  50. $t->is((string) $storage, $path.'/foo.php', '->load() returns a FileStorage pointing to the absolute path of the template');
  51. $loader = new ProjectTemplateLoader($pathPattern);
  52. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  53. $t->ok($loader->load('foo', array('renderer' => 'xml')) === false, '->load() returns false if the template does not exists for the given renderer');
  54. $t->ok($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
  55. $loader = new ProjectTemplateLoader(array($fixturesPath.'/null/%name%', $pathPattern));
  56. $loader->setDebugger($debugger = new ProjectTemplateDebugger());
  57. $loader->load('foo');
  58. $t->ok($debugger->hasMessage('Failed loading template'), '->load() logs a "Failed loading template" message if the template is not found');
  59. $t->ok($debugger->hasMessage('Loaded template file'), '->load() logs a "Loaded template file" message if the template is found');