Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / view / __tests__ / PhabricatorUnitsTestCase.php
blobaae9598ddf1d799912cc8cd86167e09fe99dd732
1 <?php
3 final class PhabricatorUnitsTestCase extends PhabricatorTestCase {
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.
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',
19 foreach ($tests as $input => $expect) {
20 $this->assertEqual(
21 $expect,
22 phutil_format_bytes($input),
23 'phutil_format_bytes('.$input.')');
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),
42 foreach ($tests as $input => $expect) {
43 $this->assertEqual(
44 $expect,
45 phutil_parse_bytes($input),
46 'phutil_parse_bytes('.$input.')');
49 $this->tryTestCases(
50 array('string' => 'string'),
51 array(false),
52 'phutil_parse_bytes');
55 public function testDetailedDurationFormatting() {
56 $expected_zero = 'now';
58 $tests = array(
59 12095939 => '19 w, 6 d',
60 -12095939 => '19 w, 6 d ago',
62 3380521 => '5 w, 4 d',
63 -3380521 => '5 w, 4 d ago',
65 0 => $expected_zero,
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.')');
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',
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',
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,
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.')');
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.')');