LoggerInterfaceTest.php 4.5 KB

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