From: Tobias Schultze Date: Thu, 26 Jan 2012 11:19:51 +0000 (+0100) Subject: added tests for random function X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=fe663585b7429a276f86873f374a5aa3b7f401c2;p=web%2Fkonrad%2Ftwig.git added tests for random function --- diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index e35a1e4..15d6d19 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -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), + 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), + ), ); - foreach ($values as $value) { - for ($i = 0; $i < 100; $i++) { - $this->assertTrue(in_array(twig_random($value), $items)); - } + } + + 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()); + } }