3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite\test
;
18 use hoplite\views
as views
;
20 require_once HOPLITE_ROOT
. '/views/cache_backend.php';
21 require_once HOPLITE_ROOT
. '/views/template_loader.php';
23 class TemplateLoaderTest
extends \PHPUnit_Framework_TestCase
25 public function setUp()
27 $this->fixture
= new views\
TemplateLoader();
28 $this->fixture
->set_template_path(dirname(__FILE__
) . '/%s.tpl');
30 $this->cache
= $this->getMock('\\hoplite\\views\\CacheBackend');
31 $this->fixture
->set_cache_backend($this->cache
);
34 public function testTemplatePath()
36 $path = '/webapp/views/%s.tpl';
37 $this->fixture
->set_template_path($path);
38 $this->assertEquals($path, $this->fixture
->template_path());
41 public function testCacheMiss()
43 $this->cache
->expects($this->once())
44 ->method('GetTemplateDataForName')
45 ->with($this->equalTo('cache_test'))
46 ->will($this->returnValue(NULL));
48 $expected = file_get_contents(sprintf($this->fixture
->template_path(), 'cache_test'));
50 $this->cache
->expects($this->once())
51 ->method('StoreCompiledTemplate')
52 ->with($this->equalTo('cache_test'),
53 $this->greaterThan(0),
54 $this->equalTo($expected));
56 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
57 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
60 public function testCacheHit()
62 $expected = 'Cache hit!';
63 $this->cache
->expects($this->once())
64 ->method('GetTemplateDataForName')
65 ->with($this->equalTo('cache_test'))
66 ->will($this->returnValue($expected));
68 // The cache backend is only consulted once.
69 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
70 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
71 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
72 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
75 public function testSingleton()
77 $fixture = views\TemplateLoader
::GetInstance();
78 $this->assertNotSame($fixture, $this->fixture
);
80 views\TemplateLoader
::SetInstance($this->fixture
);
81 $this->assertSame(views\TemplateLoader
::GetInstance(), $this->fixture
);
83 $template = views\TemplateLoader
::Fetch('cache_test');
84 $this->assertEquals('This file should be cached.', $template->template());