92 lines
2.5 KiB
PHP
92 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests\Metrics;
|
|
|
|
use Monitor\Api\Metrics\HookCounter;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class HookCounterTest extends TestCase
|
|
{
|
|
private string $path;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->path = sys_get_temp_dir() . '/monitor-hook-counter-' . uniqid() . '/hooks-count';
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (file_exists($this->path)) {
|
|
unlink($this->path);
|
|
}
|
|
if (is_dir(dirname($this->path))) {
|
|
rmdir(dirname($this->path));
|
|
}
|
|
}
|
|
|
|
public function testCountIsZeroWithoutAnyIncrement(): void
|
|
{
|
|
self::assertSame(0, (new HookCounter($this->path))->count());
|
|
}
|
|
|
|
public function testIncrementReturnsTheNewCountAndPersistsIt(): void
|
|
{
|
|
$counter = new HookCounter($this->path);
|
|
|
|
self::assertSame(1, $counter->increment());
|
|
self::assertSame(2, $counter->increment());
|
|
self::assertSame(3, $counter->increment());
|
|
self::assertSame(3, $counter->count());
|
|
}
|
|
|
|
/** Ein zweites Objekt auf demselben Pfad muss den bereits geschriebenen Stand sehen. */
|
|
public function testCountIsSharedAcrossInstancesOnTheSamePath(): void
|
|
{
|
|
(new HookCounter($this->path))->increment();
|
|
(new HookCounter($this->path))->increment();
|
|
|
|
self::assertSame(2, (new HookCounter($this->path))->count());
|
|
}
|
|
|
|
public function testIncrementCreatesMissingDirectory(): void
|
|
{
|
|
(new HookCounter($this->path))->increment();
|
|
|
|
self::assertFileExists($this->path);
|
|
}
|
|
|
|
public function testFingerprintReflectsTheCurrentCount(): void
|
|
{
|
|
$counter = new HookCounter($this->path);
|
|
|
|
self::assertSame('0', $counter->fingerprint());
|
|
$counter->increment();
|
|
self::assertSame('1', $counter->fingerprint());
|
|
}
|
|
|
|
public function testCountResetsOnANewDay(): void
|
|
{
|
|
$day = '2026-10-01';
|
|
$counter = new HookCounter($this->path, static function () use (&$day): string {
|
|
return $day;
|
|
});
|
|
|
|
$counter->increment();
|
|
$counter->increment();
|
|
self::assertSame(2, $counter->count());
|
|
|
|
$day = '2026-10-02';
|
|
self::assertSame(0, $counter->count());
|
|
self::assertSame(1, $counter->increment());
|
|
}
|
|
|
|
public function testLegacyFileContentCountsAsZero(): void
|
|
{
|
|
mkdir(dirname($this->path), 0o775, true);
|
|
file_put_contents($this->path, '12');
|
|
|
|
self::assertSame(0, (new HookCounter($this->path))->count());
|
|
}
|
|
}
|