|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Clock\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Clock\HrClock; |
| 16 | + |
| 17 | +class HrClockTest extends TestCase |
| 18 | +{ |
| 19 | + public function testNow() |
| 20 | + { |
| 21 | + $clock = new HrClock(); |
| 22 | + $before = new \DateTimeImmutable(sprintf('@%.6f', hrtime(true) / 1E9)); |
| 23 | + usleep(10); |
| 24 | + $now = $clock->now(); |
| 25 | + usleep(10); |
| 26 | + $after = new \DateTimeImmutable(sprintf('@%.6f', hrtime(true) / 1E9)); |
| 27 | + |
| 28 | + $this->assertGreaterThan($before, $now); |
| 29 | + $this->assertLessThan($after, $now); |
| 30 | + } |
| 31 | + |
| 32 | + public function testConstruct() |
| 33 | + { |
| 34 | + $clock = new HrClock('UTC'); |
| 35 | + $this->assertSame('UTC', $clock->now()->getTimezone()->getName()); |
| 36 | + |
| 37 | + $tz = date_default_timezone_get(); |
| 38 | + $clock = new HrClock(); |
| 39 | + $this->assertSame($tz, $clock->now()->getTimezone()->getName()); |
| 40 | + |
| 41 | + $clock = new HrClock(new \DateTimeZone($tz)); |
| 42 | + $this->assertSame($tz, $clock->now()->getTimezone()->getName()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testTimeArray() |
| 46 | + { |
| 47 | + $clock = new HrClock(); |
| 48 | + $before = hrtime(true) / 1E9; |
| 49 | + usleep(10); |
| 50 | + $now = $clock->timeArray(); |
| 51 | + usleep(10); |
| 52 | + $after = hrtime(true) / 1E9; |
| 53 | + |
| 54 | + $this->assertSame([0, 1], array_keys($now)); |
| 55 | + $this->assertGreaterThan($before, $now[0] + $now[1] / 1E6); |
| 56 | + $this->assertLessThan($after, $now[0] + $now[1] / 1E6); |
| 57 | + } |
| 58 | + |
| 59 | + public function testTimeFloat() |
| 60 | + { |
| 61 | + $clock = new HrClock(); |
| 62 | + $before = hrtime(true) / 1E9; |
| 63 | + usleep(10); |
| 64 | + $now = $clock->timeFloat(); |
| 65 | + usleep(10); |
| 66 | + $after = hrtime(true) / 1E9; |
| 67 | + |
| 68 | + $this->assertGreaterThan($before, $now); |
| 69 | + $this->assertLessThan($after, $now); |
| 70 | + } |
| 71 | + |
| 72 | + public function testSleep() |
| 73 | + { |
| 74 | + $clock = new HrClock(); |
| 75 | + $tz = $clock->now()->getTimezone()->getName(); |
| 76 | + |
| 77 | + $before = hrtime(true) / 1E9; |
| 78 | + $clock->sleep(1.5); |
| 79 | + $now = $clock->timeFloat(); |
| 80 | + usleep(10); |
| 81 | + $after = hrtime(true) / 1E9; |
| 82 | + |
| 83 | + $this->assertGreaterThan($before + 1.5, $now); |
| 84 | + $this->assertLessThan($after, $now); |
| 85 | + $this->assertLessThan(1.9, $now - $before); |
| 86 | + $this->assertSame($tz, $clock->now()->getTimezone()->getName()); |
| 87 | + } |
| 88 | +} |
0 commit comments