Skip to content

Commit 3eb8985

Browse files
committed
Update test suite and complete the Formatter class along with its tests
1 parent 6a3f3ab commit 3eb8985

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

src/Formatter.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,41 @@
1111
*/
1212
class Formatter {
1313

14+
private function __construct() {
15+
}
16+
17+
public static function formatTime(int $miliseconds): string {
18+
$units = [ // Do not reorder the array order.
19+
31536000000 => ['1 year', '@count years'],
20+
2592000000 => ['1 month', '@count months'],
21+
604800000 => ['1 week', '@count weeks'],
22+
86400000 => ['1 day', '@count days'],
23+
3600000 => ['1 hour', '@count hours'],
24+
60000 => ['1 min', '@count min'],
25+
1000 => ['1 sec', '@count sec'],
26+
1 => ['1 ms', '@count ms'],
27+
];
28+
29+
$granularity = 2;
30+
$output = [];
31+
foreach ($units as $value => $string_pair) {
32+
if ($miliseconds >= $value) {
33+
$output[] = static::formatPlural((int) floor($miliseconds / $value), $string_pair[0], $string_pair[1]);
34+
$miliseconds %= $value;
35+
$granularity--;
36+
}
37+
if ($granularity === 0) {
38+
break;
39+
}
40+
}
41+
42+
return $output ? implode(' ', $output) : '0 sec';
43+
}
44+
45+
protected static function formatPlural(int $count, string $singular, string $plural, array $args = array()): string {
46+
$args['@count'] = $count;
47+
return $count === 1
48+
? strtr($singular, $args)
49+
: strtr($plural, $args);
50+
}
1451
}

src/Timer.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,8 @@ private static function formatTime(float $value, $format): string {
122122
}
123123

124124
private static function secondsToTimeString(float $time): string {
125-
$ms = \round($time * 1000);
126-
127-
foreach (self::TIMES as $unit => $value) {
128-
if ($ms >= $value) {
129-
$time = floor($ms / $value * 100.0) / 100.0;
130-
return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
131-
}
132-
}
133-
134-
return $ms . ' ms';
125+
$ms = (int) \round($time * 1000);
126+
return Formatter::formatTime($ms);
135127
}
136128

137129
/**

tests/FormatterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Ayesh\PHP_Timer\Tests;
4+
5+
use Ayesh\PHP_Timer\Formatter;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FormatterTest extends TestCase {
9+
public function testClassInit(): void {
10+
$this->expectException(\Error::class);
11+
$formatter = new Formatter();
12+
}
13+
14+
public function testSecondFormatter(): void {
15+
$count = Formatter::formatTime(30000);
16+
$this->assertSame('30 sec', $count);
17+
}
18+
19+
public function testSecondMinuteFormatter(): void {
20+
$count = Formatter::formatTime(65000);
21+
$this->assertSame('1 min 5 sec', $count);
22+
}
23+
24+
public function testDaySecondFormatting(): void {
25+
$count = Formatter::formatTime(86400000 + 5000);
26+
$this->assertSame('1 day 5 sec', $count);
27+
}
28+
}

tests/TimerFormatterTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
* @group time-sensitive
1111
*/
1212
class TimerFormatterTest extends TestCase {
13-
14-
public function testTimerFormat_Human(): void {
13+
public function testTimerFormat_Human3(): void {
1514
Timer::start(__FUNCTION__);
1615

17-
usleep(1000);
16+
sleep(1);
1817
$read = Timer::read(__FUNCTION__, Timer::FORMAT_HUMAN);
19-
$this->assertSame('1 ms', $read);
18+
$this->assertSame('1 sec', $read);
2019

2120
sleep(1);
2221
$read = Timer::read(__FUNCTION__, Timer::FORMAT_HUMAN);
23-
$this->assertSame('1 second', $read);
22+
$this->assertSame('2 sec', $read);
23+
24+
sleep(60);
25+
$read = Timer::read(__FUNCTION__, Timer::FORMAT_HUMAN);
26+
$this->assertSame('1 min 2 sec', $read);
27+
28+
sleep(86400);
29+
$read = Timer::read(__FUNCTION__, Timer::FORMAT_HUMAN);
30+
$this->assertSame('1 day 1 min', $read);
2431
}
2532
}

0 commit comments

Comments
 (0)