refactor: one logging helper LIT\MCP\Log (M6)
Log::failure(context, detail) keeps the '[wp-lit-mcp] <context> failed: <detail>' format of the nine former error_log() sites; Log::line() covers the change log line. One phpcs ignore instead of eleven. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit;
|
||||
|
||||
use LIT\MCP\Log;
|
||||
use RuntimeException;
|
||||
|
||||
final class LogTest extends TestCase
|
||||
{
|
||||
/** @return string What the callback wrote to the error log. */
|
||||
private static function captured(callable $write): string
|
||||
{
|
||||
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
|
||||
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
||||
$previous = ini_set('error_log', $log);
|
||||
|
||||
try {
|
||||
$write();
|
||||
} finally {
|
||||
ini_set('error_log', (string) $previous);
|
||||
}
|
||||
|
||||
$content = (string) file_get_contents($log);
|
||||
unlink($log);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function testFailureKeepsThePrefixedFormat(): void
|
||||
{
|
||||
$content = self::captured(static fn () => Log::failure('media usage query', 'Table gone'));
|
||||
|
||||
self::assertStringContainsString('[wp-lit-mcp] media usage query failed: Table gone', $content);
|
||||
}
|
||||
|
||||
public function testFailureLogsAThrowableWithItsTrace(): void
|
||||
{
|
||||
$e = new RuntimeException('kaputt');
|
||||
$content = self::captured(static fn () => Log::failure('lit/x', $e));
|
||||
|
||||
self::assertStringContainsString('[wp-lit-mcp] lit/x failed: RuntimeException: kaputt', $content);
|
||||
self::assertStringContainsString('Stack trace', $content);
|
||||
}
|
||||
|
||||
public function testLineOnlyAddsThePrefix(): void
|
||||
{
|
||||
$content = self::captured(static fn () => Log::line('change log insert failed for a b:1'));
|
||||
|
||||
self::assertStringContainsString('[wp-lit-mcp] change log insert failed for a b:1', $content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user