8000 [Clock] Add ClockAwareTrait to help write time-sensitive classes by nicolas-grekas · Pull Request #48362 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Clock] Add ClockAwareTrait to help write time-sensitive classes #48362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Clock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `ClockAwareTrait` to help write time-sensitive classes

6.2
---

Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/Clock/ClockAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

use Psr\Clock\ClockInterface;
use Symfony\Contracts\Service\Attribute\Required;

/**
* A trait to help write time-sensitive classes.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait ClockAwareTrait
{
private readonly ClockInterface $clock;

#[Required]
public function setClock(ClockInterface $clock): void
{
$this->clock = $clock;
}

protected function now(): \DateTimeImmutable
{
return ($this->clock ??= new NativeClock())->now();
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Clock/Tests/ClockAwareTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\ClockAwareTrait;
use Symfony\Component\Clock\MockClock;

class ClockAwareTraitTest extends TestCase
{
public function testTrait()
{
$sut = new class() {
use ClockAwareTrait {
now as public;
}
};

$this->assertInstanceOf(\DateTimeImmutable::class, $sut->now());

$clock = new MockClock();
$sut = new $sut();
$sut->setClock($clock);

$ts = $sut->now()->getTimestamp();
$this->assertEquals($clock->now(), $sut->now());
$clock->sleep(1);
$this->assertEquals($clock->now(), $sut->now());
$this->assertSame(1.0, round($sut->now()->getTimestamp() - $ts, 1));
}
}
0