Skip to content

Commit 1d415af

Browse files
WarLikeLauxvjik
andauthored
Strengthen test coverage and mutation score (#143)
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
1 parent e123bff commit 1d415af

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

tests/LoggerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
419419
$logger->flush(true);
420420
}
421421

422+
public function testFlushIntervalZeroDoesNotFlushDuringLog(): void
423+
{
424+
$this->logger->setFlushInterval(0);
425+
$this->target->setExportInterval(1);
426+
$this->logger->log(LogLevel::INFO, 'test');
427+
428+
$this->assertSame(0, $this->target->getExportCount());
429+
}
430+
422431
public function testSetTraceLevelWithCustomContextProvider(): void
423432
{
424433
$logger = new Logger(contextProvider: new StubContextProvider());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Log\Tests\Message;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Log\Message\ContextValueExtractor;
9+
10+
final class ContextValueExtractorTest extends TestCase
11+
{
12+
public static function dataExtract(): array
13+
{
14+
return [
15+
'empty-key-found' => [['' => 'found'], '', [true, 'found']],
16+
'empty-key-not-found' => [['foo' => 'bar'], '', [false, null]],
17+
'simple-key' => [['foo' => 'bar'], 'foo', [true, 'bar']],
18+
'missing-key' => [['foo' => 'bar'], 'baz', [false, null]],
19+
'nested-key' => [['user' => ['name' => 'John']], 'user.name', [true, 'John']],
20+
'nested-key-not-found' => [['user' => ['name' => 'John']], 'user.age', [false, null]],
21+
'nested-key-non-array-intermediate' => [['user' => 'string'], 'user.name', [false, null]],
22+
'escaped-dot' => [['user.name' => 'John'], 'user\.name', [true, 'John']],
23+
'escaped-backslash' => [['user\\' => 'John'], 'user\\\\', [true, 'John']],
24+
'deeply-nested' => [['a' => ['b' => ['c' => 'deep']]], 'a.b.c', [true, 'deep']],
25+
'backslash-key-nested' => [['a\\' => ['b' => 'value']], 'a\\\\.b', [true, 'value']],
26+
'multiple-backslashes-before-dot' => [['a\\\\' => ['b' => 'value']], 'a\\\\\\\\.b', [true, 'value']],
27+
'escaped-dot-and-nesting' => [['a.b' => ['c' => 'value']], 'a\\.b.c', [true, 'value']],
28+
];
29+
}
30+
31+
/**
32+
* @dataProvider dataExtract
33+
*/
34+
public function testExtract(array $context, string $key, array $expected): void
35+
{
36+
$this->assertSame($expected, ContextValueExtractor::extract($context, $key));
37+
}
38+
}

tests/Message/FormatterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,44 @@ public function testFormatWithTraceInContext(string $expectedTrace, array $trace
217217
$this->assertSame($expected, $this->formatter->format($message, []));
218218
}
219219

220+
public function testTraceWithFileWithoutLineUsesFunction(): void
221+
{
222+
$this->formatter->setTimestampFormat('Y-m-d H:i:s');
223+
$message = new Message(
224+
LogLevel::INFO,
225+
'message',
226+
[
227+
'category' => 'app',
228+
'time' => 1_508_160_390,
229+
'trace' => [['file' => '/path/to/file', 'function' => 'myFunc']],
230+
],
231+
);
232+
233+
$result = $this->formatter->format($message, []);
234+
235+
$this->assertStringNotContainsString('in /path/to/file:', $result);
236+
$this->assertStringContainsString('myFunc', $result);
237+
}
238+
239+
public function testTraceWithLineWithoutFileUsesFunction(): void
240+
{
241+
$this->formatter->setTimestampFormat('Y-m-d H:i:s');
242+
$message = new Message(
243+
LogLevel::INFO,
244+
'message',
245+
[
246+
'category' => 'app',
247+
'time' => 1_508_160_390,
248+
'trace' => [['line' => 99, 'function' => 'myFunc']],
249+
],
250+
);
251+
252+
$result = $this->formatter->format($message, []);
253+
254+
$this->assertStringNotContainsString(':99', $result);
255+
$this->assertStringContainsString('myFunc', $result);
256+
}
257+
220258
public function invalidCallableReturnStringProvider(): array
221259
{
222260
return [

tests/MessageTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ public function testCategory(string $expected, array $context): void
192192
$this->assertSame($expected, $message->category());
193193
}
194194

195+
public function testTraceReturnsNullWhenNoTraceInContext(): void
196+
{
197+
$message = new Message(LogLevel::INFO, 'message', ['foo' => 'bar']);
198+
199+
$this->assertNull($message->trace());
200+
}
201+
202+
public function testMessageWithoutPlaceholdersReturnsUnchanged(): void
203+
{
204+
$message = new Message(LogLevel::INFO, 'simple message without braces', ['foo' => 'bar']);
205+
206+
$this->assertSame('simple message without braces', $message->message());
207+
}
208+
195209
public function testInvalidCategoryType(): void
196210
{
197211
$message = new Message(LogLevel::INFO, 'message', ['category' => 23.1]);

tests/TargetTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,54 @@ public function testSetLevelsViaConstructorWithEmptyArray(): void
495495
$this->assertCount(3, $messages);
496496
}
497497

498+
public function testCollectWithNoMessagesDoesNotExport(): void
499+
{
500+
$this->target->collect([], true);
501+
502+
$this->assertSame(0, $this->target->getExportCount());
503+
}
504+
505+
public function testExportIntervalZeroDoesNotExport(): void
506+
{
507+
$this->target->setExportInterval(0);
508+
$this->target->collect(
509+
[new Message(LogLevel::INFO, 'message', ['category' => 'app'])],
510+
false,
511+
);
512+
513+
$this->assertSame(0, $this->target->getExportCount());
514+
}
515+
516+
public function testSetTimestampFormatAffectsNonExportedMessages(): void
517+
{
518+
$this->target->setTimestampFormat('Y');
519+
$this->target->collect(
520+
[new Message(LogLevel::INFO, 'message', ['category' => 'app', 'time' => 1_508_160_390])],
521+
false,
522+
);
523+
524+
$this->assertStringStartsWith('2017 ', $this->target->formatMessages());
525+
}
526+
527+
public function testFilterContinuesAfterExcludedCategory(): void
528+
{
529+
$this->target->setExcept(['excluded']);
530+
$this->target->collect(
531+
[
532+
new Message(LogLevel::INFO, 'first', ['category' => 'excluded']),
533+
new Message(LogLevel::INFO, 'second', ['category' => 'allowed']),
534+
new Message(LogLevel::INFO, 'third', ['category' => 'allowed']),
535+
],
536+
false,
537+
);
538+
539+
$messages = $this->target->getMessages();
540+
541+
$this->assertCount(2, $messages);
542+
$this->assertSame('second', $messages[0]->message());
543+
$this->assertSame('third', $messages[1]->message());
544+
}
545+
498546
private function collectOneAndExport(string $level, string $message, array $context = []): void
499547
{
500548
$this->target->collect([new Message($level, $message, $context)], true);

0 commit comments

Comments
 (0)