1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider getRandomFunctionTestData
*/
public function testRandomFunction($value, $expectedInArray)
{
$env = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
for ($i = 0; $i < 100; ++$i) {
$this->assertTrue(in_array(twig_random($env, $value), $expectedInArray, true)); // assertContains() would not consider the type
}
}
public function getRandomFunctionTestData()
{
return array(
array(// array
array('apple', 'orange', 'citrus'),
array('apple', 'orange', 'citrus'),
),
array(// Traversable
new ArrayObject(array('apple', 'orange', 'citrus')),
array('apple', 'orange', 'citrus'),
),
array(// unicode string
'Ä€é',
array('Ä', '€', 'é'),
),
array(// numeric but string
'123',
array('1', '2', '3'),
),
array(// integer
5,
range(0, 5, 1),
),
array(// float
5.9,
range(0, 5, 1),
),
array(// negative
-2,
array(0, -1, -2),
),
);
}
public function testRandomFunctionWithoutParameter()
{
$max = mt_getrandmax();
for ($i = 0; $i < 100; ++$i) {
$val = twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')));
$this->assertTrue(is_int($val) && $val >= 0 && $val <= $max);
}
}
public function testRandomFunctionReturnsAsIs()
{
$this->assertSame('', twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), ''));
$this->assertSame('', twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('charset' => null)), ''));
$instance = new stdClass();
$this->assertSame($instance, twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $instance));
}
/**
* @expectedException Twig_Error_Runtime
*/
public function testRandomFunctionOfEmptyArrayThrowsException()
{
twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), array());
}
public function testRandomFunctionOnNonUTF8String()
{
if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
$this->markTestSkipped('needs iconv or mbstring');
}
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->setCharset('ISO-8859-1');
$text = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
for ($i = 0; $i < 30; ++$i) {
$rand = twig_random($twig, $text);
$this->assertTrue(in_array(twig_convert_encoding($rand, 'UTF-8', 'ISO-8859-1'), array('Ä', 'é'), true));
}
}
public function testReverseFilterOnNonUTF8String()
{
if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
$this->markTestSkipped('needs iconv or mbstring');
}
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->setCharset('ISO-8859-1');
$input = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8');
$output = twig_convert_encoding(twig_reverse_filter($twig, $input), 'UTF-8', 'ISO-8859-1');
$this->assertEquals($output, 'éÄ');
}
public function testCustomEscaper()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test');
$this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo'));
$this->assertEquals('UTF-8', twig_escape_filter($twig, null, 'foo'));
$this->assertEquals('42UTF-8', twig_escape_filter($twig, 42, 'foo'));
}
/**
* @expectedException Twig_Error_Runtime
*/
public function testUnknownCustomEscaper()
{
twig_escape_filter(new Twig_Environment($this->getMock('Twig_LoaderInterface')), 'foo', 'bar');
}
public function testTwigFirst()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$this->assertEquals('a', twig_first($twig, 'abc'));
$this->assertEquals(1, twig_first($twig, array(1, 2, 3)));
$this->assertSame('', twig_first($twig, null));
$this->assertSame('', twig_first($twig, ''));
}
public function testTwigLast()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$this->assertEquals('c', twig_last($twig, 'abc'));
$this->assertEquals(3, twig_last($twig, array(1, 2, 3)));
$this->assertSame('', twig_last($twig, null));
$this->assertSame('', twig_last($twig, ''));
}
}
function foo_escaper_for_test(Twig_Environment $env, $string, $charset)
{
return $string.$charset;
}
|