@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorUnitsTestCase extends PhabricatorTestCase {
4
5 // NOTE: Keep tests below PHP_INT_MAX on 32-bit systems, since if you write
6 // larger numeric literals they'll evaluate to nonsense.
7
8 public function testByteFormatting() {
9 $tests = array(
10 1 => '1 B',
11 1024 => '1 KB',
12 1024 * 1024 => '1 MB',
13 10 * 1024 * 1024 => '10 MB',
14 100 * 1024 * 1024 => '100 MB',
15 1024 * 1024 * 1024 => '1 GB',
16 999 => '999 B',
17 );
18
19 foreach ($tests as $input => $expect) {
20 $this->assertEqual(
21 $expect,
22 phutil_format_bytes($input),
23 'phutil_format_bytes('.$input.')');
24 }
25 }
26
27 public function testByteParsing() {
28 $tests = array(
29 '1' => 1,
30 '1k' => 1024,
31 '1K' => 1024,
32 '1kB' => 1024,
33 '1Kb' => 1024,
34 '1KB' => 1024,
35 '1MB' => 1024 * 1024,
36 '1GB' => 1024 * 1024 * 1024,
37 '1.5M' => (int)(1024 * 1024 * 1.5),
38 '1 000' => 1000,
39 '1,234.56 KB' => (int)(1024 * 1234.56),
40 );
41
42 foreach ($tests as $input => $expect) {
43 $this->assertEqual(
44 $expect,
45 phutil_parse_bytes($input),
46 'phutil_parse_bytes('.$input.')');
47 }
48
49 $this->tryTestCases(
50 array('string' => 'string'),
51 array(false),
52 'phutil_parse_bytes');
53 }
54
55 public function testDetailedDurationFormatting() {
56 $expected_zero = 'now';
57
58 $tests = array(
59 12095939 => '19 w, 6 d',
60 -12095939 => '19 w, 6 d ago',
61
62 3380521 => '5 w, 4 d',
63 -3380521 => '5 w, 4 d ago',
64
65 0 => $expected_zero,
66 );
67
68 foreach ($tests as $duration => $expect) {
69 $this->assertEqual(
70 $expect,
71 phutil_format_relative_time_detailed($duration),
72 'phutil_format_relative_time_detailed('.$duration.')');
73 }
74
75
76 $tests = array(
77 3380521 => array(
78 -1 => '5 w',
79 0 => '5 w',
80 1 => '5 w',
81 2 => '5 w, 4 d',
82 3 => '5 w, 4 d, 3 h',
83 4 => '5 w, 4 d, 3 h, 2 m',
84 5 => '5 w, 4 d, 3 h, 2 m, 1 s',
85 6 => '5 w, 4 d, 3 h, 2 m, 1 s',
86 ),
87
88 -3380521 => array(
89 -1 => '5 w ago',
90 0 => '5 w ago',
91 1 => '5 w ago',
92 2 => '5 w, 4 d ago',
93 3 => '5 w, 4 d, 3 h ago',
94 4 => '5 w, 4 d, 3 h, 2 m ago',
95 5 => '5 w, 4 d, 3 h, 2 m, 1 s ago',
96 6 => '5 w, 4 d, 3 h, 2 m, 1 s ago',
97 ),
98
99 0 => array(
100 -1 => $expected_zero,
101 0 => $expected_zero,
102 1 => $expected_zero,
103 2 => $expected_zero,
104 3 => $expected_zero,
105 4 => $expected_zero,
106 5 => $expected_zero,
107 6 => $expected_zero,
108 ),
109 );
110
111 foreach ($tests as $duration => $sub_tests) {
112 if (is_array($sub_tests)) {
113 foreach ($sub_tests as $levels => $expect) {
114 $this->assertEqual(
115 $expect,
116 phutil_format_relative_time_detailed($duration, $levels),
117 'phutil_format_relative_time_detailed('.$duration.',
118 '.$levels.')');
119 }
120 } else {
121 $expect = $sub_tests;
122 $this->assertEqual(
123 $expect,
124 phutil_format_relative_time_detailed($duration),
125 'phutil_format_relative_time_detailed('.$duration.')');
126
127 }
128 }
129 }
130}