Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
$logger->flush(true);
}

public function testFlushIntervalZeroDoesNotFlushDuringLog(): void
{
$this->logger->setFlushInterval(0);
$this->target->setExportInterval(1);
$this->logger->log(LogLevel::INFO, 'test');

$this->assertSame(0, $this->target->getExportCount());
}

public function testSetTraceLevelWithCustomContextProvider(): void
{
$logger = new Logger(contextProvider: new StubContextProvider());
Expand Down
38 changes: 38 additions & 0 deletions tests/Message/ContextValueExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Log\Tests\Message;

use PHPUnit\Framework\TestCase;
use Yiisoft\Log\Message\ContextValueExtractor;

final class ContextValueExtractorTest extends TestCase
{
public static function dataExtract(): array
{
return [
'empty-key-found' => [['' => 'found'], '', [true, 'found']],
'empty-key-not-found' => [['foo' => 'bar'], '', [false, null]],
'simple-key' => [['foo' => 'bar'], 'foo', [true, 'bar']],
'missing-key' => [['foo' => 'bar'], 'baz', [false, null]],
'nested-key' => [['user' => ['name' => 'John']], 'user.name', [true, 'John']],
'nested-key-not-found' => [['user' => ['name' => 'John']], 'user.age', [false, null]],
'nested-key-non-array-intermediate' => [['user' => 'string'], 'user.name', [false, null]],
'escaped-dot' => [['user.name' => 'John'], 'user\.name', [true, 'John']],
'escaped-backslash' => [['user\\' => 'John'], 'user\\\\', [true, 'John']],
'deeply-nested' => [['a' => ['b' => ['c' => 'deep']]], 'a.b.c', [true, 'deep']],
'backslash-key-nested' => [['a\\' => ['b' => 'value']], 'a\\\\.b', [true, 'value']],
'multiple-backslashes-before-dot' => [['a\\\\' => ['b' => 'value']], 'a\\\\\\\\.b', [true, 'value']],
'escaped-dot-and-nesting' => [['a.b' => ['c' => 'value']], 'a\\.b.c', [true, 'value']],
];
}

/**
* @dataProvider dataExtract
*/
public function testExtract(array $context, string $key, array $expected): void
{
$this->assertSame($expected, ContextValueExtractor::extract($context, $key));
}
}
38 changes: 38 additions & 0 deletions tests/Message/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ public function testFormatWithTraceInContext(string $expectedTrace, array $trace
$this->assertSame($expected, $this->formatter->format($message, []));
}

public function testTraceWithFileWithoutLineUsesFunction(): void
{
$this->formatter->setTimestampFormat('Y-m-d H:i:s');
$message = new Message(
LogLevel::INFO,
'message',
[
'category' => 'app',
'time' => 1_508_160_390,
'trace' => [['file' => '/path/to/file', 'function' => 'myFunc']],
],
);

$result = $this->formatter->format($message, []);

$this->assertStringNotContainsString('in /path/to/file:', $result);
$this->assertStringContainsString('myFunc', $result);
}

public function testTraceWithLineWithoutFileUsesFunction(): void
{
$this->formatter->setTimestampFormat('Y-m-d H:i:s');
$message = new Message(
LogLevel::INFO,
'message',
[
'category' => 'app',
'time' => 1_508_160_390,
'trace' => [['line' => 99, 'function' => 'myFunc']],
],
);

$result = $this->formatter->format($message, []);

$this->assertStringNotContainsString(':99', $result);
$this->assertStringContainsString('myFunc', $result);
}

public function invalidCallableReturnStringProvider(): array
{
return [
Expand Down
14 changes: 14 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ public function testCategory(string $expected, array $context): void
$this->assertSame($expected, $message->category());
}

public function testTraceReturnsNullWhenNoTraceInContext(): void
{
$message = new Message(LogLevel::INFO, 'message', ['foo' => 'bar']);

$this->assertNull($message->trace());
}

public function testMessageWithoutPlaceholdersReturnsUnchanged(): void
{
$message = new Message(LogLevel::INFO, 'simple message without braces', ['foo' => 'bar']);

$this->assertSame('simple message without braces', $message->message());
}

public function testInvalidCategoryType(): void
{
$message = new Message(LogLevel::INFO, 'message', ['category' => 23.1]);
Expand Down
48 changes: 48 additions & 0 deletions tests/TargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,54 @@ public function testSetLevelsViaConstructorWithEmptyArray(): void
$this->assertCount(3, $messages);
}

public function testCollectWithNoMessagesDoesNotExport(): void
{
$this->target->collect([], true);

$this->assertSame(0, $this->target->getExportCount());
}

public function testExportIntervalZeroDoesNotExport(): void
{
$this->target->setExportInterval(0);
$this->target->collect(
[new Message(LogLevel::INFO, 'message', ['category' => 'app'])],
false,
);

$this->assertSame(0, $this->target->getExportCount());
}

public function testSetTimestampFormatAffectsNonExportedMessages(): void
{
$this->target->setTimestampFormat('Y');
$this->target->collect(
[new Message(LogLevel::INFO, 'message', ['category' => 'app', 'time' => 1_508_160_390])],
false,
);

$this->assertStringStartsWith('2017 ', $this->target->formatMessages());
}

public function testFilterContinuesAfterExcludedCategory(): void
{
$this->target->setExcept(['excluded']);
$this->target->collect(
[
new Message(LogLevel::INFO, 'first', ['category' => 'excluded']),
new Message(LogLevel::INFO, 'second', ['category' => 'allowed']),
new Message(LogLevel::INFO, 'third', ['category' => 'allowed']),
],
false,
);

$messages = $this->target->getMessages();

$this->assertCount(2, $messages);
$this->assertSame('second', $messages[0]->message());
$this->assertSame('third', $messages[1]->message());
}

private function collectOneAndExport(string $level, string $message, array $context = []): void
{
$this->target->collect([new Message($level, $message, $context)], true);
Expand Down
Loading