1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-13 00:24:10 +02:00

Fix some issues, bump phpunit version

This commit is contained in:
Jordi Boggiano
2022-02-26 15:09:41 +01:00
parent 22c8b19358
commit 400effdd45
45 changed files with 602 additions and 774 deletions

View File

@@ -12,8 +12,9 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Test\TestCase;
class ChromePHPFormatterTest extends \PHPUnit\Framework\TestCase
class ChromePHPFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\ChromePHPFormatter::format
@@ -21,15 +22,14 @@ class ChromePHPFormatterTest extends \PHPUnit\Framework\TestCase
public function testDefaultFormat()
{
$formatter = new ChromePHPFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['ip' => '127.0.0.1'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['ip' => '127.0.0.1'],
);
$message = $formatter->format($record);
@@ -54,15 +54,14 @@ class ChromePHPFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatWithFileAndLine()
{
$formatter = new ChromePHPFormatter();
$record = [
'level' => Logger::CRITICAL,
'level_name' => 'CRITICAL',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
'message' => 'log',
];
$record = $this->getRecord(
Logger::CRITICAL,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
);
$message = $formatter->format($record);
@@ -87,15 +86,12 @@ class ChromePHPFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatWithoutContext()
{
$formatter = new ChromePHPFormatter();
$record = [
'level' => Logger::DEBUG,
'level_name' => 'DEBUG',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$record = $this->getRecord(
Logger::DEBUG,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
);
$message = $formatter->format($record);
@@ -117,24 +113,18 @@ class ChromePHPFormatterTest extends \PHPUnit\Framework\TestCase
{
$formatter = new ChromePHPFormatter();
$records = [
[
'level' => Logger::INFO,
'level_name' => 'INFO',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
],
[
'level' => Logger::WARNING,
'level_name' => 'WARNING',
'channel' => 'foo',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log2',
],
$this->getRecord(
Logger::INFO,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
),
$this->getRecord(
Logger::WARNING,
'log2',
channel: 'foo',
datetime: new \DateTimeImmutable("@0"),
),
];
$this->assertEquals(

View File

@@ -13,8 +13,9 @@ namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Test\TestCase;
class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
class ElasticaFormatterTest extends TestCase
{
public function setUp(): void
{
@@ -31,18 +32,16 @@ class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormat()
{
// test log message
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
$msg = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
message: 'log',
);
// expected values
$expected = (array) $msg;
unset($expected['formatted']);
$expected = $msg->toArray();
$expected['datetime'] = '1970-01-01T00:00:00.000000+00:00';
$expected['context'] = [
'class' => ['stdClass' => []],

View File

@@ -12,8 +12,9 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Test\TestCase;
class ElasticsearchFormatterTest extends \PHPUnit\Framework\TestCase
class ElasticsearchFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\ElasticsearchFormatter::__construct
@@ -23,18 +24,16 @@ class ElasticsearchFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormat()
{
// Test log message
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$msg = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
);
// Expected values
$expected = $msg;
$expected = $msg->toArray();
$expected['datetime'] = '1970-01-01T00:00:00+0000';
$expected['context'] = [
'class' => ['stdClass' => []],

View File

@@ -34,7 +34,7 @@ class FlowdockFormatterTest extends TestCase
];
$formatted = $formatter->format($record);
$this->assertEquals($expected, $formatted['flowdock']);
$this->assertEquals($expected, $formatted);
}
/**
@@ -49,7 +49,7 @@ class FlowdockFormatterTest extends TestCase
];
$formatted = $formatter->formatBatch($records);
$this->assertArrayHasKey('flowdock', $formatted[0]);
$this->assertArrayHasKey('flowdock', $formatted[1]);
$this->assertArrayHasKey('from_address', $formatted[0]);
$this->assertArrayHasKey('from_address', $formatted[1]);
}
}

View File

@@ -35,8 +35,7 @@ class FluentdFormatterTest extends TestCase
*/
public function testFormat()
{
$record = $this->getRecord(Logger::WARNING);
$record['datetime'] = new \DateTimeImmutable("@0");
$record = $this->getRecord(Logger::WARNING, datetime: new \DateTimeImmutable("@0"));
$formatter = new FluentdFormatter();
$this->assertEquals(
@@ -50,8 +49,7 @@ class FluentdFormatterTest extends TestCase
*/
public function testFormatWithTag()
{
$record = $this->getRecord(Logger::ERROR);
$record['datetime'] = new \DateTimeImmutable("@0");
$record = $this->getRecord(Logger::ERROR, datetime: new \DateTimeImmutable("@0"));
$formatter = new FluentdFormatter(true);
$this->assertEquals(

View File

@@ -12,7 +12,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Monolog\Test\TestCase;
class GelfMessageFormatterTest extends TestCase
{
@@ -29,15 +29,12 @@ class GelfMessageFormatterTest extends TestCase
public function testDefaultFormatter()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
);
$message = $formatter->format($record);
@@ -64,15 +61,14 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithFileAndLine()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['file' => 'test', 'line' => 14],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['file' => 'test', 'line' => 14],
);
$message = $formatter->format($record);
@@ -81,37 +77,20 @@ class GelfMessageFormatterTest extends TestCase
$this->assertEquals(14, $message->getLine());
}
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testFormatInvalidFails()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
];
$this->expectException(\InvalidArgumentException::class);
$formatter->format($record);
}
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testFormatWithContext()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => 'pair'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => 'pair'],
);
$message = $formatter->format($record);
@@ -140,19 +119,17 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithContextContainingException()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger', 'exception' => [
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger', 'exception' => [
'class' => '\Exception',
'file' => '/some/file/in/dir.php:56',
'trace' => ['/some/file/1.php:23', '/some/file/2.php:3'],
]],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
datetime: new \DateTimeImmutable("@0"),
);
$message = $formatter->format($record);
@@ -168,15 +145,14 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithExtra()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => 'pair'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => 'pair'],
);
$message = $formatter->format($record);
@@ -202,15 +178,14 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithLargeData()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['exception' => str_repeat(' ', 32767)],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => str_repeat(' ', 32767)],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['exception' => str_repeat(' ', 32767)],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => str_repeat(' ', 32767)],
);
$message = $formatter->format($record);
$messageArray = $message->toArray();
@@ -229,14 +204,13 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithUnlimitedLength()
{
$formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX);
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('exception' => str_repeat(' ', 32767 * 2)),
'datetime' => new \DateTime("@0"),
'extra' => array('key' => str_repeat(' ', 32767 * 2)),
'message' => 'log',
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['exception' => str_repeat(' ', 32767 * 2)],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => str_repeat(' ', 32767 * 2)],
);
$message = $formatter->format($record);
$messageArray = $message->toArray();
@@ -256,15 +230,14 @@ class GelfMessageFormatterTest extends TestCase
public function testFormatWithLargeCyrillicData()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['exception' => str_repeat('а', 32767)],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => str_repeat('б', 32767)],
'message' => str_repeat('в', 32767),
];
$record = $this->getRecord(
Logger::ERROR,
str_repeat('в', 32767),
channel: 'meh',
context: ['exception' => str_repeat('а', 32767)],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => str_repeat('б', 32767)],
);
$message = $formatter->format($record);
$messageArray = $message->toArray();

