mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 02:16:41 +00:00
added tests for random function
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user