Skip to content

Commit

Permalink
SymfonyToOrisaiClockAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Nov 26, 2023
1 parent 27771ce commit ca2a281
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ jobs:
- name: "Composer"
uses: "orisai/github-workflows/.github/actions/setup-composer@v1"

- name: "Extra dependencies"
if: "!startsWith(${{ matrix.php-version }}, 7) && !startsWith(${{ matrix.php-version }}, 8.0)"
run: "composer require symfony/clock"

- name: "PHPStan"
uses: "orisai/github-workflows/.github/actions/phpstan@v1"
with:
Expand Down Expand Up @@ -130,6 +134,10 @@ jobs:
with:
command: "composer update --no-interaction --no-progress --prefer-dist ${{ matrix.composer-flags }}"

- name: "Extra dependencies"
if: "!startsWith(${{ matrix.php-version }}, 7) && !startsWith(${{ matrix.php-version }}, 8.0)"
run: "composer require symfony/clock"

- name: "PHPUnit"
uses: "orisai/github-workflows/.github/actions/phpunit@v1"
with:
Expand Down Expand Up @@ -198,6 +206,10 @@ jobs:
- name: "Composer"
uses: "orisai/github-workflows/.github/actions/setup-composer@v1"

- name: "Extra dependencies"
if: "!startsWith(${{ matrix.php-version }}, 7) && !startsWith(${{ matrix.php-version }}, 8.0)"
run: "composer require symfony/clock"

- name: "PHPUnit"
uses: "orisai/github-workflows/.github/actions/phpunit@v1"
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- `Clock` interface has a `sleep()` method
- `PsrToOrisaiClockAdapter` - makes any PSR clock compatible with `Orisai\Clock\Clock`
- `SymfonyToOrisaiClockAdapter` - makes any symfony/clock compatible with `Orisai\Clock\Clock`

### Changed

Expand Down
12 changes: 11 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Provides current time for runtime and controllable time for testing
- [Frozen clock](#frozen-clock)
- [Measurement clock](#measurement-clock)
- [PSR to Orisai clock adapter](#psr-to-orisai-clock-adapter)
- [Symfony to Orisai clock adapter](#symfony-to-orisai-clock-adapter)
- [Integrations and extensions](#integrations-and-extensions)

## Setup
Expand Down Expand Up @@ -159,7 +160,16 @@ use Orisai\Clock\Adapter\PsrToOrisaiClockAdapter;
$clock = new PsrToOrisaiClockAdapter(new ExamplePsrClock());
```

[Sleep](#sleep) uses standard `sleep()` function for non-Orisai clock implementation.
### Symfony to Orisai clock adapter

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

```php
use Orisai\Clock\Adapter\SymfonyToOrisaiClockAdapter;
use Symfony\Component\Clock\NativeClock;

$clock = new SymfonyToOrisaiClockAdapter(new NativeClock());
```

## Integrations and extensions

Expand Down
46 changes: 46 additions & 0 deletions src/Adapter/SymfonyToOrisaiClockAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace Orisai\Clock\Adapter;

use DateTimeImmutable;
use Orisai\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use function func_get_args;
use function trigger_error;
use const E_USER_WARNING;

final class SymfonyToOrisaiClockAdapter implements Clock
{

private ClockInterface $clock;

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

public function now(): DateTimeImmutable
{
return $this->clock->now();
}

/**
* @infection-ignore-all
*/
public function sleep(int $seconds = 0, int $milliseconds = 0, int $microseconds = 0): void
{
if (func_get_args() === []) {
trigger_error(
'Arguments must be passed to method sleep(), otherwise it does not do anything.',
E_USER_WARNING,
);
}

$this->clock->sleep(
$seconds
+ ($milliseconds / 1_000)
+ ($microseconds / 1_000_000),
);
}

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

namespace Tests\Orisai\Clock\Unit;

use DateTimeImmutable;
use Orisai\Clock\Adapter\SymfonyToOrisaiClockAdapter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Clock\NativeClock;
use function error_get_last;
use function interface_exists;
use function microtime;
use function usleep;

final class SymfonyToOrisaiClockAdapterTest extends TestCase
{

public static function setUpBeforeClass(): void
{
// Workaround for minimal PHP version - Symfony requires PHP 8.1, we only 7.4
if (!interface_exists(ClockInterface::class)) {
self::markTestSkipped('symfony/clock is not installed.');
}
}

public function testCurrentTime(): void
{
$clock = new SymfonyToOrisaiClockAdapter(new NativeClock());

$start = microtime(true);
usleep(1);

$now = $clock->now();

usleep(1);
$stop = microtime(true);

self::assertGreaterThan($start, (float) $now->format('U.u'));
self::assertLessThan($stop, (float) $now->format('U.u'));
}

public function testSleep(): void
{
$clock = new SymfonyToOrisaiClockAdapter(
new MockClock(DateTimeImmutable::createFromFormat('U', '10')),
);

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

$clock->sleep(1, 2, 3);
self::assertSame(
'11.002003',
$clock->now()->format('U.u'),
);
}

public function testSleepNoArgs(): void
{
$clock = new SymfonyToOrisaiClockAdapter(
new MockClock(DateTimeImmutable::createFromFormat('U', '0')),
);

@$clock->sleep();

self::assertSame(
'Arguments must be passed to method sleep(), otherwise it does not do anything.',
error_get_last()['message'] ?? null,
);
self::assertSame(
'0.000000',
$clock->now()->format('U.u'),
);
}

}
5 changes: 5 additions & 0 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ parameters:
"""
count: 9
path: ../tests/Unit/FrozenClockTest.php

-
message: "#^Parameter \\#1 \\$now of class Symfony\\\\Component\\\\Clock\\\\MockClock constructor expects DateTimeImmutable\\|string, DateTimeImmutable\\|false given\\.$#"
count: 2
path: ../tests/Unit/SymfonyToOrisaiClockAdapterTest.php

0 comments on commit ca2a281

Please sign in to comment.