From 40555fc16e6ecde653abd0d9b767c692f5b823e3 Mon Sep 17 00:00:00 2001 From: Lorenz Schori Date: Mon, 21 Sep 2015 10:43:56 +0200 Subject: [PATCH 1/8] Add test coverage for Twig_Cache_Filesystem --- test/Twig/Tests/Cache/FilesystemTest.php | 170 +++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 test/Twig/Tests/Cache/FilesystemTest.php diff --git a/test/Twig/Tests/Cache/FilesystemTest.php b/test/Twig/Tests/Cache/FilesystemTest.php new file mode 100644 index 000000000..35a6e5422 --- /dev/null +++ b/test/Twig/Tests/Cache/FilesystemTest.php @@ -0,0 +1,170 @@ +nonce = hash('sha256', uniqid(mt_rand(), true)); + $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$this->nonce; + $this->directory = sys_get_temp_dir().'/twig-test-'.$this->nonce; + $this->cache = new Twig_Cache_Filesystem($this->directory); + } + + public function tearDown() + { + if (file_exists($this->directory)) { + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::CHILD_FIRST); + foreach ($iterator as $filename => $fileInfo) { + if (!$iterator->isDot()) { + if ($fileInfo->isDir()) { + rmdir($filename); + } else { + unlink($filename); + } + } + } + rmdir($this->directory); + } + } + + public function testLoad() + { + $key = $this->directory.'/cache/cachefile.php'; + + $dir = dirname($key); + @mkdir($dir, 0777, true); + $this->assertTrue(is_dir($dir)); + + $this->assertFalse(class_exists($this->classname, false)); + + $content = $this->generateSource(); + file_put_contents($key, $content); + + $this->cache->load($key); + + $this->assertTrue(class_exists($this->classname, false)); + } + + public function testLoadMissing() + { + $key = $this->directory.'/cache/cachefile.php'; + + $this->assertFalse(class_exists($this->classname, false)); + + $this->cache->load($key); + + $this->assertFalse(class_exists($this->classname, false)); + } + + public function testWrite() + { + $key = $this->directory.'/cache/cachefile.php'; + $content = $this->generateSource(); + + $this->assertFalse(file_exists($key)); + $this->assertFalse(file_exists($this->directory)); + + $this->cache->write($key, $content); + + $this->assertTrue(file_exists($this->directory)); + $this->assertTrue(file_exists($key)); + $this->assertSame(file_get_contents($key), $content); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessageRegExp #^Unable to create the cache directory # + */ + public function testWriteFailMkdir() + { + $key = $this->directory.'/cache/cachefile.php'; + $content = $this->generateSource(); + + $this->assertFalse(file_exists($key)); + + // Create read-only root directory. + @mkdir($this->directory, 0555, true); + $this->assertTrue(is_dir($this->directory)); + + $this->cache->write($key, $content); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessageRegExp #^Unable to write in the cache directory # + */ + public function testWriteFailDirWritable() + { + $key = $this->directory.'/cache/cachefile.php'; + $content = $this->generateSource(); + + $this->assertFalse(file_exists($key)); + + // Create root directory. + @mkdir($this->directory, 0777, true); + // Create read-only subdirectory. + @mkdir($this->directory.'/cache' , 0555); + $this->assertTrue(is_dir($this->directory.'/cache')); + + $this->cache->write($key, $content); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessageRegExp #^Failed to write cache file # + */ + public function testWriteFailWriteFile() + { + $key = $this->directory.'/cache/cachefile.php'; + $content = $this->generateSource(); + + $this->assertFalse(file_exists($key)); + + // Create a directory in the place of the cache file. + @mkdir($key, 0777, true); + $this->assertTrue(is_dir($key)); + + $this->cache->write($key, $content); + } + + public function testGetTimestamp() + { + $key = $this->directory.'/cache/cachefile.php'; + + $dir = dirname($key); + @mkdir($dir, 0777, true); + $this->assertTrue(is_dir($dir)); + + // Create the file with a specific modification time. + touch($key, 1234567890); + + $this->assertSame(1234567890, $this->cache->getTimestamp($key)); + } + + public function testGetTimestampMissingFile() + { + $key = $this->directory.'/cache/cachefile.php'; + $this->assertSame(0, $this->cache->getTimestamp($key)); + } + + private function generateSource() + { + return strtr(' $this->classname, + )); + } +} From 76ecd43338223ff2e6394a943d6e0a67596fe5a3 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 21 Sep 2015 09:20:31 +0200 Subject: [PATCH 2/8] changed template cache names to take into account the Twig C extension --- CHANGELOG | 2 +- lib/Twig/Environment.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c7fc76487..ec589ec10 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.22.3 (2015-XX-XX) - * n/a + * changed template cache names to take into account the Twig C extension * 1.22.2 (2015-09-22) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 00aa8789a..2b3676df9 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -291,6 +291,12 @@ class Twig_Environment /** * Gets the template class associated with the given string. * + * The generated template class is based on the following parameters: + * + * * The cache key for the given template; + * * The currently enabled extensions; + * * Whether the Twig C extension is available or not. + * * @param string $name The name for which to calculate the template class name * @param int $index The index if it is an embedded template * @@ -300,6 +306,10 @@ class Twig_Environment { $key = $this->getLoader()->getCacheKey($name).'__'.implode('__', array_keys($this->extensions)); + if (function_exists('twig_template_get_attributes')) { + $key .= '__cext'; + } + return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index); } From 831fd601cdc6ee1474f7c61879363ddd272e34be Mon Sep 17 00:00:00 2001 From: Possum Date: Tue, 29 Sep 2015 17:58:36 +0200 Subject: [PATCH 3/8] Remove deprecated return types from the PHPDocs. --- lib/Twig/Extension.php | 8 ++++---- lib/Twig/ExtensionInterface.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index 5c8ad5c96..fb657806f 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -24,7 +24,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return Twig_TokenParserInterface[] */ public function getTokenParsers() { @@ -44,7 +44,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * Returns a list of filters to add to the existing list. * - * @return array An array of filters + * @return Twig_SimpleFilter[] */ public function getFilters() { @@ -54,7 +54,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * Returns a list of tests to add to the existing list. * - * @return array An array of tests + * @return Twig_SimpleTest[] */ public function getTests() { @@ -64,7 +64,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * Returns a list of functions to add to the existing list. * - * @return array An array of functions + * @return Twig_SimpleFunction[] */ public function getFunctions() { diff --git a/lib/Twig/ExtensionInterface.php b/lib/Twig/ExtensionInterface.php index 49541b02e..92abf7db2 100644 --- a/lib/Twig/ExtensionInterface.php +++ b/lib/Twig/ExtensionInterface.php @@ -28,7 +28,7 @@ interface Twig_ExtensionInterface /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return Twig_TokenParserInterface[] */ public function getTokenParsers(); @@ -42,21 +42,21 @@ interface Twig_ExtensionInterface /** * Returns a list of filters to add to the existing list. * - * @return array An array of filters + * @return Twig_SimpleFilter[] */ public function getFilters(); /** * Returns a list of tests to add to the existing list. * - * @return array An array of tests + * @return Twig_SimpleTest[] */ public function getTests(); /** * Returns a list of functions to add to the existing list. * - * @return array An array of functions + * @return Twig_SimpleFunction[] */ public function getFunctions(); From 008fc134e3670c2e7c2393c9c0a670bdcee343e4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 30 Sep 2015 14:04:32 +0200 Subject: [PATCH 4/8] remove duplicated phpdocs --- lib/Twig/Extension.php | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index fb657806f..7a3c85985 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -11,20 +11,14 @@ abstract class Twig_Extension implements Twig_ExtensionInterface { /** - * Initializes the runtime environment. - * - * This is where you can load some file that contains filter functions for instance. - * - * @param Twig_Environment $environment The current Twig_Environment instance + * {@inheritdoc} */ public function initRuntime(Twig_Environment $environment) { } /** - * Returns the token parser instances to add to the existing list. - * - * @return Twig_TokenParserInterface[] + * {@inheritdoc} */ public function getTokenParsers() { @@ -32,9 +26,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns the node visitor instances to add to the existing list. - * - * @return Twig_NodeVisitorInterface[] An array of Twig_NodeVisitorInterface instances + * {@inheritdoc} */ public function getNodeVisitors() { @@ -42,9 +34,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns a list of filters to add to the existing list. - * - * @return Twig_SimpleFilter[] + * {@inheritdoc} */ public function getFilters() { @@ -52,9 +42,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns a list of tests to add to the existing list. - * - * @return Twig_SimpleTest[] + * {@inheritdoc} */ public function getTests() { @@ -62,9 +50,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns a list of functions to add to the existing list. - * - * @return Twig_SimpleFunction[] + * {@inheritdoc} */ public function getFunctions() { @@ -72,9 +58,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns a list of operators to add to the existing list. - * - * @return array An array of operators + * {@inheritdoc} */ public function getOperators() { @@ -82,9 +66,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface } /** - * Returns a list of global variables to add to the existing list. - * - * @return array An array of global variables + * {@inheritdoc} */ public function getGlobals() { From 6f84981ae6c1399e5b1b3d18158b9e24fffe1da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 28 Sep 2015 11:31:24 +0200 Subject: [PATCH 5/8] Fix custom escaper null values --- CHANGELOG | 4 ++++ ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 12 +++++++++++- lib/Twig/Extension/Core.php | 2 +- test/Twig/Tests/Extension/CoreTest.php | 2 ++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 90905c0d1..ec589ec10 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +* 1.22.3 (2015-XX-XX) + + * changed template cache names to take into account the Twig C extension + * 1.22.2 (2015-09-22) * fixed a race condition in template loading diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index 359cd43b2..05157599b 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.22.2" +#define PHP_TWIG_VERSION "1.22.3-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 0c32550cc..2b3676df9 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,7 +16,7 @@ */ class Twig_Environment { - const VERSION = '1.22.2'; + const VERSION = '1.22.3-DEV'; protected $charset; protected $loader; @@ -291,6 +291,12 @@ class Twig_Environment /** * Gets the template class associated with the given string. * + * The generated template class is based on the following parameters: + * + * * The cache key for the given template; + * * The currently enabled extensions; + * * Whether the Twig C extension is available or not. + * * @param string $name The name for which to calculate the template class name * @param int $index The index if it is an embedded template * @@ -300,6 +306,10 @@ class Twig_Environment { $key = $this->getLoader()->getCacheKey($name).'__'.implode('__', array_keys($this->extensions)); + if (function_exists('twig_template_get_attributes')) { + $key .= '__cext'; + } + return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index); } diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f9ec2a475..45e0030dd 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1021,7 +1021,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html', if (!is_string($string)) { if (is_object($string) && method_exists($string, '__toString')) { $string = (string) $string; - } else { + } elseif (in_array($strategy, array('html', 'js', 'css', 'html_attr', 'url'))) { return $string; } } diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index a4692c267..cd49e2f5e 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -121,6 +121,8 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase $twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test'); $this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo')); + $this->assertEquals('UTF-8', twig_escape_filter($twig, null, 'foo')); + $this->assertEquals('42UTF-8', twig_escape_filter($twig, 42, 'foo')); } /** From f379e8141c8820143c2da91ff5279b641d563f8c Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 24 Sep 2015 20:43:44 +0200 Subject: [PATCH 6/8] fix template class name generation to prevent possible collisions and one inode per file --- lib/Twig/Cache/Filesystem.php | 2 +- lib/Twig/Environment.php | 12 +++++------- lib/Twig/Test/NodeTestCase.php | 2 +- test/Twig/Tests/Node/ModuleTest.php | 6 +++--- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/Twig/Cache/Filesystem.php b/lib/Twig/Cache/Filesystem.php index 2e1c0a069..463104463 100644 --- a/lib/Twig/Cache/Filesystem.php +++ b/lib/Twig/Cache/Filesystem.php @@ -38,7 +38,7 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface { $hash = hash('sha256', $className); - return $this->directory.'/'.$hash[0].'/'.$hash[1].'/'.$hash.'.php'; + return $this->directory.'/'.$hash[0].$hash[1].'/'.$hash.'.php'; } /** diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 2b3676df9..49816ed9d 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -297,18 +297,16 @@ class Twig_Environment * * The currently enabled extensions; * * Whether the Twig C extension is available or not. * - * @param string $name The name for which to calculate the template class name - * @param int $index The index if it is an embedded template + * @param string $name The name for which to calculate the template class name + * @param int|null $index The index if it is an embedded template * * @return string The template class name */ public function getTemplateClass($name, $index = null) { - $key = $this->getLoader()->getCacheKey($name).'__'.implode('__', array_keys($this->extensions)); - - if (function_exists('twig_template_get_attributes')) { - $key .= '__cext'; - } + $key = $this->getLoader()->getCacheKey($name); + $key .= json_encode(array_keys($this->extensions)); + $key .= function_exists('twig_template_get_attributes'); return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index); } diff --git a/lib/Twig/Test/NodeTestCase.php b/lib/Twig/Test/NodeTestCase.php index 908ef6196..6fe1b5ff9 100644 --- a/lib/Twig/Test/NodeTestCase.php +++ b/lib/Twig/Test/NodeTestCase.php @@ -25,7 +25,7 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase $compiler = $this->getCompiler($environment); $compiler->compile($node); - $this->assertEquals($source, trim($compiler->getSource())); + $this->assertStringMatchesFormat($source, trim($compiler->getSource())); } protected function getCompiler(Twig_Environment $environment = null) diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 19a4be9ff..ca784e51e 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -46,7 +46,7 @@ class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase Date: Mon, 21 Sep 2015 12:33:12 +0200 Subject: [PATCH 7/8] Add test coverage for auto_reload --- test/Twig/Tests/EnvironmentTest.php | 97 +++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 83a98ef52..87ff9dcaa 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -178,6 +178,88 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase 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')); @@ -212,6 +294,21 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $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 From efaf5036e6857616379b78648ffdb5fdafb92c87 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 4 Oct 2015 10:38:04 +0200 Subject: [PATCH 8/8] fixed CS --- test/Twig/Tests/Cache/FilesystemTest.php | 13 ++++--- test/Twig/Tests/EnvironmentTest.php | 43 +++++++++++------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/test/Twig/Tests/Cache/FilesystemTest.php b/test/Twig/Tests/Cache/FilesystemTest.php index 35a6e5422..20e83efd5 100644 --- a/test/Twig/Tests/Cache/FilesystemTest.php +++ b/test/Twig/Tests/Cache/FilesystemTest.php @@ -11,12 +11,12 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase { - protected $nonce; - protected $classname; - protected $directory; - protected $cache; + private $nonce; + private $classname; + private $directory; + private $cache; - public function setUp() + protected function setUp() { $this->nonce = hash('sha256', uniqid(mt_rand(), true)); $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$this->nonce; @@ -24,7 +24,7 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase $this->cache = new Twig_Cache_Filesystem($this->directory); } - public function tearDown() + protected function tearDown() { if (file_exists($this->directory)) { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::CHILD_FIRST); @@ -48,7 +48,6 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase $dir = dirname($key); @mkdir($dir, 0777, true); $this->assertTrue(is_dir($dir)); - $this->assertFalse(class_exists($this->classname, false)); $content = $this->generateSource(); diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 87ff9dcaa..57b91ae50 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -180,13 +180,12 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase public function testAutoReloadCacheMiss() { - $template_name = __FUNCTION__; - $template_content = __FUNCTION__; + $templateName = __FUNCTION__; + $templateContent = __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); + $loader = $this->getMockLoader($templateName, $templateContent); + $twig = new Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => true, 'debug' => false)); // Cache miss: getTimestamp returns 0 and as a result the load() is // skipped. @@ -201,18 +200,17 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $cache->expects($this->never()) ->method('load'); - $twig->loadTemplate($template_name); + $twig->loadTemplate($templateName); } public function testAutoReloadCacheHit() { - $template_name = __FUNCTION__; - $template_content = __FUNCTION__; + $templateName = __FUNCTION__; + $templateContent = __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); + $loader = $this->getMockLoader($templateName, $templateContent); + $twig = new Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => true, 'debug' => false)); $now = time(); @@ -230,18 +228,17 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $cache->expects($this->once()) ->method('load'); - $twig->loadTemplate($template_name); + $twig->loadTemplate($templateName); } public function testAutoReloadOutdatedCacheHit() { - $template_name = __FUNCTION__; - $template_content = __FUNCTION__; + $templateName = __FUNCTION__; + $templateContent = __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); + $loader = $this->getMockLoader($templateName, $templateContent); + $twig = new Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => true, 'debug' => false)); $now = time(); @@ -257,7 +254,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $cache->expects($this->never()) ->method('load'); - $twig->loadTemplate($template_name); + $twig->loadTemplate($templateName); } public function testAddExtension() @@ -295,17 +292,17 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertCount(2, $twig->getNodeVisitors()); } - protected function getMockLoader($template_name, $template_content) + protected function getMockLoader($templateName, $templateContent) { $loader = $this->getMock('Twig_LoaderInterface'); $loader->expects($this->any()) ->method('getSource') - ->with($template_name) - ->will($this->returnValue($template_content)); + ->with($templateName) + ->will($this->returnValue($templateContent)); $loader->expects($this->any()) ->method('getCacheKey') - ->with($template_name) - ->will($this->returnValue($template_name)); + ->with($templateName) + ->will($this->returnValue($templateName)); return $loader; }