1*dad993c5SAndreas Gohr<?php 2*dad993c5SAndreas Gohr 3*dad993c5SAndreas Gohrnamespace Psr\Log\Test; 4*dad993c5SAndreas Gohr 5*dad993c5SAndreas Gohruse Psr\Log\LoggerInterface; 6*dad993c5SAndreas Gohruse Psr\Log\LogLevel; 7*dad993c5SAndreas Gohruse PHPUnit\Framework\TestCase; 8*dad993c5SAndreas Gohr 9*dad993c5SAndreas Gohr/** 10*dad993c5SAndreas Gohr * Provides a base test class for ensuring compliance with the LoggerInterface. 11*dad993c5SAndreas Gohr * 12*dad993c5SAndreas Gohr * Implementors can extend the class and implement abstract methods to run this 13*dad993c5SAndreas Gohr * as part of their test suite. 14*dad993c5SAndreas Gohr */ 15*dad993c5SAndreas Gohrabstract class LoggerInterfaceTest extends TestCase 16*dad993c5SAndreas Gohr{ 17*dad993c5SAndreas Gohr /** 18*dad993c5SAndreas Gohr * @return LoggerInterface 19*dad993c5SAndreas Gohr */ 20*dad993c5SAndreas Gohr abstract public function getLogger(); 21*dad993c5SAndreas Gohr 22*dad993c5SAndreas Gohr /** 23*dad993c5SAndreas Gohr * This must return the log messages in order. 24*dad993c5SAndreas Gohr * 25*dad993c5SAndreas Gohr * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". 26*dad993c5SAndreas Gohr * 27*dad993c5SAndreas Gohr * Example ->error('Foo') would yield "error Foo". 28*dad993c5SAndreas Gohr * 29*dad993c5SAndreas Gohr * @return string[] 30*dad993c5SAndreas Gohr */ 31*dad993c5SAndreas Gohr abstract public function getLogs(); 32*dad993c5SAndreas Gohr 33*dad993c5SAndreas Gohr public function testImplements() 34*dad993c5SAndreas Gohr { 35*dad993c5SAndreas Gohr $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); 36*dad993c5SAndreas Gohr } 37*dad993c5SAndreas Gohr 38*dad993c5SAndreas Gohr /** 39*dad993c5SAndreas Gohr * @dataProvider provideLevelsAndMessages 40*dad993c5SAndreas Gohr */ 41*dad993c5SAndreas Gohr public function testLogsAtAllLevels($level, $message) 42*dad993c5SAndreas Gohr { 43*dad993c5SAndreas Gohr $logger = $this->getLogger(); 44*dad993c5SAndreas Gohr $logger->{$level}($message, array('user' => 'Bob')); 45*dad993c5SAndreas Gohr $logger->log($level, $message, array('user' => 'Bob')); 46*dad993c5SAndreas Gohr 47*dad993c5SAndreas Gohr $expected = array( 48*dad993c5SAndreas Gohr $level.' message of level '.$level.' with context: Bob', 49*dad993c5SAndreas Gohr $level.' message of level '.$level.' with context: Bob', 50*dad993c5SAndreas Gohr ); 51*dad993c5SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 52*dad993c5SAndreas Gohr } 53*dad993c5SAndreas Gohr 54*dad993c5SAndreas Gohr public function provideLevelsAndMessages() 55*dad993c5SAndreas Gohr { 56*dad993c5SAndreas Gohr return array( 57*dad993c5SAndreas Gohr LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), 58*dad993c5SAndreas Gohr LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), 59*dad993c5SAndreas Gohr LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), 60*dad993c5SAndreas Gohr LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), 61*dad993c5SAndreas Gohr LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), 62*dad993c5SAndreas Gohr LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), 63*dad993c5SAndreas Gohr LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), 64*dad993c5SAndreas Gohr LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), 65*dad993c5SAndreas Gohr ); 66*dad993c5SAndreas Gohr } 67*dad993c5SAndreas Gohr 68*dad993c5SAndreas Gohr /** 69*dad993c5SAndreas Gohr * @expectedException \Psr\Log\InvalidArgumentException 70*dad993c5SAndreas Gohr */ 71*dad993c5SAndreas Gohr public function testThrowsOnInvalidLevel() 72*dad993c5SAndreas Gohr { 73*dad993c5SAndreas Gohr $logger = $this->getLogger(); 74*dad993c5SAndreas Gohr $logger->log('invalid level', 'Foo'); 75*dad993c5SAndreas Gohr } 76*dad993c5SAndreas Gohr 77*dad993c5SAndreas Gohr public function testContextReplacement() 78*dad993c5SAndreas Gohr { 79*dad993c5SAndreas Gohr $logger = $this->getLogger(); 80*dad993c5SAndreas Gohr $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); 81*dad993c5SAndreas Gohr 82*dad993c5SAndreas Gohr $expected = array('info {Message {nothing} Bob Bar a}'); 83*dad993c5SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 84*dad993c5SAndreas Gohr } 85*dad993c5SAndreas Gohr 86*dad993c5SAndreas Gohr public function testObjectCastToString() 87*dad993c5SAndreas Gohr { 88*dad993c5SAndreas Gohr if (method_exists($this, 'createPartialMock')) { 89*dad993c5SAndreas Gohr $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); 90*dad993c5SAndreas Gohr } else { 91*dad993c5SAndreas Gohr $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); 92*dad993c5SAndreas Gohr } 93*dad993c5SAndreas Gohr $dummy->expects($this->once()) 94*dad993c5SAndreas Gohr ->method('__toString') 95*dad993c5SAndreas Gohr ->will($this->returnValue('DUMMY')); 96*dad993c5SAndreas Gohr 97*dad993c5SAndreas Gohr $this->getLogger()->warning($dummy); 98*dad993c5SAndreas Gohr 99*dad993c5SAndreas Gohr $expected = array('warning DUMMY'); 100*dad993c5SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 101*dad993c5SAndreas Gohr } 102*dad993c5SAndreas Gohr 103*dad993c5SAndreas Gohr public function testContextCanContainAnything() 104*dad993c5SAndreas Gohr { 105*dad993c5SAndreas Gohr $closed = fopen('php://memory', 'r'); 106*dad993c5SAndreas Gohr fclose($closed); 107*dad993c5SAndreas Gohr 108*dad993c5SAndreas Gohr $context = array( 109*dad993c5SAndreas Gohr 'bool' => true, 110*dad993c5SAndreas Gohr 'null' => null, 111*dad993c5SAndreas Gohr 'string' => 'Foo', 112*dad993c5SAndreas Gohr 'int' => 0, 113*dad993c5SAndreas Gohr 'float' => 0.5, 114*dad993c5SAndreas Gohr 'nested' => array('with object' => new DummyTest), 115*dad993c5SAndreas Gohr 'object' => new \DateTime, 116*dad993c5SAndreas Gohr 'resource' => fopen('php://memory', 'r'), 117*dad993c5SAndreas Gohr 'closed' => $closed, 118*dad993c5SAndreas Gohr ); 119*dad993c5SAndreas Gohr 120*dad993c5SAndreas Gohr $this->getLogger()->warning('Crazy context data', $context); 121*dad993c5SAndreas Gohr 122*dad993c5SAndreas Gohr $expected = array('warning Crazy context data'); 123*dad993c5SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 124*dad993c5SAndreas Gohr } 125*dad993c5SAndreas Gohr 126*dad993c5SAndreas Gohr public function testContextExceptionKeyCanBeExceptionOrOtherValues() 127*dad993c5SAndreas Gohr { 128*dad993c5SAndreas Gohr $logger = $this->getLogger(); 129*dad993c5SAndreas Gohr $logger->warning('Random message', array('exception' => 'oops')); 130*dad993c5SAndreas Gohr $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); 131*dad993c5SAndreas Gohr 132*dad993c5SAndreas Gohr $expected = array( 133*dad993c5SAndreas Gohr 'warning Random message', 134*dad993c5SAndreas Gohr 'critical Uncaught Exception!' 135*dad993c5SAndreas Gohr ); 136*dad993c5SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 137*dad993c5SAndreas Gohr } 138*dad993c5SAndreas Gohr} 139