View File

@@ -12,6 +12,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Test\TestCase;
class JsonFormatterTest extends TestCase
@@ -38,8 +39,7 @@ class JsonFormatterTest extends TestCase
{
$formatter = new JsonFormatter();
$record = $this->getRecord();
$record['context'] = $record['extra'] = new \stdClass;
$this->assertEquals(json_encode($record)."\n", $formatter->format($record));
$this->assertEquals(json_encode($record->toArray(), JSON_FORCE_OBJECT)."\n", $formatter->format($record));
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
$record = $this->getRecord();
@@ -54,8 +54,7 @@ class JsonFormatterTest extends TestCase
$formatter = new JsonFormatter();
$formatter->setJsonPrettyPrint(true);
$record = $this->getRecord();
$record['context'] = $record['extra'] = new \stdClass;
$this->assertEquals(json_encode($record, JSON_PRETTY_PRINT)."\n", $formatter->format($record));
$this->assertEquals(json_encode($record->toArray(), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT)."\n", $formatter->format($record));
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
$formatter->setJsonPrettyPrint(true);
@@ -99,14 +98,11 @@ class JsonFormatterTest extends TestCase
public function testFormatBatchNewlines()
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
$records = $expected = [
$records = [
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
];
array_walk($expected, function (&$value, $key) {
$value['context'] = $value['extra'] = new \stdClass;
$value = json_encode($value);
});
$expected = array_map(fn (LogRecord $record) => json_encode($record->toArray(), JSON_FORCE_OBJECT), $records);
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
}
@@ -165,7 +161,7 @@ class JsonFormatterTest extends TestCase
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
'{"...":"Over 0 items (6 total), aborting normalization"}'."\n",
'{"...":"Over 0 items (7 total), aborting normalization"}'."\n",
$message
);
}
@@ -180,7 +176,7 @@ class JsonFormatterTest extends TestCase
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
'{"level_name":"CRITICAL","channel":"core","...":"Over 2 items (6 total), aborting normalization"}'."\n",
'{"message":"foobar","context":{"exception":{"class":"Error","message":"Foo","code":0,"file":"'.__FILE__.':'.(__LINE__ - 5).'"}},"...":"Over 2 items (7 total), aborting normalization"}'."\n",
$message
);
}
@@ -188,8 +184,9 @@ class JsonFormatterTest extends TestCase
public function testDefFormatWithResource()
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
$record = $this->getRecord();
$record['context'] = ['field_resource' => opendir(__DIR__)];
$record = $this->getRecord(
context: ['field_resource' => opendir(__DIR__)],
);
$this->assertEquals('{"message":"test","context":{"field_resource":"[resource(stream)]"},"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record['datetime']->format('Y-m-d\TH:i:s.uP').'","extra":{}}', $formatter->format($record));
}
@@ -202,7 +199,7 @@ class JsonFormatterTest extends TestCase
private function assertContextContainsFormattedException($expected, $actual)
{
$this->assertEquals(
'{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":{},"message":"foobar"}'."\n",
'{"message":"foobar","context":{"exception":'.$expected.'},"level":500,"level_name":"CRITICAL","channel":"core","datetime":"2022-02-22T00:00:00+00:00","extra":{}}'."\n",
$actual
);
}
@@ -215,14 +212,13 @@ class JsonFormatterTest extends TestCase
*/
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => $exception],
'datetime' => null,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => $exception],
datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),
));
return $message;
}
@@ -265,13 +261,11 @@ class JsonFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$largeArray = range(1, 1000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
'bar',
channel: 'test',
context: array($largeArray),
));
$this->assertCount(1000, $res['context'][0]);
@@ -283,13 +277,11 @@ class JsonFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$largeArray = range(1, 2000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
'bar',
channel: 'test',
context: array($largeArray),
));
$this->assertCount(1001, $res['context'][0]);
@@ -300,17 +292,15 @@ class JsonFormatterTest extends TestCase
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
$record = $formatter->format(array(
'level' => 100,
'level_name' => 'DEBUG',
'channel' => 'test',
'message' => 'Testing',
'context' => array(),
'extra' => array(),
$record = $formatter->format($this->getRecord(
Logger::DEBUG,
'Testing',
channel: 'test',
datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),
));
$this->assertSame(
'{"level":100,"level_name":"DEBUG","channel":"test","message":"Testing"}'."\n",
'{"message":"Testing","level":100,"level_name":"DEBUG","channel":"test","datetime":"2022-02-22T00:00:00+00:00"}'."\n",
$record
);
}

