Skip to content

Commit

Permalink
Add test coverage for auto_reload
Browse files Browse the repository at this point in the history
  • Loading branch information
znerol authored and fabpot committed Oct 4, 2015
1 parent 821464a commit 1769f84
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/Twig/Tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,88 @@ public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
unlink($key);
}

public function testAutoReloadCacheMiss()
{
$template_name = __FUNCTION__;
$template_content = __FUNCTION__;

$cache = $this->getMock('Twig_CacheInterface');
$loader = $this->getMockLoader($template_name, $template_content);
$options = array('cache' => $cache, 'auto_reload' => true, 'debug' => false);
$twig = new Twig_Environment($loader, $options);

// Cache miss: getTimestamp returns 0 and as a result the load() is
// skipped.
$cache->expects($this->once())
->method('generateKey')
->will($this->returnValue('key'));
$cache->expects($this->once())
->method('getTimestamp')
->will($this->returnValue(0));
$loader->expects($this->never())
->method('isFresh');
$cache->expects($this->never())
->method('load');

$twig->loadTemplate($template_name);
}

public function testAutoReloadCacheHit()
{
$template_name = __FUNCTION__;
$template_content = __FUNCTION__;

$cache = $this->getMock('Twig_CacheInterface');
$loader = $this->getMockLoader($template_name, $template_content);
$options = array('cache' => $cache, 'auto_reload' => true, 'debug' => false);
$twig = new Twig_Environment($loader, $options);

$now = time();

// Cache hit: getTimestamp returns something > extension timestamps and
// the loader returns true for isFresh().
$cache->expects($this->once())
->method('generateKey')
->will($this->returnValue('key'));
$cache->expects($this->once())
->method('getTimestamp')
->will($this->returnValue($now));
$loader->expects($this->once())
->method('isFresh')
->will($this->returnValue(true));
$cache->expects($this->once())
->method('load');

$twig->loadTemplate($template_name);
}

public function testAutoReloadOutdatedCacheHit()
{
$template_name = __FUNCTION__;
$template_content = __FUNCTION__;

$cache = $this->getMock('Twig_CacheInterface');
$loader = $this->getMockLoader($template_name, $template_content);
$options = array('cache' => $cache, 'auto_reload' => true, 'debug' => false);
$twig = new Twig_Environment($loader, $options);

$now = time();

$cache->expects($this->once())
->method('generateKey')
->will($this->returnValue('key'));
$cache->expects($this->once())
->method('getTimestamp')
->will($this->returnValue($now));
$loader->expects($this->once())
->method('isFresh')
->will($this->returnValue(false));
$cache->expects($this->never())
->method('load');

$twig->loadTemplate($template_name);
}

public function testAddExtension()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
Expand Down Expand Up @@ -212,6 +294,21 @@ public function testRemoveExtension()
$this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
$this->assertCount(2, $twig->getNodeVisitors());
}

protected function getMockLoader($template_name, $template_content)
{
$loader = $this->getMock('Twig_LoaderInterface');
$loader->expects($this->any())
->method('getSource')
->with($template_name)
->will($this->returnValue($template_content));
$loader->expects($this->any())
->method('getCacheKey')
->with($template_name)
->will($this->returnValue($template_name));

return $loader;
}
}

class Twig_Tests_EnvironmentTest_Extension extends Twig_Extension
Expand Down

0 comments on commit 1769f84

Please sign in to comment.