Skip to content

Commit 995fc0b

Browse files
rekmixasamdarkvjik
authored
Improve mutation tests (#135) (#136)
Co-authored-by: Alexander Makarov <[email protected]> Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent 8d62394 commit 995fc0b

6 files changed

Lines changed: 77 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Enh #132: Add benchmarks, improve performance of `Message::parse()` (@samdark)
66
- Bug #130: Updated `Message::parse()` to correctly support multiple placeholders (@technicated)
77
- Chg #130, #133: Changed `Message::parse()` to conform to PSR-3 (@technicated, @vjik)
8+
- Enh #135: Add validation for `$traceLevel` in `SystemContextProvider` to ensure values are greater than or equal to zero (@rekmixa)
89
- Enh #137: Explicitly import classes, functions, and constants in "use" section (@mspirkov)
910

1011
## 2.2.0 December 13, 2025

infection.json.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
}
1212
},
1313
"mutators": {
14-
"@default": true
14+
"@default": true,
15+
"global-ignoreSourceCodeByRegex": [
16+
"register_shutdown_function"
17+
]
1518
}
1619
}

src/ContextProvider/SystemContextProvider.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(
3333
private int $traceLevel = 0,
3434
array $excludedTracePaths = [],
3535
) {
36+
$this->assertTraceLevelIsValid($this->traceLevel);
3637
/** @psalm-suppress DeprecatedMethod `setExcludedTracePaths` will be private and not deprecated */
3738
$this->setExcludedTracePaths($excludedTracePaths);
3839
}
@@ -61,7 +62,9 @@ public function getContext(): array
6162
*/
6263
public function setTraceLevel(int $traceLevel): self
6364
{
65+
$this->assertTraceLevelIsValid($traceLevel);
6466
$this->traceLevel = $traceLevel;
67+
6568
return $this;
6669
}
6770

@@ -114,7 +117,7 @@ private function collectTrace(array $backtrace): array
114117
if (isset($trace['file'], $trace['line'])) {
115118
$excludedMatch = array_filter(
116119
$this->excludedTracePaths,
117-
static fn($path) => str_contains($trace['file'], $path),
120+
static fn(string $path): bool => str_contains($trace['file'], $path),
118121
);
119122

120123
if (empty($excludedMatch)) {
@@ -129,4 +132,23 @@ private function collectTrace(array $backtrace): array
129132

130133
return $traces;
131134
}
135+
136+
/**
137+
* Validates $traceLevel property
138+
*
139+
* @param int $traceLevel The number of call stack information.
140+
*
141+
* @see self::$traceLevel
142+
*/
143+
private function assertTraceLevelIsValid(int $traceLevel): void
144+
{
145+
if ($traceLevel < 0) {
146+
throw new InvalidArgumentException(
147+
sprintf(
148+
'Trace level must be greater than or equal to zero, %s received.',
149+
$traceLevel,
150+
),
151+
);
152+
}
153+
}
132154
}

src/Logger.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use RuntimeException;
1212
use Stringable;
1313
use Throwable;
14-
use Yiisoft\Log\ContextProvider\SystemContextProvider;
1514
use Yiisoft\Log\ContextProvider\ContextProviderInterface;
15+
use Yiisoft\Log\ContextProvider\SystemContextProvider;
1616

1717
use function count;
1818
use function implode;
@@ -105,24 +105,13 @@ public function __construct(
105105
* @throws \Psr\Log\InvalidArgumentException for invalid log message level.
106106
*
107107
* @return string The text display of the level.
108-
* @deprecated since 2.1, to be removed in 3.0. Use {@see LogLevel::assertLevelIsValid()} instead.
108+
* @deprecated since 2.1, to be removed in 3.0. Use {@see Logger::assertLevelIsValid()} instead.
109+
* @psalm-suppress MixedInferredReturnType
110+
* @psalm-suppress MixedReturnStatement
109111
*/
110112
public static function validateLevel(mixed $level): string
111113
{
112-
if (!is_string($level)) {
113-
throw new \Psr\Log\InvalidArgumentException(sprintf(
114-
'The log message level must be a string, %s provided.',
115-
get_debug_type($level),
116-
));
117-
}
118-
119-
if (!in_array($level, self::LEVELS, true)) {
120-
throw new \Psr\Log\InvalidArgumentException(sprintf(
121-
'Invalid log message level "%s" provided. The following values are supported: "%s".',
122-
$level,
123-
implode('", "', self::LEVELS),
124-
));
125-
}
114+
self::assertLevelIsValid($level);
126115

127116
return $level;
128117
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Log\Tests\ContextProvider;
6+
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\TestCase;
9+
use Yiisoft\Log\ContextProvider\SystemContextProvider;
10+
11+
final class SystemContextProviderTest extends TestCase
12+
{
13+
public function testWrongTraceLevel(): void
14+
{
15+
$this->expectException(InvalidArgumentException::class);
16+
new SystemContextProvider(-1);
17+
}
18+
19+
public function testContextHasNeededData(): void
20+
{
21+
$provider = new SystemContextProvider();
22+
$context = $provider->getContext();
23+
24+
$this->assertArrayHasKey('time', $context);
25+
$this->assertArrayHasKey('trace', $context);
26+
$this->assertArrayHasKey('memory', $context);
27+
$this->assertArrayHasKey('category', $context);
28+
}
29+
}

tests/LoggerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,24 @@ public function testLog(): void
5656
$this->assertGreaterThanOrEqual($memory, $messages[1]->context('memory'));
5757
}
5858

59+
public function testLogWithWrongLevel(): void
60+
{
61+
$this->expectException(\Psr\Log\InvalidArgumentException::class);
62+
$this->logger->log(123, 'test1');
63+
}
64+
65+
public function testLogWithUnsupportedLevel(): void
66+
{
67+
$this->expectException(\Psr\Log\InvalidArgumentException::class);
68+
$this->logger->log('unsupported-level', 'test1');
69+
}
70+
5971
public function testLogWithTraceLevel(): void
6072
{
6173
$memory = memory_get_usage();
6274
$this->logger->setTraceLevel($traceLevel = 3);
6375

76+
$line = __LINE__;
6477
$this->logger->log(LogLevel::INFO, 'test3');
6578
$messages = $this->getInaccessibleMessages($this->logger);
6679

@@ -70,7 +83,7 @@ public function testLogWithTraceLevel(): void
7083
$this->assertSame('application', $messages[0]->context('category'));
7184
$this->assertSame([
7285
'file' => __FILE__,
73-
'line' => 64,
86+
'line' => $line + 1,
7487
'function' => 'log',
7588
'class' => Logger::class,
7689
'type' => '->',
@@ -145,6 +158,7 @@ public function testSetExcludedTracePaths(): void
145158
$this->logger->info('info message');
146159
$messages = $this->getInaccessibleMessages($this->logger);
147160

161+
$this->assertNotEmpty($messages[1]->context('trace'));
148162
foreach ($messages[1]->context('trace') as $trace) {
149163
$this->assertNotSame(__FILE__, $trace['file']);
150164
}

0 commit comments

Comments
 (0)