ArrayUtilTest.php
1 year ago
StringUtilTest.php
1 year ago
TimeUnitTest.php
1 year ago
TimeUtilTest.php
1 year ago
TimeValueTest.php
1 year ago
UrlUtilTest.php
1 year ago
TimeValueTest.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Util; |
| 4 | |
| 5 | class TimeValueTest extends \WonderPush\TestCase { |
| 6 | |
| 7 | public function testCompareTo() { |
| 8 | $this->assertEquals(-1, TimeValue::Seconds( 1)->compareTo(TimeValue::Seconds( 2))); |
| 9 | $this->assertEquals( 0, TimeValue::Seconds( 1)->compareTo(TimeValue::Seconds( 1))); |
| 10 | $this->assertEquals(+1, TimeValue::Seconds( 2)->compareTo(TimeValue::Seconds( 1))); |
| 11 | |
| 12 | $this->assertEquals(-1, TimeValue::Seconds( 1)->compareTo(TimeValue::Minutes( 1))); |
| 13 | $this->assertEquals( 0, TimeValue::Seconds(60)->compareTo(TimeValue::Minutes( 1))); |
| 14 | $this->assertEquals( 0, TimeValue::Minutes( 1)->compareTo(TimeValue::Minutes( 1))); |
| 15 | $this->assertEquals( 0, TimeValue::Minutes( 1)->compareTo(TimeValue::Seconds(60))); |
| 16 | $this->assertEquals(+1, TimeValue::Minutes( 1)->compareTo(TimeValue::Seconds( 1))); |
| 17 | } |
| 18 | |
| 19 | public function testToString() { |
| 20 | $this->assertEquals('42ns', (string)new TimeValue(42, TimeUnit::NANOSECONDS)); |
| 21 | $this->assertEquals('42us', (string)new TimeValue(42, TimeUnit::MICROSECONDS)); |
| 22 | $this->assertEquals('42ms', (string)new TimeValue(42, TimeUnit::MILLISECONDS)); |
| 23 | $this->assertEquals('42s', (string)new TimeValue(42, TimeUnit::SECONDS)); |
| 24 | $this->assertEquals('42m', (string)new TimeValue(42, TimeUnit::MINUTES)); |
| 25 | $this->assertEquals('42h', (string)new TimeValue(42, TimeUnit::HOURS)); |
| 26 | $this->assertEquals('42d', (string)new TimeValue(42, TimeUnit::DAYS)); |
| 27 | $this->assertEquals('42w', (string)new TimeValue(42, TimeUnit::WEEKS)); |
| 28 | $this->assertEquals('-1s', (string)new TimeValue(-1, TimeUnit::SECONDS)); |
| 29 | } |
| 30 | |
| 31 | public function testParse() { |
| 32 | $this->assertEquals(null, TimeValue::parse(null)); |
| 33 | $this->assertEquals(null, TimeValue::parse('')); |
| 34 | $this->assertEquals(new TimeValue(0, TimeUnit::MILLISECONDS), TimeValue::parse('0')); |
| 35 | $this->assertEquals(new TimeValue(0, TimeUnit::MILLISECONDS), TimeValue::parse('0.')); |
| 36 | $this->assertEquals(new TimeValue(0, TimeUnit::MILLISECONDS), TimeValue::parse('.')); |
| 37 | $this->assertEquals(new TimeValue(0, TimeUnit::MILLISECONDS), TimeValue::parse('.0')); |
| 38 | |
| 39 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42s')); |
| 40 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42 s')); |
| 41 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42seconds')); |
| 42 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42 seconds')); |
| 43 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse(' 42 seconds')); |
| 44 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42 seconds ')); |
| 45 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse(' 42 seconds ')); |
| 46 | |
| 47 | $this->assertEquals(new TimeValue(42, TimeUnit::NANOSECONDS), TimeValue::parse('42ns')); |
| 48 | $this->assertEquals(new TimeValue(42, TimeUnit::MICROSECONDS), TimeValue::parse('42us')); |
| 49 | $this->assertEquals(new TimeValue(42, TimeUnit::MILLISECONDS), TimeValue::parse('42ms')); |
| 50 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::parse('42s')); |
| 51 | $this->assertEquals(new TimeValue(42, TimeUnit::MINUTES), TimeValue::parse('42m')); |
| 52 | $this->assertEquals(new TimeValue(42, TimeUnit::HOURS), TimeValue::parse('42h')); |
| 53 | $this->assertEquals(new TimeValue(42, TimeUnit::DAYS), TimeValue::parse('42d')); |
| 54 | $this->assertEquals(new TimeValue(42, TimeUnit::WEEKS), TimeValue::parse('42w')); |
| 55 | |
| 56 | $this->assertEquals(new TimeValue(+42, TimeUnit::SECONDS), TimeValue::parse('+42s')); |
| 57 | $this->assertEquals(new TimeValue(-42, TimeUnit::SECONDS), TimeValue::parse('-42s')); |
| 58 | } |
| 59 | |
| 60 | public function testNanoseconds() { |
| 61 | $this->assertEquals(new TimeValue(42, TimeUnit::NANOSECONDS), TimeValue::Nanoseconds(42)); |
| 62 | } |
| 63 | |
| 64 | public function testMicroseconds() { |
| 65 | $this->assertEquals(new TimeValue(42, TimeUnit::MICROSECONDS), TimeValue::Microseconds(42)); |
| 66 | } |
| 67 | |
| 68 | public function testMilliseconds() { |
| 69 | $this->assertEquals(new TimeValue(42, TimeUnit::MILLISECONDS), TimeValue::Milliseconds(42)); |
| 70 | } |
| 71 | |
| 72 | public function testSeconds() { |
| 73 | $this->assertEquals(new TimeValue(42, TimeUnit::SECONDS), TimeValue::Seconds(42)); |
| 74 | } |
| 75 | |
| 76 | public function testMinutes() { |
| 77 | $this->assertEquals(new TimeValue(42, TimeUnit::MINUTES), TimeValue::Minutes(42)); |
| 78 | } |
| 79 | |
| 80 | public function testHours() { |
| 81 | $this->assertEquals(new TimeValue(42, TimeUnit::HOURS), TimeValue::Hours(42)); |
| 82 | } |
| 83 | |
| 84 | public function testDays() { |
| 85 | $this->assertEquals(new TimeValue(42, TimeUnit::DAYS), TimeValue::Days(42)); |
| 86 | } |
| 87 | |
| 88 | public function testWeeks() { |
| 89 | $this->assertEquals(new TimeValue(42, TimeUnit::WEEKS), TimeValue::Weeks(42)); |
| 90 | } |
| 91 | |
| 92 | public function testTo() { |
| 93 | $tv = new TimeValue(42, TimeUnit::NANOSECONDS); |
| 94 | $this->assertEquals(0.000042, $tv->to(TimeUnit::MILLISECONDS)); |
| 95 | $tv = new TimeValue(42, TimeUnit::MILLISECONDS); |
| 96 | $this->assertEquals(42000000, $tv->to(TimeUnit::NANOSECONDS)); |
| 97 | } |
| 98 | |
| 99 | public function testConvert() { |
| 100 | $tv = new TimeValue(42, TimeUnit::NANOSECONDS); |
| 101 | $this->assertEquals(new TimeValue(0.000042, TimeUnit::MILLISECONDS), $tv->convert(TimeUnit::MILLISECONDS)); |
| 102 | $tv = new TimeValue(42, TimeUnit::MILLISECONDS); |
| 103 | $this->assertEquals(new TimeValue(42000000, TimeUnit::NANOSECONDS), $tv->convert(TimeUnit::NANOSECONDS)); |
| 104 | } |
| 105 | |
| 106 | public function testToNanoseconds() { |
| 107 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 108 | $this->assertEquals(7 * 24 * 60 * 60 * 1000 * 1000 * 1000, $tv->toNanoseconds()); |
| 109 | } |
| 110 | |
| 111 | public function testToMicroseconds() { |
| 112 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 113 | $this->assertEquals(7 * 24 * 60 * 60 * 1000 * 1000, $tv->toMicroseconds()); |
| 114 | } |
| 115 | |
| 116 | public function testToMilliseconds() { |
| 117 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 118 | $this->assertEquals(7 * 24 * 60 * 60 * 1000, $tv->toMilliseconds()); |
| 119 | } |
| 120 | |
| 121 | public function testToSeconds() { |
| 122 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 123 | $this->assertEquals(7 * 24 * 60 * 60, $tv->toSeconds()); |
| 124 | } |
| 125 | |
| 126 | public function testToMinutes() { |
| 127 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 128 | $this->assertEquals(7 * 24 * 60, $tv->toMinutes()); |
| 129 | } |
| 130 | |
| 131 | public function testToHours() { |
| 132 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 133 | $this->assertEquals(7 * 24, $tv->toHours()); |
| 134 | } |
| 135 | |
| 136 | public function testToDays() { |
| 137 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 138 | $this->assertEquals(7, $tv->toDays()); |
| 139 | } |
| 140 | |
| 141 | public function testToWeeks() { |
| 142 | $tv = new TimeValue(1, TimeUnit::WEEKS); |
| 143 | $this->assertEquals(1, $tv->toWeeks()); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 |