mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-11 18:06:46 +00:00
fixed random function when charset is null
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user