1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-13 16:44:23 +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

@@ -87,11 +87,9 @@ class DeduplicationHandlerTest extends TestCase
$test = new TestHandler();
$handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
$record = $this->getRecord(Logger::ERROR);
$record['datetime'] = $record['datetime']->modify('+62seconds');
$record = $this->getRecord(Logger::ERROR, datetime: new \DateTimeImmutable('+62seconds'));
$handler->handle($record);
$record = $this->getRecord(Logger::CRITICAL);
$record['datetime'] = $record['datetime']->modify('+62seconds');
$record = $this->getRecord(Logger::CRITICAL, datetime: new \DateTimeImmutable('+62seconds'));
$handler->handle($record);
$handler->flush();
@@ -114,14 +112,11 @@ class DeduplicationHandlerTest extends TestCase
$handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
// handle two records from yesterday, and one recent
$record = $this->getRecord(Logger::ERROR);
$record['datetime'] = $record['datetime']->modify('-1day -10seconds');
$record = $this->getRecord(Logger::ERROR, datetime: new \DateTimeImmutable('-1day -10seconds'));
$handler->handle($record);
$record2 = $this->getRecord(Logger::CRITICAL);
$record2['datetime'] = $record2['datetime']->modify('-1day -10seconds');
$record2 = $this->getRecord(Logger::CRITICAL, datetime: new \DateTimeImmutable('-1day -10seconds'));
$handler->handle($record2);
$record3 = $this->getRecord(Logger::CRITICAL);
$record3['datetime'] = $record3['datetime']->modify('-30seconds');
$record3 = $this->getRecord(Logger::CRITICAL, datetime: new \DateTimeImmutable('-30seconds'));
$handler->handle($record3);
// log is written as none of them are duplicate

View File

