2015-08-17 17:00:26 -07:00
< ? php
namespace Psr\Log\Test ;
2017-04-13 15:53:35 +01:00
use Psr\Log\LoggerInterface ;
2015-08-17 17:00:26 -07:00
use Psr\Log\LogLevel ;
/**
2017-04-13 15:53:35 +01:00
* Provides a base test class for ensuring compliance with the LoggerInterface .
2015-08-17 17:00:26 -07:00
*
2017-04-13 15:53:35 +01:00
* Implementors can extend the class and implement abstract methods to run this
* as part of their test suite .
2015-08-17 17:00:26 -07:00
*/
abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase
{
/**
* @ return LoggerInterface
*/
2017-04-13 15:53:35 +01:00
abstract public function getLogger ();
2015-08-17 17:00:26 -07:00
/**
2017-04-13 15:53:35 +01:00
* This must return the log messages in order .
2015-08-17 17:00:26 -07:00
*
2017-04-13 15:53:35 +01:00
* The simple formatting of the messages is : " <LOG LEVEL> <MESSAGE> " .
*
* Example -> error ( 'Foo' ) would yield " error Foo " .
2015-08-17 17:00:26 -07:00
*
* @ return string []
*/
2017-04-13 15:53:35 +01:00
abstract public function getLogs ();
2015-08-17 17:00:26 -07:00
public function testImplements ()
{
$this -> assertInstanceOf ( 'Psr\Log\LoggerInterface' , $this -> getLogger ());
}
/**
* @ dataProvider provideLevelsAndMessages
*/
public function testLogsAtAllLevels ( $level , $message )
{
$logger = $this -> getLogger ();
$logger -> { $level }( $message , array ( 'user' => 'Bob' ));
$logger -> log ( $level , $message , array ( 'user' => 'Bob' ));
$expected = array (
$level . ' message of level ' . $level . ' with context: Bob' ,
$level . ' message of level ' . $level . ' with context: Bob' ,
);
$this -> assertEquals ( $expected , $this -> getLogs ());
}
public function provideLevelsAndMessages ()
{
return array (
LogLevel :: EMERGENCY => array ( LogLevel :: EMERGENCY , 'message of level emergency with context: {user}' ),
LogLevel :: ALERT => array ( LogLevel :: ALERT , 'message of level alert with context: {user}' ),
LogLevel :: CRITICAL => array ( LogLevel :: CRITICAL , 'message of level critical with context: {user}' ),
LogLevel :: ERROR => array ( LogLevel :: ERROR , 'message of level error with context: {user}' ),
LogLevel :: WARNING => array ( LogLevel :: WARNING , 'message of level warning with context: {user}' ),
LogLevel :: NOTICE => array ( LogLevel :: NOTICE , 'message of level notice with context: {user}' ),
LogLevel :: INFO => array ( LogLevel :: INFO , 'message of level info with context: {user}' ),
LogLevel :: DEBUG => array ( LogLevel :: DEBUG , 'message of level debug with context: {user}' ),
);
}
/**
2017-04-13 15:53:35 +01:00
* @ expectedException \Psr\Log\InvalidArgumentException
2015-08-17 17:00:26 -07:00
*/
public function testThrowsOnInvalidLevel ()
{
$logger = $this -> getLogger ();
$logger -> log ( 'invalid level' , 'Foo' );
}
public function testContextReplacement ()
{
$logger = $this -> getLogger ();
$logger -> info ( '{Message {nothing} {user} {foo.bar} a}' , array ( 'user' => 'Bob' , 'foo.bar' => 'Bar' ));
$expected = array ( 'info {Message {nothing} Bob Bar a}' );
$this -> assertEquals ( $expected , $this -> getLogs ());
}
public function testObjectCastToString ()
{
2017-04-13 15:53:35 +01:00
if ( method_exists ( $this , 'createPartialMock' )) {
$dummy = $this -> createPartialMock ( 'Psr\Log\Test\DummyTest' , array ( '__toString' ));
} else {
$dummy = $this -> getMock ( 'Psr\Log\Test\DummyTest' , array ( '__toString' ));
}
2015-08-17 17:00:26 -07:00
$dummy -> expects ( $this -> once ())
-> method ( '__toString' )
-> will ( $this -> returnValue ( 'DUMMY' ));
$this -> getLogger () -> warning ( $dummy );
2017-04-13 15:53:35 +01:00
$expected = array ( 'warning DUMMY' );
$this -> assertEquals ( $expected , $this -> getLogs ());
2015-08-17 17:00:26 -07:00
}
public function testContextCanContainAnything ()
{
2018-11-23 12:29:20 +00:00
$closed = fopen ( 'php://memory' , 'r' );
fclose ( $closed );
2015-08-17 17:00:26 -07:00
$context = array (
'bool' => true ,
'null' => null ,
'string' => 'Foo' ,
'int' => 0 ,
'float' => 0.5 ,
'nested' => array ( 'with object' => new DummyTest ),
'object' => new \DateTime ,
'resource' => fopen ( 'php://memory' , 'r' ),
2018-11-23 12:29:20 +00:00
'closed' => $closed ,
2015-08-17 17:00:26 -07:00
);
$this -> getLogger () -> warning ( 'Crazy context data' , $context );
2017-04-13 15:53:35 +01:00
$expected = array ( 'warning Crazy context data' );
$this -> assertEquals ( $expected , $this -> getLogs ());
2015-08-17 17:00:26 -07:00
}
public function testContextExceptionKeyCanBeExceptionOrOtherValues ()
{
2017-04-13 15:53:35 +01:00
$logger = $this -> getLogger ();
$logger -> warning ( 'Random message' , array ( 'exception' => 'oops' ));
$logger -> critical ( 'Uncaught Exception!' , array ( 'exception' => new \LogicException ( 'Fail' )));
$expected = array (
'warning Random message' ,
'critical Uncaught Exception!'
);
$this -> assertEquals ( $expected , $this -> getLogs ());
2015-08-17 17:00:26 -07:00
}
}
class DummyTest
{
2017-04-13 15:53:35 +01:00
public function __toString ()
{
}
}