8000 fix transient NativeClock test · enumag/symfony@05e7de4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05e7de4

Browse files
committed
fix transient NativeClock test
1 parent 2e87d30 commit 05e7de4

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Symfony/Component/Clock/NativeClock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(\DateTimeZone|string $timezone = null)
3131

3232
public function now(): \DateTimeImmutable
3333
{
34-
return new \DateTimeImmutable('now', $this->timezone);
34+
return \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6f', microtime(true)))->setTimezone($this->timezone);
3535
}
3636

3737
public function sleep(float|int $seconds): void

src/Symfony/Component/Clock/Tests/NativeClockTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Clock\NativeClock;
1616

17+
/**
18+
* @group time-sensitive
19+
*/
1720
class NativeClockTest extends TestCase
1821
{
1922
public function testConstruct()
@@ -32,14 +35,14 @@ public function testConstruct()
3235
public function testNow()
3336
{
3437
$clock = new NativeClock();
35-
$before = new \DateTimeImmutable();
38+
$before = microtime(true);
3639
usleep(10);
3740
$now = $clock->now();
3841
usleep(10);
39-
$after = new \DateTimeImmutable();
42+
$after = microtime(true);
4043

41-
$this->assertGreaterThan($before, $now);
42-
$this->assertLessThan($after, $now);
44+
$this->assertGreaterThan($before, (float) $now->format('U.u'));
45+
$this->assertLessThan($after, (float) $now->format('U.u'));
4346
}
4447

4548
public function testSleep()
@@ -53,7 +56,7 @@ public function testSleep()
5356
usleep(10);
5457
$after = microtime(true);
5558

56-
$this->assertGreaterThan($before + 1.5, $now);
59+
$this->assertSame($before + 1.5, $now);
5760
$this->assertLessThan($after, $now);
5861
$this->assertLessThan(1.9, $now - $before);
5962
$this->assertSame($tz, $clock->now()->getTimezone()->getName());

0 commit comments

Comments
 (0)
0