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; 13 14class Twig_Tests_Loader_ArrayTest extends \PHPUnit\Framework\TestCase 15{ 16 /** 17 * @group legacy 18 */ 19 public function testGetSource() 20 { 21 $loader = new ArrayLoader(['foo' => 'bar']); 22 23 $this->assertEquals('bar', $loader->getSource('foo')); 24 } 25 26 /** 27 * @group legacy 28 * @expectedException \Twig\Error\LoaderError 29 */ 30 public function testGetSourceWhenTemplateDoesNotExist() 31 { 32 $loader = new ArrayLoader([]); 33 34 $loader->getSource('foo'); 35 } 36 37 /** 38 * @expectedException \Twig\Error\LoaderError 39 */ 40 public function testGetSourceContextWhenTemplateDoesNotExist() 41 { 42 $loader = new ArrayLoader([]); 43 44 $loader->getSourceContext('foo'); 45 } 46 47 public function testGetCacheKey() 48 { 49 $loader = new ArrayLoader(['foo' => 'bar']); 50 51 $this->assertEquals('foo:bar', $loader->getCacheKey('foo')); 52 } 53 54 public function testGetCacheKeyWhenTemplateHasDuplicateContent() 55 { 56 $loader = new ArrayLoader([ 57 'foo' => 'bar', 58 'baz' => 'bar', 59 ]); 60 61 $this->assertEquals('foo:bar', $loader->getCacheKey('foo')); 62 $this->assertEquals('baz:bar', $loader->getCacheKey('baz')); 63 } 64 65 public function testGetCacheKeyIsProtectedFromEdgeCollisions() 66 { 67 $loader = new ArrayLoader([ 68 'foo__' => 'bar', 69 'foo' => '__bar', 70 ]); 71 72 $this->assertEquals('foo__:bar', $loader->getCacheKey('foo__')); 73 $this->assertEquals('foo:__bar', $loader->getCacheKey('foo')); 74 } 75 76 /** 77 * @expectedException \Twig\Error\LoaderError 78 */ 79 public function testGetCacheKeyWhenTemplateDoesNotExist() 80 { 81 $loader = new ArrayLoader([]); 82 83 $loader->getCacheKey('foo'); 84 } 85 86 public function testSetTemplate() 87 { 88 $loader = new ArrayLoader([]); 89 $loader->setTemplate('foo', 'bar'); 90 91 $this->assertEquals('bar', $loader->getSourceContext('foo')->getCode()); 92 } 93 94 public function testIsFresh() 95 { 96 $loader = new ArrayLoader(['foo' => 'bar']); 97 $this->assertTrue($loader->isFresh('foo', time())); 98 } 99 100 /** 101 * @expectedException \Twig\Error\LoaderError 102 */ 103 public function testIsFreshWhenTemplateDoesNotExist() 104 { 105 $loader = new ArrayLoader([]); 106 107 $loader->isFresh('foo', time()); 108 } 109 110 public function testTemplateReference() 111 { 112 $name = new Twig_Test_Loader_TemplateReference('foo'); 113 $loader = new ArrayLoader(['foo' => 'bar']); 114 115 $loader->getCacheKey($name); 116 $loader->getSourceContext($name); 117 $loader->isFresh($name, time()); 118 $loader->setTemplate($name, 'foo:bar'); 119 120 // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above 121 // can be executed without crashing PHP 122 $this->addToAssertionCount(1); 123 } 124} 125 126class Twig_Test_Loader_TemplateReference 127{ 128 private $name; 129 130 public function __construct($name) 131 { 132 $this->name = $name; 133 } 134 135 public function __toString() 136 { 137 return $this->name; 138 } 139} 140