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 12use Twig\Loader\ArrayLoader; 13use Twig\Loader\ChainLoader; 14use Twig\Loader\ExistsLoaderInterface; 15use Twig\Loader\FilesystemLoader; 16use Twig\Loader\LoaderInterface; 17use Twig\Loader\SourceContextLoaderInterface; 18use Twig\Source; 19 20class Twig_Tests_Loader_ChainTest extends \PHPUnit\Framework\TestCase 21{ 22 /** 23 * @group legacy 24 */ 25 public function testGetSource() 26 { 27 $loader = new ChainLoader([ 28 new ArrayLoader(['foo' => 'bar']), 29 new ArrayLoader(['foo' => 'foobar', 'bar' => 'foo']), 30 ]); 31 32 $this->assertEquals('bar', $loader->getSource('foo')); 33 $this->assertEquals('foo', $loader->getSource('bar')); 34 } 35 36 public function testGetSourceContext() 37 { 38 $path = __DIR__.'/../Fixtures'; 39 $loader = new ChainLoader([ 40 new ArrayLoader(['foo' => 'bar']), 41 new ArrayLoader(['errors/index.html' => 'baz']), 42 new FilesystemLoader([$path]), 43 ]); 44 45 $this->assertEquals('foo', $loader->getSourceContext('foo')->getName()); 46 $this->assertSame('', $loader->getSourceContext('foo')->getPath()); 47 48 $this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName()); 49 $this->assertSame('', $loader->getSourceContext('errors/index.html')->getPath()); 50 $this->assertEquals('baz', $loader->getSourceContext('errors/index.html')->getCode()); 51 52 $this->assertEquals('errors/base.html', $loader->getSourceContext('errors/base.html')->getName()); 53 $this->assertEquals(realpath($path.'/errors/base.html'), realpath($loader->getSourceContext('errors/base.html')->getPath())); 54 $this->assertNotEquals('baz', $loader->getSourceContext('errors/base.html')->getCode()); 55 } 56 57 /** 58 * @expectedException \Twig\Error\LoaderError 59 */ 60 public function testGetSourceContextWhenTemplateDoesNotExist() 61 { 62 $loader = new ChainLoader([]); 63 64 $loader->getSourceContext('foo'); 65 } 66 67 /** 68 * @group legacy 69 * @expectedException \Twig\Error\LoaderError 70 */ 71 public function testGetSourceWhenTemplateDoesNotExist() 72 { 73 $loader = new ChainLoader([]); 74 75 $loader->getSource('foo'); 76 } 77 78 public function testGetCacheKey() 79 { 80 $loader = new ChainLoader([ 81 new ArrayLoader(['foo' => 'bar']), 82 new ArrayLoader(['foo' => 'foobar', 'bar' => 'foo']), 83 ]); 84 85 $this->assertEquals('foo:bar', $loader->getCacheKey('foo')); 86 $this->assertEquals('bar:foo', $loader->getCacheKey('bar')); 87 } 88 89 /** 90 * @expectedException \Twig\Error\LoaderError 91 */ 92 public function testGetCacheKeyWhenTemplateDoesNotExist() 93 { 94 $loader = new ChainLoader([]); 95 96 $loader->getCacheKey('foo'); 97 } 98 99 public function testAddLoader() 100 { 101 $loader = new ChainLoader(); 102 $loader->addLoader(new ArrayLoader(['foo' => 'bar'])); 103 104 $this->assertEquals('bar', $loader->getSourceContext('foo')->getCode()); 105 } 106 107 public function testExists() 108 { 109 $loader1 = $this->getMockBuilder('Twig_ChainTestLoaderWithExistsInterface')->getMock(); 110 $loader1->expects($this->once())->method('exists')->will($this->returnValue(false)); 111 $loader1->expects($this->never())->method('getSourceContext'); 112 113 // can be removed in 2.0 114 $loader2 = $this->getMockBuilder('Twig_ChainTestLoaderInterface')->getMock(); 115 //$loader2 = $this->getMockBuilder(['\Twig\Loader\LoaderInterface', '\Twig\Loader\SourceContextLoaderInterface'])->getMock(); 116 $loader2->expects($this->once())->method('getSourceContext')->will($this->returnValue(new Source('content', 'index'))); 117 118 $loader = new ChainLoader(); 119 $loader->addLoader($loader1); 120 $loader->addLoader($loader2); 121 122 $this->assertTrue($loader->exists('foo')); 123 } 124} 125 126interface Twig_ChainTestLoaderInterface extends LoaderInterface, SourceContextLoaderInterface 127{ 128} 129 130interface Twig_ChainTestLoaderWithExistsInterface extends LoaderInterface, ExistsLoaderInterface, SourceContextLoaderInterface 131{ 132} 133