fixed random function when charset is null

This commit is contained in:
Tobias Schultze
2012-04-09 09:26:53 +02:00
parent 46ae99fa4c
commit 27064127ad
3 changed files with 7 additions and 1 deletions
+5
View 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);
+1 -1
View 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()
+1
View 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));