Path: blob/master/src/view/__tests__/PhabricatorUnitsTestCase.php
12249 views
<?php12final class PhabricatorUnitsTestCase extends PhabricatorTestCase {34// NOTE: Keep tests below PHP_INT_MAX on 32-bit systems, since if you write5// larger numeric literals they'll evaluate to nonsense.67public function testByteFormatting() {8$tests = array(91 => '1 B',101024 => '1 KB',111024 * 1024 => '1 MB',1210 * 1024 * 1024 => '10 MB',13100 * 1024 * 1024 => '100 MB',141024 * 1024 * 1024 => '1 GB',15999 => '999 B',16);1718foreach ($tests as $input => $expect) {19$this->assertEqual(20$expect,21phutil_format_bytes($input),22'phutil_format_bytes('.$input.')');23}24}2526public function testByteParsing() {27$tests = array(28'1' => 1,29'1k' => 1024,30'1K' => 1024,31'1kB' => 1024,32'1Kb' => 1024,33'1KB' => 1024,34'1MB' => 1024 * 1024,35'1GB' => 1024 * 1024 * 1024,36'1.5M' => (int)(1024 * 1024 * 1.5),37'1 000' => 1000,38'1,234.56 KB' => (int)(1024 * 1234.56),39);4041foreach ($tests as $input => $expect) {42$this->assertEqual(43$expect,44phutil_parse_bytes($input),45'phutil_parse_bytes('.$input.')');46}4748$this->tryTestCases(49array('string' => 'string'),50array(false),51'phutil_parse_bytes');52}5354public function testDetailedDurationFormatting() {55$expected_zero = 'now';5657$tests = array(5812095939 => '19 w, 6 d',59-12095939 => '19 w, 6 d ago',60613380521 => '5 w, 4 d',62-3380521 => '5 w, 4 d ago',63640 => $expected_zero,65);6667foreach ($tests as $duration => $expect) {68$this->assertEqual(69$expect,70phutil_format_relative_time_detailed($duration),71'phutil_format_relative_time_detailed('.$duration.')');72}737475$tests = array(763380521 => array(77-1 => '5 w',780 => '5 w',791 => '5 w',802 => '5 w, 4 d',813 => '5 w, 4 d, 3 h',824 => '5 w, 4 d, 3 h, 2 m',835 => '5 w, 4 d, 3 h, 2 m, 1 s',846 => '5 w, 4 d, 3 h, 2 m, 1 s',85),8687-3380521 => array(88-1 => '5 w ago',890 => '5 w ago',901 => '5 w ago',912 => '5 w, 4 d ago',923 => '5 w, 4 d, 3 h ago',934 => '5 w, 4 d, 3 h, 2 m ago',945 => '5 w, 4 d, 3 h, 2 m, 1 s ago',956 => '5 w, 4 d, 3 h, 2 m, 1 s ago',96),97980 => array(99-1 => $expected_zero,1000 => $expected_zero,1011 => $expected_zero,1022 => $expected_zero,1033 => $expected_zero,1044 => $expected_zero,1055 => $expected_zero,1066 => $expected_zero,107),108);109110foreach ($tests as $duration => $sub_tests) {111if (is_array($sub_tests)) {112foreach ($sub_tests as $levels => $expect) {113$this->assertEqual(114$expect,115phutil_format_relative_time_detailed($duration, $levels),116'phutil_format_relative_time_detailed('.$duration.',117'.$levels.')');118}119} else {120$expect = $sub_tests;121$this->assertEqual(122$expect,123phutil_format_relative_time_detailed($duration),124'phutil_format_relative_time_detailed('.$duration.')');125126}127}128}129}130131132