Skip to content

Commit

Permalink
OrisaiToSymfonyClockAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Nov 27, 2023
1 parent 989e6cc commit 197f604
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `PsrToOrisaiClockAdapter` - makes any PSR clock compatible with `Orisai\Clock\Clock`
- `SymfonyToOrisaiClockAdapter` - makes any symfony/clock compatible with `Orisai\Clock\Clock`
- `ClockAdapterFactory` - chooses best supported adapter for `Orisai\Clock\Clock` compatibility
- `OrisaiToSymfonyClockAdapter` - makes any symfony/clock compatible with `Symfony\Component\Clock\ClockInterface`
- `FrozenClock` - accepts `DateTimeInterface` as the initial time

### Changed
Expand Down
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Provides current time for runtime and controllable time for testing
- [Clock adapters](#clock-adapters)
- [PSR to Orisai clock adapter](#psr-to-orisai-clock-adapter)
- [Symfony to Orisai clock adapter](#symfony-to-orisai-clock-adapter)
- [Orisai to Symfony clock adapter](#orisai-to-symfony-clock-adapter)
- [Integrations and extensions](#integrations-and-extensions)

## Setup
Expand Down Expand Up @@ -206,6 +207,17 @@ use Symfony\Component\Clock\NativeClock;
$clock = new SymfonyToOrisaiClockAdapter(new NativeClock());
```

### Orisai to Symfony clock adapter

Decorate any `Orisai\Clock\Clock` implementation to conform interface `Symfony\Component\Clock\ClockInterface`.

```php
use Orisai\Clock\Adapter\OrisaiToSymfonyClockAdapter;
use Orisai\Clock\SystemClock;

$clock = new OrisaiToSymfonyClockAdapter(new SystemClock());
```

## Integrations and extensions

- [Nette](https://github.com/nette) integration - [orisai/nette-clock](https://github.com/orisai/nette-clock)
48 changes: 48 additions & 0 deletions src/Adapter/OrisaiToSymfonyClockAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace Orisai\Clock\Adapter;

use DateTimeImmutable;
use DateTimeZone;
use Orisai\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use function is_string;
use function round;

final class OrisaiToSymfonyClockAdapter implements ClockInterface
{

private Clock $clock;

private ?DateTimeZone $timeZone = null;

public function __construct(Clock $clock)
{
$this->clock = $clock;
}

public function now(): DateTimeImmutable
{
$dt = $this->clock->now();

if ($this->timeZone !== null) {
$dt = $dt->setTimezone($this->timeZone);
}

return $dt;
}

public function sleep(float|int $seconds): void
{
$this->clock->sleep(0, 0, (int) round($seconds * 1E6));

Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ } public function sleep(float|int $seconds) : void { - $this->clock->sleep(0, 0, (int) round($seconds * 1000000.0)); + $this->clock->sleep(-1, 0, (int) round($seconds * 1000000.0)); } public function withTimeZone(DateTimeZone|string $timezone) : static {

Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ } public function sleep(float|int $seconds) : void { - $this->clock->sleep(0, 0, (int) round($seconds * 1000000.0)); + $this->clock->sleep(0, -1, (int) round($seconds * 1000000.0)); } public function withTimeZone(DateTimeZone|string $timezone) : static {

Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Escaped Mutant for Mutator "RoundingFamily": --- Original +++ New @@ @@ } public function sleep(float|int $seconds) : void { - $this->clock->sleep(0, 0, (int) round($seconds * 1000000.0)); + $this->clock->sleep(0, 0, (int) ceil($seconds * 1000000.0)); } public function withTimeZone(DateTimeZone|string $timezone) : static {
}

public function withTimeZone(DateTimeZone|string $timezone): static
{
$clone = clone $this;

Check warning on line 42 in src/Adapter/OrisaiToSymfonyClockAdapter.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ } public function withTimeZone(DateTimeZone|string $timezone) : static { - $clone = clone $this; + $clone = $this; $clone->timeZone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone; return $clone; } }
$clone->timeZone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone;

return $clone;
}

}
47 changes: 47 additions & 0 deletions tests/Unit/Adapter/OrisaiToSymfonyClockAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\Clock\Unit\Adapter;

use DateTimeZone;
use Orisai\Clock\Adapter\OrisaiToSymfonyClockAdapter;
use Orisai\Clock\FrozenClock;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\ClockInterface;
use function interface_exists;

final class OrisaiToSymfonyClockAdapterTest extends TestCase
{

public function test(): void
{
if (!interface_exists(ClockInterface::class)) {
self::markTestSkipped('symfony/clock is required.');
}

$innerClock = new FrozenClock(0, new DateTimeZone('UTC'));
$clock = new OrisaiToSymfonyClockAdapter($innerClock);

self::assertSame(
'0.000000',
$clock->now()->format('U.u'),
);

$clock->sleep(1.234_567_8);
self::assertSame(
'1.234568',
$clock->now()->format('U.u'),
);

self::assertEquals(
$clock->now()->getTimezone(),
new DateTimeZone('UTC'),
);

$clock = $clock->withTimeZone(new DateTimeZone('Europe/Prague'));
self::assertEquals(
$clock->now()->getTimezone(),
new DateTimeZone('Europe/Prague'),
);
}

}
2 changes: 1 addition & 1 deletion tools/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ includes:
- phpstan.baseline.neon

parameters:
phpVersion: 70400
phpVersion: 80100
level: 8
tmpDir: ../var/tools/PHPStan
resultCachePath: %currentWorkingDirectory%/var/tools/PHPStan/resultCache.php
Expand Down

0 comments on commit 197f604

Please sign in to comment.