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 $file_path = sprintf($this->fixture
->template_path(), 'cache_test');
45 $this->cache
->expects($this->once())
46 ->method('GetTemplateDataForName')
47 ->with($this->equalTo($file_path))
48 ->will($this->returnValue(NULL
));
50 $expected = file_get_contents($file_path);
52 $this->cache
->expects($this->once())
53 ->method('StoreCompiledTemplate')
54 ->with($this->equalTo($file_path),
55 $this->greaterThan(0),
56 $this->equalTo($expected));
58 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
59 $this->assertEquals($expected, $this->fixture
->Load('cache_test')->template());
62 public function testCacheHit()
64 $file_path = sprintf($this->fixture
->template_path(), 'cache_test');
66 $expected = 'Cache hit!';
67 $this->cache
->expects($this->once())
68 ->method('GetTemplateDataForName')
69 ->with($this->equalTo($file_path))
70 ->will($this->returnValue($expected));
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());
79 public function testSingleton()
81 $fixture = views\TemplateLoader
::GetInstance();
82 $this->assertNotSame($fixture, $this->fixture
);
84 views\TemplateLoader
::SetInstance($this->fixture
);
85 $this->assertSame(views\TemplateLoader
::GetInstance(), $this->fixture
);
87 $template = views\TemplateLoader
::Fetch('cache_test');
88 $this->assertEquals('This file should be cached.', $template->template());