ChainLoaderTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Component\Templating\Loader;
  11. require_once __DIR__.'/../Fixtures/ProjectTemplateDebugger.php';
  12. use Symfony\Component\Templating\Loader\ChainLoader;
  13. use Symfony\Component\Templating\Loader\FilesystemLoader;
  14. use Symfony\Component\Templating\Storage\FileStorage;
  15. class ChainLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $loader1;
  18. protected $loader2;
  19. public 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(array('name' => 'bar', 'engine' => 'php')), '->load() returns false if the template is not found');
  40. $this->assertFalse($loader->load(array('name' => 'foo', 'engine' => 'php')), '->load() returns false if the template does not exists for the given renderer');
  41. $this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $loader->load(array('name' => 'foo.php', 'engine' => 'php')), '->load() returns a FileStorage if the template exists');
  42. }
  43. }
  44. class ProjectTemplateLoader1 extends ChainLoader
  45. {
  46. public function getLoaders()
  47. {
  48. return $this->loaders;
  49. }
  50. }