TemplateParser: Don't fatal on cache misses
[lhc/web/wiklou.git] / tests / phpunit / includes / TemplateParserTest.php
1 <?php
2
3 /**
4 * @group Templates
5 */
6 class TemplateParserTest extends MediaWikiTestCase {
7
8 protected $templateDir;
9
10 protected function setUp() {
11 parent::setUp();
12
13 $this->setMwGlobals( array(
14 'wgSecretKey' => 'foo',
15 'wgMemc' => new EmptyBagOStuff(),
16 ) );
17
18 $this->templateDir = dirname( __DIR__ ) . '/data/templates/';
19 }
20
21 /**
22 * @covers TemplateParser::getTemplateFilename
23 * @dataProvider provideGetTemplateFilename
24 */
25 public function testGetTemplateFilename( $dir, $name, $result, $exception = false ) {
26 if ( $exception ) {
27 $this->setExpectedException( $exception );
28 }
29
30 $tp = new TemplateParser( $dir );
31 $path = $tp->getTemplateFilename( $name );
32 $this->assertEquals( $result, $path );
33 }
34
35 public static function provideGetTemplateFilename() {
36 return array(
37 array(
38 'dir/templates',
39 'foobar',
40 'dir/templates/foobar.mustache',
41 ),
42 array(
43 'dir/templates',
44 '../foobar',
45 '',
46 'UnexpectedValueException'
47 ),
48 );
49 }
50
51 /**
52 * @covers TemplateParser::getTemplate
53 */
54 public function testGetTemplate() {
55 $tp = new TemplateParser( $this->templateDir );
56 $this->assertTrue( is_callable( $tp->getTemplate( 'foobar' ) ) );
57 }
58
59 /**
60 * @covers TemplateParser::compile
61 */
62 public function testTemplateCompilation() {
63 $this->assertRegExp(
64 '/^<\?php return function/',
65 TemplateParser::compile( "test" ),
66 'compile a simple mustache template'
67 );
68 }
69
70 /**
71 * @covers TemplateParser::compile
72 */
73 public function testTemplateCompilationWithVariable() {
74 $this->assertRegExp(
75 '/return \'\'\.htmlentities\(\(string\)\(\(isset\(\$in\[\'value\'\]\) && '
76 . 'is_array\(\$in\)\) \? \$in\[\'value\'\] : null\), ENT_QUOTES, '
77 . '\'UTF-8\'\)\.\'\';/',
78 TemplateParser::compile( "{{value}}" ),
79 'compile a mustache template with an escaped variable'
80 );
81 }
82 }