Security update for Core, with self-updated composer
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / Loader / ChainTest.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 class Twig_Tests_Loader_ChainTest extends \PHPUnit\Framework\TestCase
13 {
14     /**
15      * @group legacy
16      */
17     public function testGetSource()
18     {
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')),
22         ));
23
24         $this->assertEquals('bar', $loader->getSource('foo'));
25         $this->assertEquals('foo', $loader->getSource('bar'));
26     }
27
28     public function testGetSourceContext()
29     {
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)),
35         ));
36
37         $this->assertEquals('foo', $loader->getSourceContext('foo')->getName());
38         $this->assertSame('', $loader->getSourceContext('foo')->getPath());
39
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());
43
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());
47     }
48
49     /**
50      * @expectedException Twig_Error_Loader
51      */
52     public function testGetSourceContextWhenTemplateDoesNotExist()
53     {
54         $loader = new Twig_Loader_Chain(array());
55
56         $loader->getSourceContext('foo');
57     }
58
59     /**
60      * @group legacy
61      * @expectedException Twig_Error_Loader
62      */
63     public function testGetSourceWhenTemplateDoesNotExist()
64     {
65         $loader = new Twig_Loader_Chain(array());
66
67         $loader->getSource('foo');
68     }
69
70     public function testGetCacheKey()
71     {
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')),
75         ));
76
77         $this->assertEquals('foo:bar', $loader->getCacheKey('foo'));
78         $this->assertEquals('bar:foo', $loader->getCacheKey('bar'));
79     }
80
81     /**
82      * @expectedException Twig_Error_Loader
83      */
84     public function testGetCacheKeyWhenTemplateDoesNotExist()
85     {
86         $loader = new Twig_Loader_Chain(array());
87
88         $loader->getCacheKey('foo');
89     }
90
91     public function testAddLoader()
92     {
93         $loader = new Twig_Loader_Chain();
94         $loader->addLoader(new Twig_Loader_Array(array('foo' => 'bar')));
95
96         $this->assertEquals('bar', $loader->getSourceContext('foo')->getCode());
97     }
98
99     public function testExists()
100     {
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');
104
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')));
109
110         $loader = new Twig_Loader_Chain();
111         $loader->addLoader($loader1);
112         $loader->addLoader($loader2);
113
114         $this->assertTrue($loader->exists('foo'));
115     }
116 }
117
118 interface Twig_ChainTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
119 {
120 }
121
122 interface Twig_ChainTestLoaderWithExistsInterface extends Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
123 {
124 }