View File

@@ -11,111 +11,101 @@
namespace Monolog\Formatter;
use Monolog\Test\TestCase;
use Monolog\Logger;
/**
* @covers Monolog\Formatter\LineFormatter
*/
class LineFormatterTest extends \PHPUnit\Framework\TestCase
class LineFormatterTest extends TestCase
{
public function testDefFormatWithString()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'WARNING',
'channel' => 'log',
'context' => [],
'message' => 'foo',
'datetime' => new \DateTimeImmutable,
'extra' => [],
]);
$message = $formatter->format($this->getRecord(
Logger::WARNING,
'foo',
channel: 'log',
));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
}
public function testDefFormatWithArrayContext()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'message' => 'foo',
'datetime' => new \DateTimeImmutable,
'extra' => [],
'context' => [
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'foo',
channel: 'meh',
context: [
'foo' => 'bar',
'baz' => 'qux',
'bool' => false,
'null' => null,
],
]);
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
}
public function testDefFormatExtras()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => ['ip' => '127.0.0.1'],
'message' => 'log',
]);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
extra: ['ip' => '127.0.0.1'],
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
}
public function testFormatExtras()
{
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => ['ip' => '127.0.0.1', 'file' => 'test'],
'message' => 'log',
]);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
extra: ['ip' => '127.0.0.1', 'file' => 'test'],
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
}
public function testContextAndExtraOptionallyNotShownIfEmpty()
{
$formatter = new LineFormatter(null, 'Y-m-d', false, true);
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'log',
]);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
}
public function testContextAndExtraReplacement()
{
$formatter = new LineFormatter('%context.foo% => %extra.foo%');
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 'bar'],
'datetime' => new \DateTimeImmutable,
'extra' => ['foo' => 'xbar'],
'message' => 'log',
]);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['foo' => 'bar'],
extra: ['foo' => 'xbar'],
));
$this->assertEquals('bar => xbar', $message);
}
public function testDefFormatWithObject()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => ['foo' => new TestFoo, 'bar' => new TestBar, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
'foobar',
channel: 'meh',
context: [],
extra: ['foo' => new TestFoo, 'bar' => new TestBar, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":{"Monolog\\\\Formatter\\\\TestFoo":{"foo":"fooValue"}},"bar":{"Monolog\\\\Formatter\\\\TestBar":"bar"},"baz":[],"res":"[resource(stream)]"}'."\n", $message);
}
@@ -123,54 +113,48 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
public function testDefFormatWithException()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => new \RuntimeException('Foo')],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo')],
));
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
}
public function testDefFormatWithExceptionAndStacktrace()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->includeStacktraces();
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => new \RuntimeException('Foo')],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo')],
));
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertMatchesRegularExpression('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 8).'\)\n\[stacktrace]\n#0}', $message);
$this->assertMatchesRegularExpression('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 5).'\)\n\[stacktrace]\n#0}', $message);
}
public function testDefFormatWithPreviousException()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$previous = new \LogicException('Wut?');
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => new \RuntimeException('Foo', 0, $previous)],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo', 0, $previous)],
));
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')\n[previous exception] [object] (LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 12).')"} []'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 5).')\n[previous exception] [object] (LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 10).')"} []'."\n", $message);
}
public function testDefFormatWithSoapFaultException()
@@ -180,53 +164,43 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
}
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => new \SoapFault('foo', 'bar', 'hello', 'world')],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => new \SoapFault('foo', 'bar', 'hello', 'world')],
));
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: world): bar at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: world): bar at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world'])],
'datetime' => new \DateTimeImmutable,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world'])],
));
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: {\"bar\":{\"biz\":\"baz\"},\"foo\":\"world\"}): bar at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: {\"bar\":{\"biz\":\"baz\"},\"foo\":\"world\"}): bar at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
}
public function testBatchFormat()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->formatBatch([
[
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => [],
],
[
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => [],
],
$this->getRecord(
Logger::CRITICAL,
'bar',
channel: 'test',
),
$this->getRecord(
Logger::WARNING,
'foo',
channel: 'log',
),
]);
$this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
}
@@ -234,13 +208,7 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatShouldStripInlineLineBreaks()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format(
[
'message' => "foo\nbar",
'context' => [],
'extra' => [],
]
);
$message = $formatter->format($this->getRecord(message: "foo\nbar"));
$this->assertMatchesRegularExpression('/foo bar/', $message);
}
@@ -248,13 +216,7 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
{
$formatter = new LineFormatter(null, 'Y-m-d', true);
$message = $formatter->format(
[
'message' => "foo\nbar",
'context' => [],
'extra' => [],
]
);
$message = $formatter->format($this->getRecord(message: "foo\nbar"));
$this->assertMatchesRegularExpression('/foo\nbar/', $message);
}

