100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use System\Classes\FileLogReader;
|
|
use System\Classes\Log;
|
|
|
|
class FileLogReaderTest extends TestCase
|
|
{
|
|
private string $dir;
|
|
private string $today;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dir = sys_get_temp_dir() . '/bicycle_reader_' . uniqid();
|
|
$this->today = date('Y-m-d');
|
|
mkdir($this->dir);
|
|
Log::$directory = $this->dir;
|
|
|
|
file_put_contents($this->dir . '/action-' . $this->today . '.log',
|
|
"[{$this->today} 10:00:00] INFO: GET /feedback Feedback::index\n" .
|
|
"[{$this->today} 10:01:00] DEBUG: какой-то debug\n"
|
|
);
|
|
file_put_contents($this->dir . '/error-' . $this->today . '.log',
|
|
"[{$this->today} 10:02:00] ERROR: что-то сломалось\n"
|
|
);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
foreach (glob($this->dir . '/*') as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($this->dir);
|
|
Log::$directory = null;
|
|
}
|
|
|
|
public function testParsesEntries(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'action', 'date' => $this->today]);
|
|
|
|
$this->assertCount(2, $entries);
|
|
$first = $entries[0];
|
|
$this->assertArrayHasKey('datetime', $first);
|
|
$this->assertArrayHasKey('level', $first);
|
|
$this->assertArrayHasKey('message', $first);
|
|
$this->assertSame('action', $first['channel']);
|
|
}
|
|
|
|
public function testNewestFirst(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'action', 'date' => $this->today]);
|
|
// 10:01 (debug) должно быть выше 10:00 (info)
|
|
$this->assertSame('debug', $entries[0]['level']);
|
|
$this->assertSame('info', $entries[1]['level']);
|
|
}
|
|
|
|
public function testFilterByLevel(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'action', 'date' => $this->today, 'level' => 'info']);
|
|
$this->assertCount(1, $entries);
|
|
$this->assertSame('info', $entries[0]['level']);
|
|
}
|
|
|
|
public function testFilterByText(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'action', 'date' => $this->today, 'q' => 'feedback']);
|
|
$this->assertCount(1, $entries);
|
|
$this->assertStringContainsString('feedback', $entries[0]['message']);
|
|
}
|
|
|
|
public function testErrorChannel(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'error', 'date' => $this->today]);
|
|
$this->assertCount(1, $entries);
|
|
$this->assertSame('error', $entries[0]['level']);
|
|
}
|
|
|
|
public function testAllChannelsCombined(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'all', 'date' => $this->today]);
|
|
$this->assertCount(3, $entries);
|
|
// Новейшая запись — ошибка в 10:02.
|
|
$this->assertSame('error', $entries[0]['level']);
|
|
}
|
|
|
|
public function testMissingFileReturnsEmpty(): void
|
|
{
|
|
$entries = (new FileLogReader())->read(['channel' => 'action', 'date' => '2000-01-01']);
|
|
$this->assertSame([], $entries);
|
|
}
|
|
|
|
public function testDatesListsAvailable(): void
|
|
{
|
|
$dates = (new FileLogReader())->dates();
|
|
$this->assertContains($this->today, $dates);
|
|
}
|
|
}
|