added tests for random function

This commit is contained in:
Tobias Schultze
2012-01-26 12:19:51 +01:00
parent 6f5ceeec10
commit fe663585b7
+54 -10
View File
@@ -11,17 +11,61 @@
class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase
{
public function testRandomFunction()
/**
* @dataProvider getRandomFunctionTestData
*/
public function testRandomFunction($value, $expectedInArray)
{
$items = array('apple', 'orange', 'citrus');
$values = array(
$items,
new ArrayObject($items),
);
foreach ($values as $value) {
for ($i = 0; $i < 100; $i++) {
$this->assertTrue(in_array(twig_random($value), $items));
}
for ($i = 0; $i < 100; $i++) {
$this->assertTrue(in_array(twig_random($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),
),
);
}
public function testRandomFunctionWithoutParameter()
{
$max = mt_getrandmax();
for ($i = 0; $i < 100; $i++) {
$val = twig_random();
$this->assertTrue(is_int($val) && $val >= 0 && $val <= $max);
}
}
/**
* @expectedException Twig_Error_Runtime
*/
public function testRandomFunctionOfEmptyArrayThrowsException()
{
twig_random(array());
}
}