View File

@@ -12,7 +12,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Monolog\Test\TestCase;
class LogstashFormatterTest extends TestCase
{
@@ -22,15 +22,12 @@ class LogstashFormatterTest extends TestCase
public function testDefaultFormatterV1()
{
$formatter = new LogstashFormatter('test', 'hostname');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
);
$message = json_decode($formatter->format($record), true);
@@ -56,15 +53,14 @@ class LogstashFormatterTest extends TestCase
public function testFormatWithFileAndLineV1()
{
$formatter = new LogstashFormatter('test');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['file' => 'test', 'line' => 14],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['file' => 'test', 'line' => 14],
);
$message = json_decode($formatter->format($record), true);
@@ -78,15 +74,14 @@ class LogstashFormatterTest extends TestCase
public function testFormatWithContextV1()
{
$formatter = new LogstashFormatter('test');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => 'pair'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => 'pair'],
);
$message = json_decode($formatter->format($record), true);
@@ -109,15 +104,14 @@ class LogstashFormatterTest extends TestCase
public function testFormatWithExtraV1()
{
$formatter = new LogstashFormatter('test');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => 'pair'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => 'pair'],
);
$message = json_decode($formatter->format($record), true);
@@ -137,15 +131,14 @@ class LogstashFormatterTest extends TestCase
public function testFormatWithApplicationNameV1()
{
$formatter = new LogstashFormatter('app', 'test');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => 'pair'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
datetime: new \DateTimeImmutable("@0"),
extra: ['key' => 'pair'],
);
$message = json_decode($formatter->format($record), true);
@@ -156,17 +149,15 @@ class LogstashFormatterTest extends TestCase
public function testFormatWithLatin9Data()
{
$formatter = new LogstashFormatter('test', 'hostname');
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => '¯\_(ツ)_/¯',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: '¯\_(ツ)_/¯',
datetime: new \DateTimeImmutable("@0"),
extra: [
'user_agent' => "\xD6WN; FBCR/OrangeEspa\xF1a; Vers\xE3o/4.0; F\xE4rist",
],
'message' => 'log',
];
);
$message = json_decode($formatter->format($record), true);

