LoggerTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog;
  11. use Monolog\Processor\WebProcessor;
  12. use Monolog\Handler\TestHandler;
  13. class LoggerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers Monolog\Logger::getName()
  17. */
  18. public function testGetName()
  19. {
  20. $logger = new Logger('foo');
  21. $this->assertEquals('foo', $logger->getName());
  22. }
  23. /**
  24. * @covers Monolog\Logger::__construct
  25. */
  26. public function testChannel()
  27. {
  28. $logger = new Logger('foo');
  29. $handler = new TestHandler;
  30. $logger->pushHandler($handler);
  31. $logger->addWarning('test');
  32. list($record) = $handler->getRecords();
  33. $this->assertEquals('foo', $record['channel']);
  34. }
  35. /**
  36. * @covers Monolog\Logger::addRecord
  37. */
  38. public function testLog()
  39. {
  40. $logger = new Logger(__METHOD__);
  41. $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
  42. $handler->expects($this->once())
  43. ->method('handle');
  44. $logger->pushHandler($handler);
  45. $this->assertTrue($logger->addWarning('test'));
  46. }
  47. /**
  48. * @covers Monolog\Logger::addRecord
  49. */
  50. public function testLogNotHandled()
  51. {
  52. $logger = new Logger(__METHOD__);
  53. $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'), array(Logger::ERROR));
  54. $handler->expects($this->never())
  55. ->method('handle');
  56. $logger->pushHandler($handler);
  57. $this->assertFalse($logger->addWarning('test'));
  58. }
  59. /**
  60. * @covers Monolog\Logger::pushHandler
  61. * @covers Monolog\Logger::popHandler
  62. * @expectedException LogicException
  63. */
  64. public function testPushPopHandler()
  65. {
  66. $logger = new Logger(__METHOD__);
  67. $handler1 = new TestHandler;
  68. $handler2 = new TestHandler;
  69. $logger->pushHandler($handler1);
  70. $logger->pushHandler($handler2);
  71. $this->assertEquals($handler2, $logger->popHandler());
  72. $this->assertEquals($handler1, $logger->popHandler());
  73. $logger->popHandler();
  74. }
  75. /**
  76. * @covers Monolog\Logger::pushProcessor
  77. * @covers Monolog\Logger::popProcessor
  78. * @expectedException LogicException
  79. */
  80. public function testPushPopProcessor()
  81. {
  82. $logger = new Logger(__METHOD__);
  83. $processor1 = new WebProcessor;
  84. $processor2 = new WebProcessor;
  85. $logger->pushProcessor($processor1);
  86. $logger->pushProcessor($processor2);
  87. $this->assertEquals($processor2, $logger->popProcessor());
  88. $this->assertEquals($processor1, $logger->popProcessor());
  89. $logger->popProcessor();
  90. }
  91. /**
  92. * @covers Monolog\Logger::addRecord
  93. */
  94. public function testProcessorsAreExecuted()
  95. {
  96. $logger = new Logger(__METHOD__);
  97. $handler = new TestHandler;
  98. $logger->pushHandler($handler);
  99. $logger->pushProcessor(function($record) {
  100. $record['extra']['win'] = true;
  101. return $record;
  102. });
  103. $logger->addError('test');
  104. list($record) = $handler->getRecords();
  105. $this->assertTrue($record['extra']['win']);
  106. }
  107. /**
  108. * @dataProvider logMethodProvider
  109. * @covers Monolog\Logger::addDebug
  110. * @covers Monolog\Logger::addInfo
  111. * @covers Monolog\Logger::addWarning
  112. * @covers Monolog\Logger::addError
  113. * @covers Monolog\Logger::addCritical
  114. * @covers Monolog\Logger::addAlert
  115. * @covers Monolog\Logger::debug
  116. * @covers Monolog\Logger::info
  117. * @covers Monolog\Logger::notice
  118. * @covers Monolog\Logger::warn
  119. * @covers Monolog\Logger::err
  120. * @covers Monolog\Logger::crit
  121. * @covers Monolog\Logger::alert
  122. * @covers Monolog\Logger::emerg
  123. */
  124. public function testLogMethods($method, $expectedLevel)
  125. {
  126. $logger = new Logger('foo');
  127. $handler = new TestHandler;
  128. $logger->pushHandler($handler);
  129. $logger->{$method}('test');
  130. list($record) = $handler->getRecords();
  131. $this->assertEquals($expectedLevel, $record['level']);
  132. }
  133. public function logMethodProvider()
  134. {
  135. return array(
  136. // monolog methods
  137. array('addDebug', Logger::DEBUG),
  138. array('addInfo', Logger::INFO),
  139. array('addWarning', Logger::WARNING),
  140. array('addError', Logger::ERROR),
  141. array('addCritical', Logger::CRITICAL),
  142. array('addAlert', Logger::ALERT),
  143. // ZF/Sf2 compat methods
  144. array('debug', Logger::DEBUG),
  145. array('info', Logger::INFO),
  146. array('notice', Logger::INFO),
  147. array('warn', Logger::WARNING),
  148. array('err', Logger::ERROR),
  149. array('crit', Logger::CRITICAL),
  150. array('alert', Logger::ALERT),
  151. array('emerg', Logger::ALERT),
  152. );
  153. }
  154. }