From fe663585b7429a276f86873f374a5aa3b7f401c2 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 26 Jan 2012 12:19:51 +0100 Subject: [PATCH] added tests for random function --- test/Twig/Tests/Extension/CoreTest.php | 62 +++++++++++++++++++++++++++----- 1 files changed, 53 insertions(+), 9 deletions(-) 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()); + } } -- 1.7.2.5