PHPackages                             bitwise-operators/mockclock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. bitwise-operators/mockclock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

bitwise-operators/mockclock
===========================

A simple implementation of psr/clock to be used in unit testing

1.1.0(10mo ago)031BSD-3-ClausePHPPHP &gt;=8.3

Since Aug 25Pushed 10mo agoCompare

[ Source](https://github.com/bitwise-operators/mockclock)[ Packagist](https://packagist.org/packages/bitwise-operators/mockclock)[ RSS](/packages/bitwise-operators-mockclock/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (5)Versions (4)Used By (0)

MockClock
=========

[](#mockclock)

A simple implementation of psr/clock to be used in unit testing.

Usage
-----

[](#usage)

This class is intended to be used when testing classes that use `Psr\Clock\ClockInterface` through dependency injection.

In your unit tests, create an instance of the clock and pass it to the constructor of the class under test. You can then alter the clock as desired, and check if the output of your class matches expectation.

Unless the clock is actively modified, the time on the clock does not change after instantiation. This way, repeated calls to the class under test should be idempotent (as far as time is concerned).

### Example

[](#example)

```
#[\PHPUnit\Framework\Attributes\Test]
public function test_the_clock(
): void {
    $clock = new \BitwiseOperators\MockClock();

    $myClass = new MyClass(clock: $clock);

    $firstValue = $myClass->myMethod();

    $clock->sleep(50);

    $secondValue = $myClass->myMethod();

    self::assertNotEquals($firstValue, $secondValue);
}
```

### Available methods

[](#available-methods)

The following methods are available to on the clock:

#### `now(): DateTimeImmutable`

[](#now-datetimeimmutable)

See

#### `add(DateTimeInterval): static`

[](#adddatetimeinterval-static)

Increments the time on the clock by the specified amount.

#### `modify(string): static`

[](#modifystring-static)

Modifies the clock according to the formats described in the [PHP manual](https://www.php.net/manual/en/datetime.formats.php).

#### `set(DateTimeInterface): static`

[](#setdatetimeinterface-static)

Sets the the clock to the specified date.

#### `setTimezone(DateTimeZone | string): static`

[](#settimezonedatetimezone--string-static)

Set the timezone of the clock to the specified timezone.

Note that a subsequent call to either `set()` or `modify()` may override this.

#### `sleep(float | int): static`

[](#sleepfloat--int-static)

Advances the clock the specified number of seconds

#### `startOfDay(): static`

[](#startofday-static)

Set the time on the clock to "00:00:00". The date is unaffected.

#### `sub(DateTimeInterval): static`

[](#subdatetimeinterval-static)

Turns back the time on the clock by the specified amount.

### Chaining

[](#chaining)

Aside from the `now()` method, all methods return the clock itself, allowing for chaining:

```
$clock = new \BitwiseOperators\MockClock()
    ->set(new \DateTime('1995-06-08'))
    ->startOfDay()
    ->add(new \DateTimeInterval('PT1H'))
    ->setTimezone('Europe/Copenhagen')
;
```

### Extending

[](#extending)

The MockClock is designed to be extendable. Simply create your own Clock class that extends the MockClock and add any extra methods you might need:

```
namespace Tests\Support;

class MyMockClock extends \BitwiseOperators\MockClock {
    public function setToEpoch(): static {
        return $this->set(new \DateTime('1970-01-01T00:00:00'));
    }
}
```

Even if you don't add methods immediately, extending the class and using your own is always a good idea as a form of [dependency inversion](https://en.wikipedia.org/wiki/Dependency_inversion_principle).

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance54

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

312d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/67020940?v=4)[bitwise-operators](/maintainers/bitwise-operators)[@bitwise-operators](https://github.com/bitwise-operators)

---

Top Contributors

[![bitwise-operators](https://avatars.githubusercontent.com/u/67020940?v=4)](https://github.com/bitwise-operators "bitwise-operators (1 commits)")

---

Tags

testingphpunitmockingpsr-clock

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bitwise-operators-mockclock/health.svg)

```
[![Health](https://phpackages.com/badges/bitwise-operators-mockclock/health.svg)](https://phpackages.com/packages/bitwise-operators-mockclock)
```

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k955.1M155.1k](/packages/phpunit-phpunit)[brianium/paratest

Parallel testing for PHP

2.5k136.1M986](/packages/brianium-paratest)[dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

57129.2M612](/packages/dg-bypass-finals)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69619.8M640](/packages/spatie-phpunit-snapshot-assertions)[nette/tester

Nette Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏

4957.6M1.7k](/packages/nette-tester)[yoast/phpunit-polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

18543.6M1.3k](/packages/yoast-phpunit-polyfills)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
