Chris@0: ". Chris@0: * Chris@0: * Example ->error('Foo') would yield "error Foo". Chris@0: * Chris@0: * @return string[] Chris@0: */ Chris@0: abstract public function getLogs(); Chris@0: Chris@0: public function testImplements() Chris@0: { Chris@0: $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider provideLevelsAndMessages Chris@0: */ Chris@0: public function testLogsAtAllLevels($level, $message) Chris@0: { Chris@0: $logger = $this->getLogger(); Chris@0: $logger->{$level}($message, array('user' => 'Bob')); Chris@0: $logger->log($level, $message, array('user' => 'Bob')); Chris@0: Chris@0: $expected = array( Chris@0: $level.' message of level '.$level.' with context: Bob', Chris@0: $level.' message of level '.$level.' with context: Bob', Chris@0: ); Chris@0: $this->assertEquals($expected, $this->getLogs()); Chris@0: } Chris@0: Chris@0: public function provideLevelsAndMessages() Chris@0: { Chris@0: return array( Chris@0: LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), Chris@0: LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), Chris@0: LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), Chris@0: LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), Chris@0: LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), Chris@0: LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), Chris@0: LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), Chris@0: LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \Psr\Log\InvalidArgumentException Chris@0: */ Chris@0: public function testThrowsOnInvalidLevel() Chris@0: { Chris@0: $logger = $this->getLogger(); Chris@0: $logger->log('invalid level', 'Foo'); Chris@0: } Chris@0: Chris@0: public function testContextReplacement() Chris@0: { Chris@0: $logger = $this->getLogger(); Chris@0: $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); Chris@0: Chris@0: $expected = array('info {Message {nothing} Bob Bar a}'); Chris@0: $this->assertEquals($expected, $this->getLogs()); Chris@0: } Chris@0: Chris@0: public function testObjectCastToString() Chris@0: { Chris@0: if (method_exists($this, 'createPartialMock')) { Chris@0: $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); Chris@0: } else { Chris@0: $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); Chris@0: } Chris@0: $dummy->expects($this->once()) Chris@0: ->method('__toString') Chris@0: ->will($this->returnValue('DUMMY')); Chris@0: Chris@0: $this->getLogger()->warning($dummy); Chris@0: Chris@0: $expected = array('warning DUMMY'); Chris@0: $this->assertEquals($expected, $this->getLogs()); Chris@0: } Chris@0: Chris@0: public function testContextCanContainAnything() Chris@0: { Chris@17: $closed = fopen('php://memory', 'r'); Chris@17: fclose($closed); Chris@17: Chris@0: $context = array( Chris@0: 'bool' => true, Chris@0: 'null' => null, Chris@0: 'string' => 'Foo', Chris@0: 'int' => 0, Chris@0: 'float' => 0.5, Chris@0: 'nested' => array('with object' => new DummyTest), Chris@0: 'object' => new \DateTime, Chris@0: 'resource' => fopen('php://memory', 'r'), Chris@17: 'closed' => $closed, Chris@0: ); Chris@0: Chris@0: $this->getLogger()->warning('Crazy context data', $context); Chris@0: Chris@0: $expected = array('warning Crazy context data'); Chris@0: $this->assertEquals($expected, $this->getLogs()); Chris@0: } Chris@0: Chris@0: public function testContextExceptionKeyCanBeExceptionOrOtherValues() Chris@0: { Chris@0: $logger = $this->getLogger(); Chris@0: $logger->warning('Random message', array('exception' => 'oops')); Chris@0: $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); Chris@0: Chris@0: $expected = array( Chris@0: 'warning Random message', Chris@0: 'critical Uncaught Exception!' Chris@0: ); Chris@0: $this->assertEquals($expected, $this->getLogs()); Chris@0: } Chris@0: } Chris@0: Chris@0: class DummyTest Chris@0: { Chris@0: public function __toString() Chris@0: { Chris@0: } Chris@0: }