From b15d84c8b9c81754eb1ea3dba39e42c909db4edc Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Mon, 4 Apr 2016 02:13:10 -0500 Subject: [PATCH 1/2] [Console] Correct time formatting. The previous behavior caused dramatic jumps in the reported time instead of smoothly transitioning between time ranges. Added tests around the new behavior and the transitions between seconds, minutes, and days. --- .../Component/Console/Helper/Helper.php | 32 +++++------ .../Console/Tests/Helper/HelperTest.php | 53 +++++++++++++++++++ 2 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 src/Symfony/Component/Console/Tests/Helper/HelperTest.php diff --git a/src/Symfony/Component/Console/Helper/Helper.php b/src/Symfony/Component/Console/Helper/Helper.php index 156d8dc644cf9..444bbffc768e3 100644 --- a/src/Symfony/Component/Console/Helper/Helper.php +++ b/src/Symfony/Component/Console/Helper/Helper.php @@ -66,26 +66,28 @@ public static function formatTime($secs) { static $timeFormats = array( array(0, '< 1 sec'), - array(2, '1 sec'), - array(59, 'secs', 1), + array(1, '1 sec'), + array(2, 'secs', 1), array(60, '1 min'), - array(3600, 'mins', 60), - array(5400, '1 hr'), - array(86400, 'hrs', 3600), - array(129600, '1 day'), - array(604800, 'days', 86400), + array(120, 'mins', 60), + array(3600, '1 hr'), + array(7200, 'hrs', 3600), + array(86400, '1 day'), + array(172800, 'days', 86400), ); - foreach ($timeFormats as $format) { + foreach ($timeFormats as $index => $format) { if ($secs >= $format[0]) { - continue; + if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0]) + || $index == count($timeFormats) - 1 + ) { + if (2 == count($format)) { + return $format[1]; + } + + return floor($secs / $format[2]).' '.$format[1]; + } } - - if (2 == count($format)) { - return $format[1]; - } - - return ceil($secs / $format[2]).' '.$format[1]; } } diff --git a/src/Symfony/Component/Console/Tests/Helper/HelperTest.php b/src/Symfony/Component/Console/Tests/Helper/HelperTest.php new file mode 100644 index 0000000000000..a1fc763211a5f --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Helper/HelperTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Helper; + +use Symfony\Component\Console\Helper\Helper; + +class HelperTest extends \PHPUnit_Framework_TestCase +{ + public function formatTimeProvider() + { + return array( + array(0, '< 1 sec'), + array(1, '1 sec'), + array(2, '2 secs'), + array(59, '59 secs'), + array(60, '1 min'), + array(61, '1 min'), + array(119, '1 min'), + array(120, '2 mins'), + array(121, '2 mins'), + array(3599, '59 mins'), + array(3600, '1 hr'), + array(7199, '1 hr'), + array(7200, '2 hrs'), + array(7201, '2 hrs'), + array(86399, '23 hrs'), + array(86400, '1 day'), + array(86401, '1 day'), + array(172799, '1 day'), + array(172800, '2 days'), + array(172801, '2 days'), + ); + } + + /** + * @dataProvider formatTimeProvider + * @param int $secs + * @param string $expectedFormat + */ + public function testFormatTime($secs, $expectedFormat) + { + $this->assertEquals($expectedFormat, Helper::formatTime($secs)); + } +} From 510789c465c935c49d17174d99c80395c14188d1 Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Mon, 4 Apr 2016 02:55:00 -0500 Subject: [PATCH 2/2] [Console] Fix code standard issue and test. Fix test that set < 1 sec to 1 sec due to time formatting. --- src/Symfony/Component/Console/Tests/Helper/HelperTest.php | 3 ++- .../Component/Console/Tests/Helper/ProgressBarTest.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/HelperTest.php b/src/Symfony/Component/Console/Tests/Helper/HelperTest.php index a1fc763211a5f..33fa22051a05d 100644 --- a/src/Symfony/Component/Console/Tests/Helper/HelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/HelperTest.php @@ -43,7 +43,8 @@ public function formatTimeProvider() /** * @dataProvider formatTimeProvider - * @param int $secs + * + * @param int $secs * @param string $expectedFormat */ public function testFormatTime($secs, $expectedFormat) diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index ffe5bed086348..c6bbe81c98a57 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -591,17 +591,17 @@ public function testAnsiColorsAndEmojis() $this->generateOutput( " \033[44;37m Starting the demo... fingers crossed \033[0m\n". ' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n". - " \xf0\x9f\x8f\x81 1 sec \033[44;37m 0 B \033[0m" + " \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m" ). $this->generateOutput( " \033[44;37m Looks good to me... \033[0m\n". ' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n". - " \xf0\x9f\x8f\x81 1 sec \033[41;37m 97 KiB \033[0m" + " \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m" ). $this->generateOutput( " \033[44;37m Thanks, bye \033[0m\n". ' 15/15 '.str_repeat($done, 28)." 100%\n". - " \xf0\x9f\x8f\x81 1 sec \033[41;37m 195 KiB \033[0m" + " \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 195 KiB \033[0m" ), stream_get_contents($output->getStream()) );