ChainLoaderTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\ChainLoader;
  12. use Symfony\Component\Templating\Loader\FilesystemLoader;
  13. use Symfony\Component\Templating\Storage\FileStorage;
  14. use Symfony\Component\Templating\TemplateReference;
  15. class ChainLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $loader1;
  18. protected $loader2;
  19. protected function setUp()
  20. {
  21. $fixturesPath = realpath(__DIR__.'/../Fixtures/');
  22. $this->loader1 = new FilesystemLoader($fixturesPath.'/null/%name%');
  23. $this->loader2 = new FilesystemLoader($fixturesPath.'/templates/%name%');
  24. }
  25. public function testConstructor()
  26. {
  27. $loader = new ProjectTemplateLoader1(array($this->loader1, $this->loader2));
  28. $this->assertEquals(array($this->loader1, $this->loader2), $loader->getLoaders(), '__construct() takes an array of template loaders as its second argument');
  29. }
  30. public function testAddLoader()
  31. {
  32. $loader = new ProjectTemplateLoader1(array($this->loader1));
  33. $loader->addLoader($this->loader2);
  34. $this->assertEquals(array($this->loader1, $this->loader2), $loader->getLoaders(), '->addLoader() adds a template loader at the end of the loaders');
  35. }
  36. public function testLoad()
  37. {
  38. $loader = new ProjectTemplateLoader1(array($this->loader1, $this->loader2));
  39. $this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
  40. $this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the template does not exist for the given renderer');
  41. $this->assertInstanceOf(
  42. 'Symfony\Component\Templating\Storage\FileStorage',
  43. $loader->load(new TemplateReference('foo.php', 'php')),
  44. '->load() returns a FileStorage if the template exists'
  45. );
  46. }
  47. }
  48. class ProjectTemplateLoader1 extends ChainLoader
  49. {
  50. public function getLoaders()
  51. {
  52. return $this->loaders;
  53. }
  54. }