]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of Twig. | |
5 | * | |
6 | * (c) Fabien Potencier | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase | |
13 | { | |
14 | /** | |
15 | * @dataProvider getRandomFunctionTestData | |
16 | */ | |
17 | public function testRandomFunction($value, $expectedInArray) | |
18 | { | |
19 | $env = new Twig_Environment(); | |
20 | ||
21 | for ($i = 0; $i < 100; $i++) { | |
22 | $this->assertTrue(in_array(twig_random($env, $value), $expectedInArray, true)); // assertContains() would not consider the type | |
23 | } | |
24 | } | |
25 | ||
26 | public function getRandomFunctionTestData() | |
27 | { | |
28 | return array( | |
29 | array( // array | |
30 | array('apple', 'orange', 'citrus'), | |
31 | array('apple', 'orange', 'citrus'), | |
32 | ), | |
33 | array( // Traversable | |
34 | new ArrayObject(array('apple', 'orange', 'citrus')), | |
35 | array('apple', 'orange', 'citrus'), | |
36 | ), | |
37 | array( // unicode string | |
38 | 'Ä€é', | |
39 | array('Ä', '€', 'é'), | |
40 | ), | |
41 | array( // numeric but string | |
42 | '123', | |
43 | array('1', '2', '3'), | |
44 | ), | |
45 | array( // integer | |
46 | 5, | |
47 | range(0, 5, 1), | |
48 | ), | |
49 | array( // float | |
50 | 5.9, | |
51 | range(0, 5, 1), | |
52 | ), | |
53 | array( // negative | |
54 | -2, | |
55 | array(0, -1, -2), | |
56 | ), | |
57 | ); | |
58 | } | |
59 | ||
60 | public function testRandomFunctionWithoutParameter() | |
61 | { | |
62 | $max = mt_getrandmax(); | |
63 | ||
64 | for ($i = 0; $i < 100; $i++) { | |
65 | $val = twig_random(new Twig_Environment()); | |
66 | $this->assertTrue(is_int($val) && $val >= 0 && $val <= $max); | |
67 | } | |
68 | } | |
69 | ||
70 | public function testRandomFunctionReturnsAsIs() | |
71 | { | |
72 | $this->assertSame('', twig_random(new Twig_Environment(), '')); | |
73 | $this->assertSame('', twig_random(new Twig_Environment(null, array('charset' => null)), '')); | |
74 | ||
75 | $instance = new stdClass(); | |
76 | $this->assertSame($instance, twig_random(new Twig_Environment(), $instance)); | |
77 | } | |
78 | ||
79 | /** | |
80 | * @expectedException Twig_Error_Runtime | |
81 | */ | |
82 | public function testRandomFunctionOfEmptyArrayThrowsException() | |
83 | { | |
84 | twig_random(new Twig_Environment(), array()); | |
85 | } | |
86 | ||
87 | public function testRandomFunctionOnNonUTF8String() | |
88 | { | |
89 | if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) { | |
90 | $this->markTestSkipped('needs iconv or mbstring'); | |
91 | } | |
92 | ||
93 | $twig = new Twig_Environment(); | |
94 | $twig->setCharset('ISO-8859-1'); | |
95 | ||
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)); | |
100 | } | |
101 | } | |
102 | ||
103 | public function testReverseFilterOnNonUTF8String() | |
104 | { | |
105 | if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) { | |
106 | $this->markTestSkipped('needs iconv or mbstring'); | |
107 | } | |
108 | ||
109 | $twig = new Twig_Environment(); | |
110 | $twig->setCharset('ISO-8859-1'); | |
111 | ||
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'); | |
114 | ||
115 | $this->assertEquals($output, 'éÄ'); | |
116 | } | |
117 | } |