| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace Test\Michel\Log;
- use Michel\Log\Handler\StreamHandler;
- use Michel\UniTester\TestCase;
- use Psr\Log\LogLevel;
- class StreamHandlerTest extends TestCase
- {
- protected function setUp(): void
- {
- // TODO: Implement setUp() method.
- }
- protected function tearDown(): void
- {
- // TODO: Implement tearDown() method.
- }
- protected function execute(): void
- {
- $this->testWriteToStream();
- $this->testWriteToFile();
- }
- private function testWriteToStream()
- {
- $stream = fopen('php://memory', 'a+');
- $handler = new StreamHandler($stream);
- $handler->handle([
- 'level' => LogLevel::INFO,
- 'message' => 'test message',
- 'timestamp' => '2023-01-01 00:00:00'
- ]);
- rewind($stream);
- $content = stream_get_contents($stream);
- $this->assertStringContains($content, 'test message');
- $this->assertStringContains($content, '[INFO]');
- }
- private function testWriteToFile()
- {
- $file = sys_get_temp_dir() . '/test_stream_handler.log';
- if (file_exists($file)) {
- unlink($file);
- }
- $handler = new StreamHandler($file);
- $handler->handle([
- 'level' => LogLevel::ERROR,
- 'message' => 'error message',
- 'timestamp' => '2023-01-01 00:00:00'
- ]);
- $this->assertTrue(file_exists($file), 'File should exist');
- $content = file_get_contents($file);
- $this->assertStringContains($content, 'error message');
- $this->assertStringContains($content, '[ERROR]');
- unlink($file);
- }
- }
|