Skip to content

Commit

Permalink
Redone tests for new feature introduced by #8
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Jun 9, 2014
1 parent 253ca33 commit 5077817
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
39 changes: 18 additions & 21 deletions Lib/Twig_Loader_Cakephp.php
Expand Up @@ -7,38 +7,22 @@ class Twig_Loader_Cakephp implements Twig_LoaderInterface {
*/
public function getSource($name) {
$name = $this->resolveFileName($name);

if(file_exists( $name ) !== false) {
return file_get_contents($name);
}

throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
return file_get_contents($name);
}

/**
* @{inheritDoc}
*/
public function getCacheKey($name) {
$name = $this->resolveFileName($name);

if(file_exists( $name ) !== false) {
return $name;
}

throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
return $this->resolveFileName($name);
}

/**
* @{inheritDoc}
*/
public function isFresh($name, $time) {
$name = $this->resolveFileName($name);

if(file_exists( $name ) !== false) {
return filemtime($name) < $time;
}

throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
return filemtime($name) < $time;
}

/**
Expand All @@ -51,9 +35,22 @@ private function resolveFileName($name) {

list($plugin, $file) = pluginSplit($name);
if ($plugin === null || !CakePlugin::loaded($plugin)) {
return APP . 'View' . DS . $file . TwigView::EXT;
$paths = App::path('View');
foreach ($paths as $path) {
$filePath = $path . $file . TwigView::EXT;
if (file_exists($filePath)) {
return $filePath;
}
}

throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}

return CakePlugin::path($plugin) . 'View' . DS . $file . TwigView::EXT;
$filePath = CakePlugin::path($plugin) . 'View' . DS . $file . TwigView::EXT;
if (file_exists($filePath)) {
return $filePath;
}

throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}
}
28 changes: 26 additions & 2 deletions Test/Case/Lib/Twig_Loader_CakephpTest.php
Expand Up @@ -8,8 +8,11 @@ public function setUp() {
parent::setUp();

App::build(array(
'Plugin' => array(App::pluginPath('TwigView') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS )
'Plugin' => array(App::pluginPath('TwigView') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS ),
));
App::build(array(
'View' => array(App::pluginPath('TwigView') . 'Test' . DS . 'test_app' . DS . 'View' . DS ),
), App::RESET);
CakePlugin::load('TestTwigView');

$this->Loader = new Twig_Loader_Cakephp();
Expand All @@ -27,14 +30,28 @@ public function testGetSource() {
$this->assertSame('TwigView', $this->Loader->getSource('TestTwigView.twig'));
}

/**
* @expectedException Twig_Error_Loader
*/
public function testGetSourceNonExistingFile() {
$this->Loader->getSource('TestTwigView.no_twig');
}

public function testGetCacheKeyNoPlugin() {
$this->assertSame(APP . 'View/layout.tpl', $this->Loader->getCacheKey('layout'));
$this->assertSame(APP . 'Plugin/TwigView/Test/test_app/View/layout.tpl', $this->Loader->getCacheKey('layout'));
}

public function testGetCacheKeyPlugin() {
$this->assertSame(APP . 'Plugin/TwigView/Test/test_app/Plugin/TestTwigView/View/twig.tpl', $this->Loader->getCacheKey('TestTwigView.twig'));
}

/**
* @expectedException Twig_Error_Loader
*/
public function testGetCacheKeyPluginNonExistingFile() {
$this->Loader->getCacheKey('TestTwigView.twog');
}

public function testIsFresh() {
file_put_contents(TMP . 'TwigViewIsFreshTest', 'TwigViewIsFreshTest');
$time = filemtime(TMP . 'TwigViewIsFreshTest');
Expand All @@ -45,4 +62,11 @@ public function testIsFresh() {
unlink(TMP . 'TwigViewIsFreshTest');
}

/**
* @expectedException Twig_Error_Loader
*/
public function testIsFreshNonExistingFile() {
$this->Loader->isFresh(TMP . 'foobar' . time(), time());
}

}
1 change: 1 addition & 0 deletions Test/test_app/View/layout.tpl
@@ -0,0 +1 @@
layout
Empty file removed Test/test_app/View/twig.tpl
Empty file.

0 comments on commit 5077817

Please sign in to comment.