fixed random function when charset is null
authorTobias Schultze <webmaster@tubo-world.de>
Mon, 9 Apr 2012 07:26:53 +0000 (09:26 +0200)
committerTobias Schultze <webmaster@tubo-world.de>
Mon, 9 Apr 2012 07:26:53 +0000 (09:26 +0200)
lib/Twig/Extension/Core.php
test/Twig/Tests/ErrorTest.php
test/Twig/Tests/Extension/CoreTest.php

index 78787d8..e241504 100644 (file)
@@ -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);
index 45d4053..978ee80 100644 (file)
@@ -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()
index 89a7750..77a8101 100644 (file)
@@ -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));