Cache templates with the full template path rather than just the name.
[hoplite.git] / testing / tests / views / template_loader_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\views as views;
19
20 require_once HOPLITE_ROOT . '/views/cache_backend.php';
21 require_once HOPLITE_ROOT . '/views/template_loader.php';
22
23 class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
24 {
25 public function setUp()
26 {
27 $this->fixture = new views\TemplateLoader();
28 $this->fixture->set_template_path(dirname(__FILE__) . '/%s.tpl');
29
30 $this->cache = $this->getMock('\\hoplite\\views\\CacheBackend');
31 $this->fixture->set_cache_backend($this->cache);
32 }
33
34 public function testTemplatePath()
35 {
36 $path = '/webapp/views/%s.tpl';
37 $this->fixture->set_template_path($path);
38 $this->assertEquals($path, $this->fixture->template_path());
39 }
40
41 public function testCacheMiss()
42 {
43 $file_path = sprintf($this->fixture->template_path(), 'cache_test');
44
45 $this->cache->expects($this->once())
46 ->method('GetTemplateDataForName')
47 ->with($this->equalTo($file_path))
48 ->will($this->returnValue(NULL));
49
50 $expected = file_get_contents($file_path);
51
52 $this->cache->expects($this->once())
53 ->method('StoreCompiledTemplate')
54 ->with($this->equalTo($file_path),
55 $this->greaterThan(0),
56 $this->equalTo($expected));
57
58 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
59 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
60 }
61
62 public function testCacheHit()
63 {
64 $file_path = sprintf($this->fixture->template_path(), 'cache_test');
65
66 $expected = 'Cache hit!';
67 $this->cache->expects($this->once())
68 ->method('GetTemplateDataForName')
69 ->with($this->equalTo($file_path))
70 ->will($this->returnValue($expected));
71
72 // The cache backend is only consulted once.
73 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
74 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
75 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
76 $this->assertEquals($expected, $this->fixture->Load('cache_test')->template());
77 }
78
79 public function testSingleton()
80 {
81 $fixture = views\TemplateLoader::GetInstance();
82 $this->assertNotSame($fixture, $this->fixture);
83
84 views\TemplateLoader::SetInstance($this->fixture);
85 $this->assertSame(views\TemplateLoader::GetInstance(), $this->fixture);
86
87 $template = views\TemplateLoader::Fetch('cache_test');
88 $this->assertEquals('This file should be cached.', $template->template());
89 }
90 }