From 79dafd5af48495566fe9eac8ca03826ce8c3952a Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 26 Feb 2017 12:47:54 +0100 Subject: [PATCH] turning Filter::formatHumanReadableTime() test case into property based one, clarifying the allowed units --- lib/Filter.php | 4 +++- tst/FilterTest.php | 44 ++++++++++++++++++++++++++++++++++++---- tst/Vizhash16x16Test.php | 12 ++++------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/Filter.php b/lib/Filter.php index 60f6f170..d0f1c338 100644 --- a/lib/Filter.php +++ b/lib/Filter.php @@ -39,7 +39,9 @@ class Filter /** * format a given time string into a human readable label (localized) * - * accepts times in the format "[integer][time unit]" + * accepts times in the format "[integer][time unit]", valid time units are: + * sec, second, seconds, min, minute, minutes, hour, hours, day, days, week, + * weeks, month, months, year, years * * @access public * @static diff --git a/tst/FilterTest.php b/tst/FilterTest.php index 63cc8f89..1b40998b 100644 --- a/tst/FilterTest.php +++ b/tst/FilterTest.php @@ -1,9 +1,12 @@ assertEquals( @@ -14,10 +17,43 @@ class FilterTest extends PHPUnit_Framework_TestCase public function testFilterMakesTimesHumanlyReadable() { - $this->assertEquals('5 minutes', Filter::formatHumanReadableTime('5min')); - $this->assertEquals('90 seconds', Filter::formatHumanReadableTime('90sec')); - $this->assertEquals('1 week', Filter::formatHumanReadableTime('1week')); - $this->assertEquals('6 months', Filter::formatHumanReadableTime('6months')); + $this->forAll( + Generator\nat(), + Generator\oneOf( + 'sec', 'second', 'seconds' + ) + )->then( + function ($int, $unit) + { + $suffix = $int === 1 ? '' : 's'; + $this->assertEquals($int . ' second' . $suffix, Filter::formatHumanReadableTime($int . $unit)); + } + ); + $this->forAll( + Generator\nat(), + Generator\oneOf( + 'min', 'minute', 'minutes' + ) + )->then( + function ($int, $unit) + { + $suffix = $int === 1 ? '' : 's'; + $this->assertEquals($int . ' minute' . $suffix, Filter::formatHumanReadableTime($int . $unit)); + } + ); + $this->forAll( + Generator\nat(), + Generator\oneOf( + 'hour', 'hours', 'day', 'days', 'week', 'weeks', + 'month', 'months', 'year', 'years' + ) + )->then( + function ($int, $unit) + { + $suffix = $int === 1 ? '' : 's'; + $this->assertEquals($int . ' ' . rtrim($unit, 's') . $suffix, Filter::formatHumanReadableTime($int . $unit)); + } + ); } /** diff --git a/tst/Vizhash16x16Test.php b/tst/Vizhash16x16Test.php index 6a6c8a71..a8db0748 100644 --- a/tst/Vizhash16x16Test.php +++ b/tst/Vizhash16x16Test.php @@ -41,16 +41,14 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase $vz = new Vizhash16x16(); $pngdata = $vz->generate($string1); - if (empty($string1)) - { + if (empty($string1)) { $this->assertEquals($pngdata, ''); } else { $this->assertNotEquals($pngdata, ''); file_put_contents($this->_file, $pngdata); $finfo = new finfo(FILEINFO_MIME_TYPE); $this->assertEquals('image/png', $finfo->file($this->_file)); - if ($string1 !== $string2) - { + if ($string1 !== $string2) { $this->assertNotEquals($pngdata, $string2); } } @@ -73,8 +71,7 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase file_put_contents($this->_file, $pngdata); $finfo = new finfo(FILEINFO_MIME_TYPE); $this->assertEquals('image/png', $finfo->file($this->_file)); - if ($hash1 !== $hash2) - { + if ($hash1 !== $hash2) { $this->assertNotEquals($pngdata, $vz->generate($hash2)); } $this->assertEquals($pngdata, $vz->generate($hash1)); @@ -96,8 +93,7 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase file_put_contents($this->_file, $pngdata); $finfo = new finfo(FILEINFO_MIME_TYPE); $this->assertEquals('image/png', $finfo->file($this->_file)); - if ($hash1 !== $hash2) - { + if ($hash1 !== $hash2) { $this->assertNotEquals($pngdata, $vz->generate($hash2)); } $this->assertEquals($pngdata, $vz->generate($hash1));