From: Tobias Schultze Date: Mon, 9 Apr 2012 07:26:53 +0000 (+0200) Subject: fixed random function when charset is null X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=27064127ad2f31af4ee6f91785e5f1c45d02e16b;p=konrad%2Ftwig.git fixed random function when charset is null --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 78787d8..e241504 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -313,6 +313,8 @@ function twig_cycle($values, $i) * @param Twig_Environment $env A Twig_Environment instance * @param Traversable|array|int|string $values The values to pick a random item from * + * @throws Twig_Error_Runtime When $values is an empty array (does not apply to an empty string which is returned as is). + * * @return mixed A random value from the given sequence */ function twig_random(Twig_Environment $env, $values = null) @@ -328,6 +330,9 @@ function twig_random(Twig_Environment $env, $values = null) if ($values instanceof Traversable) { $values = iterator_to_array($values); } elseif (is_string($values)) { + if ('' === $values) { + return ''; + } if (null !== $charset = $env->getCharset()) { if ('UTF-8' != $charset) { $values = twig_convert_encoding($values, 'UTF-8', $charset); diff --git a/test/Twig/Tests/ErrorTest.php b/test/Twig/Tests/ErrorTest.php index 45d4053..978ee80 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -16,7 +16,7 @@ class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase $error = new Twig_Error('foo'); $error->setTemplateFile(new SplFileInfo(__FILE__)); - $this->assertContains('test/Twig/Tests/ErrorTest.php', $error->getMessage()); + $this->assertContains('test'.DIRECTORY_SEPARATOR.'Twig'.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage()); } public function testErrorWithArrayFilename() diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index 89a7750..77a8101 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -68,6 +68,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase public function testRandomFunctionReturnsAsIs() { $this->assertSame('', twig_random(new Twig_Environment(), '')); + $this->assertSame('', twig_random(new Twig_Environment(null, array('charset' => null)), '')); $instance = new stdClass(); $this->assertSame($instance, twig_random(new Twig_Environment(), $instance));