* @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)
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);
$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()
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));