From 6d2c92575da69b17f859c302122bcdd3f96640a4 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 1 Feb 2020 23:03:23 +0000 Subject: [PATCH] tests for format_milliseconds --- core/imageboard/misc.php | 20 -------------------- core/polyfills.php | 18 ++++++++++++++++++ core/tests/polyfills.test.php | 12 +++++++++++- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 75452f70..2cfcfa44 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -196,23 +196,3 @@ function create_image_thumb(string $hash, string $type, string $engine = null) $config->get_bool('thumb_upscale', false) )); } - - -const TIME_UNITS = ["s"=>60,"m"=>60,"h"=>24,"d"=>365,"y"=>PHP_INT_MAX]; -function format_milliseconds(int $input): string -{ - $output = ""; - - $remainder = floor($input / 1000); - - foreach (TIME_UNITS as $unit=>$conversion) { - $count = $remainder % $conversion; - $remainder = floor($remainder / $conversion); - if ($count==0&&$remainder<1) { - break; - } - $output = "$count".$unit." ".$output; - } - - return trim($output); -} diff --git a/core/polyfills.php b/core/polyfills.php index 3fab2e15..7a3af678 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -593,6 +593,24 @@ function to_shorthand_int(int $int): string } } +const TIME_UNITS = ["s"=>60,"m"=>60,"h"=>24,"d"=>365,"y"=>PHP_INT_MAX]; +function format_milliseconds(int $input): string +{ + $output = ""; + + $remainder = floor($input / 1000); + + foreach (TIME_UNITS as $unit=>$conversion) { + $count = $remainder % $conversion; + $remainder = floor($remainder / $conversion); + if ($count==0&&$remainder<1) { + break; + } + $output = "$count".$unit." ".$output; + } + + return trim($output); +} /** * Turn a date into a time, a date, an "X minutes ago...", etc diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index 84497096..00073668 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -89,17 +89,27 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(truncate("te...", 2), "te..."); } - public function test_shorthand_int() + public function test_to_shorthand_int() { $this->assertEquals(to_shorthand_int(1231231231), "1.1GB"); $this->assertEquals(to_shorthand_int(2), "2"); + } + public function test_parse_shorthand_int() + { $this->assertEquals(parse_shorthand_int("foo"), -1); $this->assertEquals(parse_shorthand_int("32M"), 33554432); $this->assertEquals(parse_shorthand_int("43.4KB"), 44441); $this->assertEquals(parse_shorthand_int("1231231231"), 1231231231); } + public function test_format_milliseconds() + { + $this->assertEquals("", format_milliseconds(5)); + $this->assertEquals("5s", format_milliseconds(5000)); + $this->assertEquals("1y 213d 16h 53m 20s", format_milliseconds(50000000000)); + } + public function test_autodate() { $this->assertEquals(