@@ -58,15 +58,7 @@ class ElasticaHandlerTest extends TestCase
public function testHandle()
{
// log message
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
$msg = $this->getRecord(Logger::ERROR, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
// format expected result
$formatter = new ElasticaFormatter($this->options['index'], $this->options['type']);
@@ -166,17 +158,9 @@ class ElasticaHandlerTest extends TestCase
*/
public function testHandleIntegration()
{
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
$msg = $this->getRecord(Logger::ERROR, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
$expected = (array) $msg;
$expected = $msg->toArray();
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',
@@ -220,15 +204,7 @@ class ElasticaHandlerTest extends TestCase
*/
public function testHandleIntegrationNewESVersion()
{
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
$msg = $this->getRecord(Logger::ERROR, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
$expected = (array) $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);

View File

@@ -56,15 +56,7 @@ class ElasticsearchHandlerTest extends TestCase
public function testHandle()
{
// 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', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
// format expected result
$formatter = new ElasticsearchFormatter($this->options['index'], $this->options['type']);
@@ -180,17 +172,9 @@ class ElasticsearchHandlerTest extends TestCase
*/
public function testHandleIntegration()
{
$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', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
$expected = $msg;
$expected = $msg->toArray();
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => ["stdClass" => []],

View File

@@ -209,8 +209,7 @@ class FingersCrossedHandlerTest extends TestCase
$handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, ['othertest' => Logger::DEBUG]));
$handler->handle($this->getRecord(Logger::WARNING));
$this->assertFalse($test->hasWarningRecords());
$record = $this->getRecord(Logger::DEBUG);
$record['channel'] = 'othertest';
$record = $this->getRecord(Logger::DEBUG, channel: 'othertest');
$handler->handle($record);
$this->assertTrue($test->hasDebugRecords());
$this->assertTrue($test->hasWarningRecords());
@@ -226,8 +225,7 @@ class FingersCrossedHandlerTest extends TestCase
$handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', ['othertest' => 'debug']));
$handler->handle($this->getRecord(Logger::WARNING));
$this->assertFalse($test->hasWarningRecords());
$record = $this->getRecord(Logger::DEBUG);
$record['channel'] = 'othertest';
$record = $this->getRecord(Logger::DEBUG, channel: 'othertest');
$handler->handle($record);
$this->assertTrue($test->hasDebugRecords());
$this->assertTrue($test->hasWarningRecords());
@@ -242,7 +240,7 @@ class FingersCrossedHandlerTest extends TestCase
$test = new TestHandler();
$handler = new FingersCrossedHandler($test, Logger::INFO);
$handler->pushProcessor(function ($record) {
$record['extra']['foo'] = true;
$record->extra['foo'] = true;
return $record;
});

View File

@@ -56,15 +56,7 @@ class FleepHookHandlerTest extends TestCase
*/
public function testHandlerUsesLineFormatterWhichIgnoresEmptyArrays()
{
$record = [
'message' => 'msg',
'context' => [],
'level' => Logger::DEBUG,
'level_name' => Logger::getLevelName(Logger::DEBUG),
'channel' => 'channel',
'datetime' => new \DateTimeImmutable(),
'extra' => [],
];
$record = $this->getRecord(Logger::DEBUG, 'msg');
$expectedFormatter = new LineFormatter(null, null, true, true);
$expected = $expectedFormatter->format($record);

View File

@@ -93,9 +93,12 @@ class GelfHandlerTest extends TestCase
public function testInjectedGelfMessageFormatter()
{
$record = $this->getRecord(Logger::WARNING, "A test warning message");
$record['extra']['blarg'] = 'yep';
$record['context']['from'] = 'logger';
$record = $this->getRecord(
Logger::WARNING,
"A test warning message",
extra: ['blarg' => 'yep'],
context: ['from' => 'logger'],
);
$expectedMessage = new Message();
$expectedMessage

View File

@@ -43,7 +43,7 @@ class MongoDBHandlerTest extends TestCase
->will($this->returnValue($collection));
$record = $this->getRecord();
$expected = $record;
$expected = $record->toArray();
$expected['datetime'] = new \MongoDB\BSON\UTCDateTime((int) floor(((float) $record['datetime']->format('U.u')) * 1000));
$collection->expects($this->once())

View File

@@ -46,7 +46,7 @@ class PsrHandlerTest extends TestCase
->with(strtolower($levelName), $message, $context);
$handler = new PsrHandler($psrLogger);
$handler->handle(['level' => $level, 'level_name' => $levelName, 'message' => $message, 'context' => $context]);
$handler->handle($this->getRecord($level, $message, context: $context));
}
public function testFormatter()
@@ -63,6 +63,6 @@ class PsrHandlerTest extends TestCase
$handler = new PsrHandler($psrLogger);
$handler->setFormatter(new LineFormatter('dummy'));
$handler->handle(['level' => $level, 'level_name' => $levelName, 'message' => $message, 'context' => $context, 'extra' => [], 'date' => new \DateTimeImmutable()]);
$handler->handle($this->getRecord($level, $message, context: $context, datetime: new \DateTimeImmutable()));
}
}

View File

@@ -38,12 +38,12 @@ class SyslogUdpHandlerTest extends TestCase
->onlyMethods(['write'])
->setConstructorArgs(['lol'])
->getMock();
$socket->expects($this->at(0))
$socket->expects($this->atLeast(2))
->method('write')
->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ");
$socket->expects($this->at(1))
->method('write')
->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ");
->withConsecutive(
[$this->equalTo("lol"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ")],
[$this->equalTo("hej"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ")],
);
$handler->setSocket($socket);
@@ -64,7 +64,7 @@ class SyslogUdpHandlerTest extends TestCase
$handler->setSocket($socket);
$handler->handle($this->getRecordWithMessage(null));
$handler->handle($this->getRecordWithMessage(''));
}
public function testRfc()
@@ -84,12 +84,12 @@ class SyslogUdpHandlerTest extends TestCase
->setConstructorArgs(array('lol', 999))
->onlyMethods(array('write'))
->getMock();
$socket->expects($this->at(0))
$socket->expects($this->atLeast(2))
->method('write')
->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ");
$socket->expects($this->at(1))
->method('write')
->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ");
->withConsecutive(
[$this->equalTo("lol"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ")],
[$this->equalTo("hej"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ")],
);
$handler->setSocket($socket);
@@ -98,6 +98,6 @@ class SyslogUdpHandlerTest extends TestCase
protected function getRecordWithMessage($msg)
{
return ['message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => [], 'channel' => 'lol', 'datetime' => new \DateTimeImmutable('2014-01-07 12:34:56')];
return $this->getRecord(message: $msg, level: \Monolog\Logger::WARNING, channel: 'lol', datetime: new \DateTimeImmutable('2014-01-07 12:34:56'));
}
}

View File

@@ -27,8 +27,8 @@ class TestHandlerTest extends TestCase
$handler = new TestHandler;
$record = $this->getRecord($level, 'test'.$method);
$this->assertFalse($handler->hasRecords($level));
$this->assertFalse($handler->hasRecord($record, $level));
$this->assertFalse($handler->{'has'.$method}($record), 'has'.$method);
$this->assertFalse($handler->hasRecord($record->message, $level));
$this->assertFalse($handler->{'has'.$method}($record->message), 'has'.$method);
$this->assertFalse($handler->{'has'.$method.'ThatContains'}('test'), 'has'.$method.'ThatContains');
$this->assertFalse($handler->{'has'.$method.'ThatPasses'}(function ($rec) {
return true;
@@ -39,8 +39,8 @@ class TestHandlerTest extends TestCase
$this->assertFalse($handler->{'has'.$method}('bar'), 'has'.$method);
$this->assertTrue($handler->hasRecords($level));
$this->assertTrue($handler->hasRecord($record, $level));
$this->assertTrue($handler->{'has'.$method}($record), 'has'.$method);
$this->assertTrue($handler->hasRecord($record->message, $level));
$this->assertTrue($handler->{'has'.$method}($record->message), 'has'.$method);
$this->assertTrue($handler->{'has'.$method}('test'.$method), 'has'.$method);
$this->assertTrue($handler->{'has'.$method.'ThatContains'}('test'), 'has'.$method.'ThatContains');
$this->assertTrue($handler->{'has'.$method.'ThatPasses'}(function ($rec) {
@@ -50,7 +50,7 @@ class TestHandlerTest extends TestCase
$this->assertTrue($handler->{'has'.$method.'Records'}(), 'has'.$method.'Records');
$records = $handler->getRecords();
unset($records[0]['formatted']);
$records[0]->formatted = null;
$this->assertEquals([$record], $records);
}