summaryrefslogtreecommitdiffstats
path: root/tests/Monolog/LoggerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Monolog/LoggerTest.php')
-rw-r--r--tests/Monolog/LoggerTest.php213
1 files changed, 122 insertions, 91 deletions
diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php
index 1ecc34a..428daa0 100644
--- a/tests/Monolog/LoggerTest.php
+++ b/tests/Monolog/LoggerTest.php
@@ -1,4 +1,4 @@
-<?php
+<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
@@ -14,7 +14,7 @@ namespace Monolog;
use Monolog\Processor\WebProcessor;
use Monolog\Handler\TestHandler;
-class LoggerTest extends \PHPUnit_Framework_TestCase
+class LoggerTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers Monolog\Logger::getName
@@ -38,7 +38,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
*/
public function testWithName()
{
- $first = new Logger('first', array($handler = new TestHandler()));
+ $first = new Logger('first', [$handler = new TestHandler()]);
$second = $first->withName('second');
$this->assertSame('first', $first->getName());
@@ -78,7 +78,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger = new Logger('foo');
$handler = new TestHandler;
$logger->pushHandler($handler);
- $logger->addWarning('test');
+ $logger->warning('test');
list($record) = $handler->getRecords();
$this->assertEquals('foo', $record['channel']);
}
@@ -90,12 +90,13 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
- $handler->expects($this->once())
- ->method('handle');
- $logger->pushHandler($handler);
+ $handler = $this->prophesize('Monolog\Handler\NullHandler');
+ $handler->handle(\Prophecy\Argument::any())->shouldBeCalled();
+ $handler->isHandling(['level' => 300])->willReturn(true);
- $this->assertTrue($logger->addWarning('test'));
+ $logger->pushHandler($handler->reveal());
+
+ $this->assertTrue($logger->addRecord(Logger::WARNING, 'test'));
}
/**
@@ -105,19 +106,20 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'), array(Logger::ERROR));
- $handler->expects($this->never())
- ->method('handle');
- $logger->pushHandler($handler);
+ $handler = $this->prophesize('Monolog\Handler\NullHandler');
+ $handler->handle()->shouldNotBeCalled();
+ $handler->isHandling(['level' => 300])->willReturn(false);
- $this->assertFalse($logger->addWarning('test'));
+ $logger->pushHandler($handler->reveal());
+
+ $this->assertFalse($logger->addRecord(Logger::WARNING, 'test'));
}
public function testHandlersInCtor()
{
$handler1 = new TestHandler;
$handler2 = new TestHandler;
- $logger = new Logger(__METHOD__, array($handler1, $handler2));
+ $logger = new Logger(__METHOD__, [$handler1, $handler2]);
$this->assertEquals($handler1, $logger->popHandler());
$this->assertEquals($handler2, $logger->popHandler());
@@ -127,7 +129,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$processor1 = new WebProcessor;
$processor2 = new WebProcessor;
- $logger = new Logger(__METHOD__, array(), array($processor1, $processor2));
+ $logger = new Logger(__METHOD__, [], [$processor1, $processor2]);
$this->assertEquals($processor1, $logger->popProcessor());
$this->assertEquals($processor2, $logger->popProcessor());
@@ -162,18 +164,18 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$handler2 = new TestHandler;
$logger->pushHandler($handler1);
- $logger->setHandlers(array($handler2));
+ $logger->setHandlers([$handler2]);
// handler1 has been removed
- $this->assertEquals(array($handler2), $logger->getHandlers());
+ $this->assertEquals([$handler2], $logger->getHandlers());
- $logger->setHandlers(array(
+ $logger->setHandlers([
"AMapKey" => $handler1,
"Woop" => $handler2,
- ));
+ ]);
// Keys have been scrubbed
- $this->assertEquals(array($handler1, $handler2), $logger->getHandlers());
+ $this->assertEquals([$handler1, $handler2], $logger->getHandlers());
}
/**
@@ -196,17 +198,6 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
}
/**
- * @covers Monolog\Logger::pushProcessor
- * @expectedException InvalidArgumentException
- */
- public function testPushProcessorWithNonCallable()
- {
- $logger = new Logger(__METHOD__);
-
- $logger->pushProcessor(new \stdClass());
- }
-
- /**
* @covers Monolog\Logger::addRecord
*/
public function testProcessorsAreExecuted()
@@ -219,7 +210,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
return $record;
});
- $logger->addError('test');
+ $logger->error('test');
list($record) = $handler->getRecords();
$this->assertTrue($record['extra']['win']);
}
@@ -230,7 +221,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
public function testProcessorsAreCalledOnlyOnce()
{
$logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler = $this->createMock('Monolog\Handler\HandlerInterface');
$handler->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -243,7 +234,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
+ ->setMethods(['__invoke'])
->getMock()
;
$processor->expects($this->once())
@@ -252,7 +243,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
;
$logger->pushProcessor($processor);
- $logger->addError('test');
+ $logger->error('test');
}
/**
@@ -261,7 +252,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
public function testProcessorsNotCalledWhenNotHandled()
{
$logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler = $this->createMock('Monolog\Handler\HandlerInterface');
$handler->expects($this->once())
->method('isHandling')
->will($this->returnValue(false))
@@ -271,7 +262,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->pushProcessor(function ($record) use ($that) {
$that->fail('The processor should not be called');
});
- $logger->addAlert('test');
+ $logger->alert('test');
}
/**
@@ -281,7 +272,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler1 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->never())
->method('isHandling')
->will($this->returnValue(false))
@@ -292,7 +283,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
;
$logger->pushHandler($handler1);
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->once())
->method('isHandling')
->will($this->returnValue(true))
@@ -303,7 +294,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
;
$logger->pushHandler($handler2);
- $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler3 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler3->expects($this->once())
->method('isHandling')
->will($this->returnValue(false))
@@ -321,7 +312,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
*/
public function testHandlersNotCalledBeforeFirstHandlingWithAssocArray()
{
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler1 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->never())
->method('isHandling')
->will($this->returnValue(false))
@@ -331,7 +322,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue(false))
;
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->once())
->method('isHandling')
->will($this->returnValue(true))
@@ -341,7 +332,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue(false))
;
- $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler3 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler3->expects($this->once())
->method('isHandling')
->will($this->returnValue(false))
@@ -350,7 +341,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
->method('handle')
;
- $logger = new Logger(__METHOD__, array('last' => $handler3, 'second' => $handler2, 'first' => $handler1));
+ $logger = new Logger(__METHOD__, ['last' => $handler3, 'second' => $handler2, 'first' => $handler1]);
$logger->debug('test');
}
@@ -362,7 +353,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler1 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -373,7 +364,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
;
$logger->pushHandler($handler1);
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -394,7 +385,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler1 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -404,7 +395,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
;
$logger->pushHandler($handler1);
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -425,7 +416,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{
$logger = new Logger(__METHOD__);
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler1 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->any())
->method('isHandling')
->will($this->returnValue(false))
@@ -434,7 +425,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->pushHandler($handler1);
$this->assertFalse($logger->isHandling(Logger::DEBUG));
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
+ $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->any())
->method('isHandling')
->will($this->returnValue(true))
@@ -446,22 +437,14 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider logMethodProvider
- * @covers Monolog\Logger::addDebug
- * @covers Monolog\Logger::addInfo
- * @covers Monolog\Logger::addNotice
- * @covers Monolog\Logger::addWarning
- * @covers Monolog\Logger::addError
- * @covers Monolog\Logger::addCritical
- * @covers Monolog\Logger::addAlert
- * @covers Monolog\Logger::addEmergency
* @covers Monolog\Logger::debug
* @covers Monolog\Logger::info
* @covers Monolog\Logger::notice
- * @covers Monolog\Logger::warn
- * @covers Monolog\Logger::err
- * @covers Monolog\Logger::crit
+ * @covers Monolog\Logger::warning
+ * @covers Monolog\Logger::error
+ * @covers Monolog\Logger::critical
* @covers Monolog\Logger::alert
- * @covers Monolog\Logger::emerg
+ * @covers Monolog\Logger::emergency
*/
public function testLogMethods($method, $expectedLevel)
{
@@ -475,27 +458,17 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
public function logMethodProvider()
{
- return array(
- // monolog methods
- array('addDebug', Logger::DEBUG),
- array('addInfo', Logger::INFO),
- array('addNotice', Logger::NOTICE),
- array('addWarning', Logger::WARNING),
- array('addError', Logger::ERROR),
- array('addCritical', Logger::CRITICAL),
- array('addAlert', Logger::ALERT),
- array('addEmergency', Logger::EMERGENCY),
-
- // ZF/Sf2 compat methods
- array('debug', Logger::DEBUG),
- array('info', Logger::INFO),
- array('notice', Logger::NOTICE),
- array('warn', Logger::WARNING),
- array('err', Logger::ERROR),
- array('crit', Logger::CRITICAL),
- array('alert', Logger::ALERT),
- array('emerg', Logger::EMERGENCY),
- );
+ return [
+ // PSR-3 methods
+ ['debug', Logger::DEBUG],
+ ['info', Logger::INFO],
+ ['notice', Logger::NOTICE],
+ ['warning', Logger::WARNING],
+ ['error', Logger::ERROR],
+ ['critical', Logger::CRITICAL],
+ ['alert', Logger::ALERT],
+ ['emergency', Logger::EMERGENCY],
+ ];
}
/**
@@ -504,8 +477,8 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
*/
public function testSetTimezone($tz)
{
- Logger::setTimezone($tz);
$logger = new Logger('foo');
+ $logger->setTimezone($tz);
$handler = new TestHandler;
$logger->pushHandler($handler);
$logger->info('test');
@@ -516,18 +489,74 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
public function setTimezoneProvider()
{
return array_map(
- function ($tz) { return array(new \DateTimeZone($tz)); },
+ function ($tz) {
+ return [new \DateTimeZone($tz)];
+ },
\DateTimeZone::listIdentifiers()
);
}
/**
+ * @covers Monolog\Logger::setTimezone
+ * @covers Monolog\DateTimeImmutable::__construct
+ */
+ public function testTimezoneIsRespectedInUTC()
+ {
+ foreach ([true, false] as $microseconds) {
+ $logger = new Logger('foo');
+ $logger->useMicrosecondTimestamps($microseconds);
+ $tz = new \DateTimeZone('America/New_York');
+ $logger->setTimezone($tz);
+ $handler = new TestHandler;
+ $logger->pushHandler($handler);
+ $dt = new \DateTime('now', $tz);
+ $logger->info('test');
+ list($record) = $handler->getRecords();
+
+ $this->assertEquals($tz, $record['datetime']->getTimezone());
+ $this->assertEquals($dt->format('Y/m/d H:i'), $record['datetime']->format('Y/m/d H:i'), 'Time should match timezone with microseconds set to: '.var_export($microseconds, true));
+ }
+ }
+
+ /**
+ * @covers Monolog\Logger::setTimezone
+ * @covers Monolog\DateTimeImmutable::__construct
+ */
+ public function testTimezoneIsRespectedInOtherTimezone()
+ {
+ date_default_timezone_set('CET');
+ foreach ([true, false] as $microseconds) {
+ $logger = new Logger('foo');
+ $logger->useMicrosecondTimestamps($microseconds);
+ $tz = new \DateTimeZone('America/New_York');
+ $logger->setTimezone($tz);
+ $handler = new TestHandler;
+ $logger->pushHandler($handler);
+ $dt = new \DateTime('now', $tz);
+ $logger->info('test');
+ list($record) = $handler->getRecords();
+
+ $this->assertEquals($tz, $record['datetime']->getTimezone());
+ $this->assertEquals($dt->format('Y/m/d H:i'), $record['datetime']->format('Y/m/d H:i'), 'Time should match timezone with microseconds set to: '.var_export($microseconds, true));
+ }
+ }
+
+ public function tearDown()
+ {
+ date_default_timezone_set('UTC');
+ }
+
+ /**
* @dataProvider useMicrosecondTimestampsProvider
* @covers Monolog\Logger::useMicrosecondTimestamps
* @covers Monolog\Logger::addRecord
*/
- public function testUseMicrosecondTimestamps($micro, $assert)
+ public function testUseMicrosecondTimestamps($micro, $assert, $assertFormat)
{
+ if (PHP_VERSION_ID === 70103) {
+ $this->markTestSkipped();
+ }
+
$logger = new Logger('foo');
$logger->useMicrosecondTimestamps($micro);
$handler = new TestHandler;
@@ -535,14 +564,16 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->info('test');
list($record) = $handler->getRecords();
$this->{$assert}('000000', $record['datetime']->format('u'));
+ $this->assertSame($record['datetime']->format($assertFormat), (string) $record['datetime']);
}
public function useMicrosecondTimestampsProvider()
{
- return array(
+ return [
// this has a very small chance of a false negative (1/10^6)
- 'with microseconds' => array(true, 'assertNotSame'),
- 'without microseconds' => array(false, PHP_VERSION_ID >= 70100 ? 'assertNotSame' : 'assertSame'),
- );
+ 'with microseconds' => [true, 'assertNotSame', 'Y-m-d\TH:i:s.uP'],
+ // php 7.1 always includes microseconds, so we keep them in, but we format the datetime without
+ 'without microseconds' => [false, PHP_VERSION_ID >= 70100 ? 'assertNotSame' : 'assertSame', 'Y-m-d\TH:i:sP'],
+ ];
}
}