菜谱项目

LoggerInterfaceTest.php 4.5KB

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