- Output for 8.2.5
- Clock mock leaks at index 0
<?php
interface ClockInterface
{
public function now(): DateTimeImmutable;
}
class BadMockClock implements ClockInterface
{
public function __construct(private readonly DateTimeImmutable $now = new DateTimeImmutable())
{
}
public function now(): DateTimeImmutable
{
return $this->now;
}
}
class GoodMockClock implements ClockInterface
{
public function __construct(private readonly DateTimeImmutable $now = new DateTimeImmutable())
{
}
public function now(): DateTimeImmutable
{
return clone $this->now;
}
}
class Clock implements ClockInterface
{
public function now(): DateTimeImmutable
{
return new DateTimeImmutable();
}
}
$clocks = [
new BadMockClock(),
new GoodMockClock(),
new Clock(),
];
shuffle($clocks);
foreach ($clocks as $i => $clock) {
$t1 = $clock->now();
$t2 = $clock->now();
if ($t1 === $t2) {
echo "Clock mock leaks at index $i\n";
}
}