View File

@@ -15,11 +15,12 @@ use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;
use Monolog\Logger;
use Monolog\Test\TestCase;
/**
* @author Florian Plattner <me@florianplattner.de>
*/
class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
class MongoDBFormatterTest extends TestCase
{
public function setUp(): void
{
@@ -59,15 +60,12 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
public function testSimpleFormat()
{
$record = [
'message' => 'some log message',
'context' => [],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
'extra' => [],
];
$record = $this->getRecord(
message: 'some log message',
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
$formatter = new MongoDBFormatter();
$formattedRecord = $formatter->format($record);
@@ -89,21 +87,19 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
$someObject->foo = 'something';
$someObject->bar = 'stuff';
$record = [
'message' => 'some log message',
'context' => [
$record = $this->getRecord(
message: 'some log message',
context: [
'stuff' => new \DateTimeImmutable('1969-01-21T21:11:30.213000+00:00'),
'some_object' => $someObject,
'context_string' => 'some string',
'context_int' => 123456,
'except' => new \Exception('exception message', 987),
],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.213000+00:00'),
'extra' => [],
];
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.213000+00:00'),
);
$formatter = new MongoDBFormatter();
$formattedRecord = $formatter->format($record);
@@ -133,9 +129,9 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatDepthArray()
{
$record = [
'message' => 'some log message',
'context' => [
$record = $this->getRecord(
message: 'some log message',
context: [
'nest2' => [
'property' => 'anything',
'nest3' => [
@@ -144,12 +140,10 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
],
],
],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
'extra' => [],
];
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
$formatter = new MongoDBFormatter(2);
$formattedResult = $formatter->format($record);
@@ -167,9 +161,9 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatDepthArrayInfiniteNesting()
{
$record = [
'message' => 'some log message',
'context' => [
$record = $this->getRecord(
message: 'some log message',
context: [
'nest2' => [
'property' => 'something',
'nest3' => [
@@ -180,12 +174,10 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
],
],
],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
'extra' => [],
];
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
$formatter = new MongoDBFormatter(0);
$formattedResult = $formatter->format($record);
@@ -214,17 +206,15 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
$someObject->nest3->property = 'nothing';
$someObject->nest3->nest4 = 'invisible';
$record = [
'message' => 'some log message',
'context' => [
$record = $this->getRecord(
message: 'some log message',
context: [
'nest2' => $someObject,
],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
'extra' => [],
];
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
$formatter = new MongoDBFormatter(2, true);
$formattedResult = $formatter->format($record);
@@ -243,17 +233,15 @@ class MongoDBFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatDepthException()
{
$record = [
'message' => 'some log message',
'context' => [
$record = $this->getRecord(
message: 'some log message',
context: [
'nest2' => new \Exception('exception message', 987),
],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
'extra' => [],
];
level: Logger::WARNING,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
$formatter = new MongoDBFormatter(2, false);
$formattedRecord = $formatter->format($record);

View File

@@ -11,7 +11,8 @@
namespace Monolog\Formatter;
use PHPUnit\Framework\TestCase;
use Monolog\Test\TestCase;
use Monolog\Logger;
/**
* @covers Monolog\Formatter\NormalizerFormatter
@@ -21,23 +22,23 @@ class NormalizerFormatterTest extends TestCase
public function testFormat()
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->format([
'level_name' => 'ERROR',
'channel' => 'meh',
'message' => 'foo',
'datetime' => new \DateTimeImmutable,
'extra' => ['foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
'context' => [
$formatted = $formatter->format($this->getRecord(
Logger::ERROR,
'foo',
channel: 'meh',
extra: ['foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
context: [
'foo' => 'bar',
'baz' => 'qux',
'inf' => INF,
'-inf' => -INF,
'nan' => acos(4),
],
]);
));
$this->assertEquals([
'level_name' => 'ERROR',
'level' => Logger::ERROR,
'channel' => 'meh',
'message' => 'foo',
'datetime' => date('Y-m-d'),
@@ -62,7 +63,7 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter('Y-m-d');
$e = new \LogicException('bar');
$e2 = new \RuntimeException('foo', 0, $e);
$formatted = $formatter->format([
$formatted = $formatter->normalizeValue([
'exception' => $e2,
]);
@@ -88,7 +89,7 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter('Y-m-d');
$e = new \SoapFault('foo', 'bar', 'hello', 'world');
$formatted = $formatter->format([
$formatted = $formatter->normalizeValue([
'exception' => $e,
]);
@@ -108,7 +109,7 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter('Y-m-d');
$e = new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world']);
$formatted = $formatter->format([
$formatted = $formatter->normalizeValue([
'exception' => $e,
]);
@@ -132,35 +133,22 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter('Y-m-d');
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Could not convert to string');
$formatter->format([
$formatter->format($this->getRecord(context: [
'myObject' => new TestToStringError(),
]);
]));
}
public function testBatchFormat()
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->formatBatch([
[
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => [],
],
[
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'context' => [],
'datetime' => new \DateTimeImmutable,
'extra' => [],
],
$this->getRecord(Logger::CRITICAL, 'bar', channel: 'test'),
$this->getRecord(Logger::WARNING, 'foo', channel: 'log'),
]);
$this->assertEquals([
[
'level_name' => 'CRITICAL',
'level' => Logger::CRITICAL,
'channel' => 'test',
'message' => 'bar',
'context' => [],
@@ -169,6 +157,7 @@ class NormalizerFormatterTest extends TestCase
],
[
'level_name' => 'WARNING',
'level' => Logger::WARNING,
'channel' => 'log',
'message' => 'foo',
'context' => [],
@@ -217,7 +206,7 @@ class NormalizerFormatterTest extends TestCase
$x = ['foo' => 'bar'];
$y = ['x' => &$x];
$x['y'] = &$y;
$formatter->format($y);
$formatter->normalizeValue($y);
}
public function testToJsonIgnoresInvalidTypes()
@@ -251,13 +240,11 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$largeArray = range(1, 1000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
'bar',
channel: 'test',
context: [$largeArray],
));
$this->assertCount(1000, $res['context'][0]);
@@ -269,13 +256,11 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$largeArray = range(1, 2000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
'bar',
channel: 'test',
context: [$largeArray],
));
$this->assertCount(1001, $res['context'][0]);
@@ -328,12 +313,12 @@ class NormalizerFormatterTest extends TestCase
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
["..." => "Over 0 items (6 total), aborting normalization"],
["..." => "Over 0 items (7 total), aborting normalization"],
$message
);
}
public function testMaxNormalizeItemCountWith3ItemsMax()
public function testMaxNormalizeItemCountWith2ItemsMax()
{
$formatter = new NormalizerFormatter();
$formatter->setMaxNormalizeDepth(9);
@@ -342,8 +327,18 @@ class NormalizerFormatterTest extends TestCase
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
unset($message['context']['exception']['trace']);
unset($message['context']['exception']['file']);
$this->assertEquals(
["level_name" => "CRITICAL", "channel" => "core", "..." => "Over 2 items (6 total), aborting normalization"],
[
"message" => "foobar",
"context" => ['exception' => [
'class' => 'Error',
'message' => 'Foo',
'code' => 0,
]],
"..." => "Over 2 items (7 total), aborting normalization"
],
$message
);
}
@@ -366,7 +361,7 @@ class NormalizerFormatterTest extends TestCase
}
$formatter = new NormalizerFormatter();
$record = ['context' => ['exception' => $e]];
$record = $this->getRecord(context: ['exception' => $e]);
$result = $formatter->format($record);
$this->assertSame(
@@ -383,14 +378,12 @@ class NormalizerFormatterTest extends TestCase
*/
private function formatRecordWithExceptionInContext(NormalizerFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => $exception],
'datetime' => null,
'extra' => [],
'message' => 'foobar',
]);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
'foobar',
channel: 'core',
context: ['exception' => $exception],
));
return $message;
}
@@ -404,7 +397,7 @@ class NormalizerFormatterTest extends TestCase
}
$formatter = new NormalizerFormatter();
$record = array('context' => array('exception' => $e));
$record = $this->getRecord(context: ['exception' => $e]);
$result = $formatter->format($record);
$this->assertSame(

View File

@@ -12,8 +12,9 @@
namespace Monolog\Formatter;
use Monolog\DateTimeImmutable;
use Monolog\Test\TestCase;
class ScalarFormatterTest extends \PHPUnit\Framework\TestCase
class ScalarFormatterTest extends TestCase
{
private $formatter;
@@ -43,7 +44,7 @@ class ScalarFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormat()
{
$exception = new \Exception('foo');
$formatted = $this->formatter->format([
$formatted = $this->formatter->format($this->getRecord(context: [
'foo' => 'string',
'bar' => 1,
'baz' => false,
@@ -51,56 +52,50 @@ class ScalarFormatterTest extends \PHPUnit\Framework\TestCase
'bat' => ['foo' => 'bar'],
'bap' => $dt = new DateTimeImmutable(true),
'ban' => $exception,
]);
]));
$this->assertSame([
$this->assertSame($this->encodeJson([
'foo' => 'string',
'bar' => 1,
'baz' => false,
'bam' => $this->encodeJson([1, 2, 3]),
'bat' => $this->encodeJson(['foo' => 'bar']),
'bam' => [1, 2, 3],
'bat' => ['foo' => 'bar'],
'bap' => (string) $dt,
'ban' => $this->encodeJson([
'ban' => [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'trace' => $this->buildTrace($exception),
]),
], $formatted);
],
]), $formatted['context']);
}
public function testFormatWithErrorContext()
{
$context = ['file' => 'foo', 'line' => 1];
$formatted = $this->formatter->format([
'context' => $context,
]);
$formatted = $this->formatter->format($this->getRecord(
context: $context,
));
$this->assertSame([
'context' => $this->encodeJson($context),
], $formatted);
$this->assertSame($this->encodeJson($context), $formatted['context']);
}
public function testFormatWithExceptionContext()
{
$exception = new \Exception('foo');
$formatted = $this->formatter->format([
'context' => [
'exception' => $exception,
],
]);
$formatted = $this->formatter->format($this->getRecord(context: [
'exception' => $exception,
]));
$this->assertSame([
'context' => $this->encodeJson([
'exception' => [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'trace' => $this->buildTrace($exception),
],
]),
], $formatted);
$this->assertSame($this->encodeJson([
'exception' => [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'trace' => $this->buildTrace($exception),
]
]), $formatted['context']);
}
}

View File

@@ -12,8 +12,9 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Test\TestCase;
class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
class WildfireFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\WildfireFormatter::format
@@ -21,15 +22,13 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
public function testDefaultFormat()
{
$wildfire = new WildfireFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['ip' => '127.0.0.1'],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
extra: ['ip' => '127.0.0.1'],
);
$message = $wildfire->format($record);
@@ -46,15 +45,13 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatWithFileAndLine()
{
$wildfire = new WildfireFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['from' => 'logger'],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
context: ['from' => 'logger'],
extra: ['ip' => '127.0.0.1', 'file' => 'test', 'line' => 14],
);
$message = $wildfire->format($record);
@@ -71,15 +68,11 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormatWithoutContext()
{
$wildfire = new WildfireFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
);
$message = $wildfire->format($record);
@@ -97,15 +90,11 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
$this->expectException(\BadMethodCallException::class);
$wildfire = new WildfireFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => [],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$record = $this->getRecord(
Logger::ERROR,
'log',
channel: 'meh',
);
$wildfire->formatBatch([$record]);
}
@@ -116,11 +105,11 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
public function testTableFormat()
{
$wildfire = new WildfireFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'table-channel',
'context' => [
$record = $this->getRecord(
Logger::ERROR,
'table-message',
channel: 'table-channel',
context: [
'table' => [
['col1', 'col2', 'col3'],
['val1', 'val2', 'val3'],
@@ -128,10 +117,7 @@ class WildfireFormatterTest extends \PHPUnit\Framework\TestCase
['bar1', 'bar2', 'bar3'],
],
],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'table-message',
];
);
$message = $wildfire->format($record);