4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_Loader_ChainTest extends \PHPUnit\Framework\TestCase
17 public function testGetSource()
19 $loader = new Twig_Loader_Chain(array(
20 new Twig_Loader_Array(array('foo' => 'bar')),
21 new Twig_Loader_Array(array('foo' => 'foobar', 'bar' => 'foo')),
24 $this->assertEquals('bar', $loader->getSource('foo'));
25 $this->assertEquals('foo', $loader->getSource('bar'));
28 public function testGetSourceContext()
30 $path = dirname(__FILE__).'/../Fixtures';
31 $loader = new Twig_Loader_Chain(array(
32 new Twig_Loader_Array(array('foo' => 'bar')),
33 new Twig_Loader_Array(array('errors/index.html' => 'baz')),
34 new Twig_Loader_Filesystem(array($path)),
37 $this->assertEquals('foo', $loader->getSourceContext('foo')->getName());
38 $this->assertSame('', $loader->getSourceContext('foo')->getPath());
40 $this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName());
41 $this->assertSame('', $loader->getSourceContext('errors/index.html')->getPath());
42 $this->assertEquals('baz', $loader->getSourceContext('errors/index.html')->getCode());
44 $this->assertEquals('errors/base.html', $loader->getSourceContext('errors/base.html')->getName());
45 $this->assertEquals(realpath($path.'/errors/base.html'), realpath($loader->getSourceContext('errors/base.html')->getPath()));
46 $this->assertNotEquals('baz', $loader->getSourceContext('errors/base.html')->getCode());
50 * @expectedException Twig_Error_Loader
52 public function testGetSourceContextWhenTemplateDoesNotExist()
54 $loader = new Twig_Loader_Chain(array());
56 $loader->getSourceContext('foo');
61 * @expectedException Twig_Error_Loader
63 public function testGetSourceWhenTemplateDoesNotExist()
65 $loader = new Twig_Loader_Chain(array());
67 $loader->getSource('foo');
70 public function testGetCacheKey()
72 $loader = new Twig_Loader_Chain(array(
73 new Twig_Loader_Array(array('foo' => 'bar')),
74 new Twig_Loader_Array(array('foo' => 'foobar', 'bar' => 'foo')),
77 $this->assertEquals('foo:bar', $loader->getCacheKey('foo'));
78 $this->assertEquals('bar:foo', $loader->getCacheKey('bar'));
82 * @expectedException Twig_Error_Loader
84 public function testGetCacheKeyWhenTemplateDoesNotExist()
86 $loader = new Twig_Loader_Chain(array());
88 $loader->getCacheKey('foo');
91 public function testAddLoader()
93 $loader = new Twig_Loader_Chain();
94 $loader->addLoader(new Twig_Loader_Array(array('foo' => 'bar')));
96 $this->assertEquals('bar', $loader->getSourceContext('foo')->getCode());
99 public function testExists()
101 $loader1 = $this->getMockBuilder('Twig_ChainTestLoaderWithExistsInterface')->getMock();
102 $loader1->expects($this->once())->method('exists')->will($this->returnValue(false));
103 $loader1->expects($this->never())->method('getSourceContext');
105 // can be removed in 2.0
106 $loader2 = $this->getMockBuilder('Twig_ChainTestLoaderInterface')->getMock();
107 //$loader2 = $this->getMockBuilder(array('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface'))->getMock();
108 $loader2->expects($this->once())->method('getSourceContext')->will($this->returnValue(new Twig_Source('content', 'index')));
110 $loader = new Twig_Loader_Chain();
111 $loader->addLoader($loader1);
112 $loader->addLoader($loader2);
114 $this->assertTrue($loader->exists('foo'));
118 interface Twig_ChainTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
122 interface Twig_ChainTestLoaderWithExistsInterface extends Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface