4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_Extension_CoreTest
extends PHPUnit_Framework_TestCase
15 * @dataProvider getRandomFunctionTestData
17 public function testRandomFunction($value, $expectedInArray)
19 $env = new Twig_Environment();
21 for ($i = 0; $i < 100; $i++
) {
22 $this->assertTrue(in_array(twig_random($env, $value), $expectedInArray, true)); // assertContains() would not consider the type
26 public function getRandomFunctionTestData()
30 array('apple', 'orange', 'citrus'),
31 array('apple', 'orange', 'citrus'),
34 new ArrayObject(array('apple', 'orange', 'citrus')),
35 array('apple', 'orange', 'citrus'),
37 array( // unicode string
41 array( // numeric but string
60 public function testRandomFunctionWithoutParameter()
62 $max = mt_getrandmax();
64 for ($i = 0; $i < 100; $i++
) {
65 $val = twig_random(new Twig_Environment());
66 $this->assertTrue(is_int($val) && $val >= 0 && $val <= $max);
70 public function testRandomFunctionReturnsAsIs()
72 $this->assertSame('', twig_random(new Twig_Environment(), ''));
73 $this->assertSame('', twig_random(new Twig_Environment(null, array('charset' => null)), ''));
75 $instance = new stdClass();
76 $this->assertSame($instance, twig_random(new Twig_Environment(), $instance));
80 * @expectedException Twig_Error_Runtime
82 public function testRandomFunctionOfEmptyArrayThrowsException()
84 twig_random(new Twig_Environment(), array());
87 public function testRandomFunctionOnNonUTF8String()
89 if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
90 $this->markTestSkipped('needs iconv or mbstring');
93 $twig = new Twig_Environment();
94 $twig->setCharset('ISO-8859-1');
96 $text = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
97 for ($i = 0; $i < 30; $i++
) {
98 $rand = twig_random($twig, $text);
99 $this->assertTrue(in_array(twig_convert_encoding($rand, 'UTF-8', 'ISO-8859-1'), array('Ä', 'é'), true));
103 public function testReverseFilterOnNonUTF8String()
105 if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
106 $this->markTestSkipped('needs iconv or mbstring');
109 $twig = new Twig_Environment();
110 $twig->setCharset('ISO-8859-1');
112 $input = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
113 $output = twig_convert_encoding(twig_reverse_filter($twig, $input), 'UTF-8', 'ISO-8859-1');
115 $this->assertEquals($output, 'éÄ');