-
-
Notifications
You must be signed in to change notification settings - Fork 20
Description
Improving mutation tests
Hello Yii framework team!
It would be a once if my improvements in mutation tests were accepted.
Below is a list of improvements with explanations and a link to the PR
- log\src\ContextProvider\SystemContextProvider.php:28 [M] DecrementInteger
--- Original
+++ New
@@ @@
* @param string[] $excludedTracePaths Array of paths to exclude from tracing when tracing is enabled
* with {@see $traceLevel}.
*/
- public function __construct(private int $traceLevel = 0, array $excludedTracePaths = [])
+ public function __construct(private int $traceLevel = -1, array $excludedTracePaths = [])
{
/** @psalm-suppress DeprecatedMethod `setExcludedTracePaths` will be private and not deprecated */
$this->setExcludedTracePaths($excludedTracePaths);
@param int $traceLevel How much call stack information (file name and line number) should be logged for each
I think values less than zero shouldn't be allowed, so I added an InvalidArgumentException exception throw in the constructor: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/src/ContextProvider/SystemContextProvider.php#L32
- log\src\ContextProvider\SystemContextProvider.php:50 [M] ArrayItem
--- Original
+++ New
@@ @@
/** @psalm-var list<TraceItem> $trace */
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace);
- return ['time' => microtime(true), 'trace' => $this->collectTrace($trace), 'memory' => memory_get_usage(), 'category' => Message::DEFAULT_CATEGORY];
+ return ['time' > microtime(true), 'trace' => $this->collectTrace($trace), 'memory' => memory_get_usage(), 'category' => Message::DEFAULT_CATEGORY];
}
/**
* Sets how much call stack information (file name and line number) should be logged for each log message.
I think this mutation can be covered with such (maybe little bit stupid) tests: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/tests/ContextProvider/SystemContextProviderTest.php#L19
- log\src\ContextProvider\SystemContextProvider.php:119 [M] UnwrapArrayFilter
--- Original
+++ New
@@ @@
$count = 0;
foreach ($backtrace as $trace) {
if (isset($trace['file'], $trace['line'])) {
- $excludedMatch = array_filter($this->excludedTracePaths, static fn(string $path): bool => str_contains($trace['file'], $path));
+ $excludedMatch = $this->excludedTracePaths;
if (empty($excludedMatch)) {
$traces[] = $trace;
if (++$count >= $this->traceLevel) {
That mutation would be covered with this changes in testSetExcludedTracePaths: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/tests/LoggerTest.php#L159
- log\src\Logger.php:91 [M] FunctionCallRemoval
--- Original
+++ New
@@ @@
{
$this->setTargets($targets);
$this->contextProvider = $contextProvider ?? new SystemContextProvider();
- register_shutdown_function(function () {
- // make regular flush before other shutdown functions, which allows session data collection and so on
- $this->flush();
- // make sure log entries written by shutdown functions are also flushed
- // ensure "flush()" is called last when there are multiple shutdown functions
- register_shutdown_function([$this, 'flush'], true);
- });
+
}
/**
* Returns the text display of the specified level.
Unfortunately, this mutation can't be covered because there's no shutdown when running tests. Therefore, I propose adding this function to the exceptions: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/infection.json.dist#L15
"mutators": {
"@default": true,
"global-ignoreSourceCodeByRegex": [
"register_shutdown_function"
]
}- log\src\Logger.php:140 [M] MethodCallRemoval
--- Original
+++ New
@@ @@
}
public function log(mixed $level, string|Stringable $message, array $context = []) : void
{
- self::assertLevelIsString($level);
+
$this->messages[] = new Message($level, $message, array_merge($this->contextProvider->getContext(), $context));
if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) {
$this->flush();
That mutation was covered in this test: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/tests/LoggerTest.php#L57
- log\src\Logger.php:233 [M] MethodCallRemoval
--- Original
+++ New
@@ @@
public static function assertLevelIsValid(mixed $level) : void
{
self::assertLevelIsString($level);
- self::assertLevelIsSupported($level);
+
}
/**
* Asserts that the log message level is a string.
This mutation was covered with this test: https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/tests/LoggerTest.php#L63
and removing this duplicated code from Logger::validateLevel method (which is deprecated): https://github.com/rekmixa/log/blob/d3c43debdb07cd6caaf62c71331406b3bc458667/src/Logger.php#L109