Bicycle/tests/Unit/ProfilerTest.php
Egor Isaev 9d4edf147a dev
2026-08-07 11:40:03 +03:00

61 lines
1.8 KiB
PHP

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Services\DataBase\Classes\Profiler;
use Services\DataBase\Classes\ProfilerPDO;
class ProfilerTest extends TestCase
{
protected function setUp(): void
{
// Другие тесты (PdoDriverTest/StatementTest/RepositoryTest) пишут в этот же
// статический реестр, если есть живое подключение к БД — сбрасываем перед своими тестами.
Profiler::reset();
}
protected function tearDown(): void
{
Profiler::reset();
}
public function testLogAddsEntry(): void
{
Profiler::log('SELECT 1', [], 1.5);
$entries = Profiler::entries();
$this->assertCount(1, $entries);
$this->assertSame('SELECT 1', $entries[0]['sql']);
$this->assertSame(1.5, $entries[0]['time_ms']);
$this->assertSame('pdo', $entries[0]['driver']);
}
public function testCountAndTotalTime(): void
{
Profiler::log('SELECT 1', [], 1.0);
Profiler::log('SELECT 2', [], 2.5);
$this->assertSame(2, Profiler::count());
$this->assertSame(3.5, Profiler::totalTime());
}
public function testResetClearsEntries(): void
{
Profiler::log('SELECT 1', [], 1.0);
Profiler::reset();
$this->assertSame(0, Profiler::count());
$this->assertSame([], Profiler::entries());
}
public function testProfilerPdoWrapLogsAndReturnsResult(): void
{
$result = ProfilerPDO::wrap('SELECT 1', [], static fn () => 'result');
$this->assertSame('result', $result);
$this->assertCount(1, Profiler::entries());
$this->assertGreaterThanOrEqual(0, Profiler::entries()[0]['time_ms']);
}
}