1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MonotonicClock;
#[\PHPUnit\Framework\Attributes\Group('time-sensitive')]
class MonotonicClockTest extends TestCase
{
public function testConstruct()
{
$clock = new MonotonicClock('UTC');
$this->assertSame('UTC', $clock->now()->getTimezone()->getName());
$tz = date_default_timezone_get();
$clock = new MonotonicClock();
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
$clock = new MonotonicClock(new \DateTimeZone($tz));
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
}
public function testNow()
{
$clock = new MonotonicClock();
$before = microtime(true);
usleep(10);
$now = $clock->now();
usleep(10);
$after = microtime(true);
$this->assertGreaterThan($before, (float) $now->format('U.u'));
$this->assertLessThan($after, (float) $now->format('U.u'));
}
public function testSleep()
{
$clock = new MonotonicClock();
$tz = $clock->now()->getTimezone()->getName();
$before = microtime(true);
$clock->sleep(1.5);
$now = (float) $clock->now()->format('U.u');
usleep(10);
$after = microtime(true);
$this->assertGreaterThanOrEqual($before + 1.499999, $now);
$this->assertLessThan($after, $now);
$this->assertLessThan(1.9, $now - $before);
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
}
public function testWithTimeZone()
{
$clock = new MonotonicClock();
$utcClock = $clock->withTimeZone('UTC');
$this->assertNotSame($clock, $utcClock);
$this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
}
}
|