LoggerInterfaceTest.php 3.8 KB

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