LoggerTest.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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::pushProcessor
  93. * @expectedException InvalidArgumentException
  94. */
  95. public function testPushProcessorWithNonCallable()
  96. {
  97. $logger = new Logger(__METHOD__);
  98. $logger->pushProcessor(new \stdClass());
  99. }
  100. /**
  101. * @covers Monolog\Logger::addRecord
  102. */
  103. public function testProcessorsAreExecuted()
  104. {
  105. $logger = new Logger(__METHOD__);
  106. $handler = new TestHandler;
  107. $logger->pushHandler($handler);
  108. $logger->pushProcessor(function($record) {
  109. $record['extra']['win'] = true;
  110. return $record;
  111. });
  112. $logger->addError('test');
  113. list($record) = $handler->getRecords();
  114. $this->assertTrue($record['extra']['win']);
  115. }
  116. /**
  117. * @covers Monolog\Logger::addRecord
  118. */
  119. public function testProcessorsAreCalledOnlyOnce()
  120. {
  121. $logger = new Logger(__METHOD__);
  122. $handler = $this->getMock('Monolog\Handler\HandlerInterface');
  123. $handler->expects($this->any())
  124. ->method('isHandling')
  125. ->will($this->returnValue(true))
  126. ;
  127. $handler->expects($this->any())
  128. ->method('handle')
  129. ->will($this->returnValue(true))
  130. ;
  131. $logger->pushHandler($handler);
  132. $processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
  133. ->disableOriginalConstructor()
  134. ->setMethods(array('__invoke'))
  135. ->getMock()
  136. ;
  137. $processor->expects($this->once())
  138. ->method('__invoke')
  139. ->will($this->returnArgument(0))
  140. ;
  141. $logger->pushProcessor($processor);
  142. $logger->addError('test');
  143. }
  144. /**
  145. * @covers Monolog\Logger::addRecord
  146. */
  147. public function testProcessorsNotCalledWhenNotHandled()
  148. {
  149. $logger = new Logger(__METHOD__);
  150. $handler = $this->getMock('Monolog\Handler\HandlerInterface');
  151. $handler->expects($this->once())
  152. ->method('isHandling')
  153. ->will($this->returnValue(false))
  154. ;
  155. $logger->pushHandler($handler);
  156. $that = $this;
  157. $logger->pushProcessor(function($record) use ($that){
  158. $that->fail('The processor should not be called');
  159. });
  160. $logger->addAlert('test');
  161. }
  162. /**
  163. * @covers Monolog\Logger::addRecord
  164. */
  165. public function testHandlersNotCalledBeforeFirstHandling()
  166. {
  167. $logger = new Logger(__METHOD__);
  168. $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
  169. $handler1->expects($this->never())
  170. ->method('isHandling')
  171. ->will($this->returnValue(false))
  172. ;
  173. $handler1->expects($this->once())
  174. ->method('handle')
  175. ->will($this->returnValue(false))
  176. ;
  177. $logger->pushHandler($handler1);
  178. $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
  179. $handler2->expects($this->once())
  180. ->method('isHandling')
  181. ->will($this->returnValue(true))
  182. ;
  183. $handler2->expects($this->once())
  184. ->method('handle')
  185. ->will($this->returnValue(false))
  186. ;
  187. $logger->pushHandler($handler2);
  188. $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
  189. $handler3->expects($this->once())
  190. ->method('isHandling')
  191. ->will($this->returnValue(false))
  192. ;
  193. $handler3->expects($this->never())
  194. ->method('handle')
  195. ;
  196. $logger->pushHandler($handler3);
  197. $logger->debug('test');
  198. }
  199. /**
  200. * @covers Monolog\Logger::addRecord
  201. */
  202. public function testBubblingWhenTheHandlerReturnsFalse()
  203. {
  204. $logger = new Logger(__METHOD__);
  205. $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
  206. $handler1->expects($this->any())
  207. ->method('isHandling')
  208. ->will($this->returnValue(true))
  209. ;
  210. $handler1->expects($this->once())
  211. ->method('handle')
  212. ->will($this->returnValue(false))
  213. ;
  214. $logger->pushHandler($handler1);
  215. $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
  216. $handler2->expects($this->any())
  217. ->method('isHandling')
  218. ->will($this->returnValue(true))
  219. ;
  220. $handler2->expects($this->once())
  221. ->method('handle')
  222. ->will($this->returnValue(false))
  223. ;
  224. $logger->pushHandler($handler2);
  225. $logger->debug('test');
  226. }
  227. /**
  228. * @covers Monolog\Logger::addRecord
  229. */
  230. public function testNotBubblingWhenTheHandlerReturnsTrue()
  231. {
  232. $logger = new Logger(__METHOD__);
  233. $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
  234. $handler1->expects($this->any())
  235. ->method('isHandling')
  236. ->will($this->returnValue(true))
  237. ;
  238. $handler1->expects($this->never())
  239. ->method('handle')
  240. ;
  241. $logger->pushHandler($handler1);
  242. $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
  243. $handler2->expects($this->any())
  244. ->method('isHandling')
  245. ->will($this->returnValue(true))
  246. ;
  247. $handler2->expects($this->once())
  248. ->method('handle')
  249. ->will($this->returnValue(true))
  250. ;
  251. $logger->pushHandler($handler2);
  252. $logger->debug('test');
  253. }
  254. /**
  255. * @dataProvider logMethodProvider
  256. * @covers Monolog\Logger::addDebug
  257. * @covers Monolog\Logger::addInfo
  258. * @covers Monolog\Logger::addWarning
  259. * @covers Monolog\Logger::addError
  260. * @covers Monolog\Logger::addCritical
  261. * @covers Monolog\Logger::addAlert
  262. * @covers Monolog\Logger::debug
  263. * @covers Monolog\Logger::info
  264. * @covers Monolog\Logger::notice
  265. * @covers Monolog\Logger::warn
  266. * @covers Monolog\Logger::err
  267. * @covers Monolog\Logger::crit
  268. * @covers Monolog\Logger::alert
  269. * @covers Monolog\Logger::emerg
  270. */
  271. public function testLogMethods($method, $expectedLevel)
  272. {
  273. $logger = new Logger('foo');
  274. $handler = new TestHandler;
  275. $logger->pushHandler($handler);
  276. $logger->{$method}('test');
  277. list($record) = $handler->getRecords();
  278. $this->assertEquals($expectedLevel, $record['level']);
  279. }
  280. public function logMethodProvider()
  281. {
  282. return array(
  283. // monolog methods
  284. array('addDebug', Logger::DEBUG),
  285. array('addInfo', Logger::INFO),
  286. array('addWarning', Logger::WARNING),
  287. array('addError', Logger::ERROR),
  288. array('addCritical', Logger::CRITICAL),
  289. array('addAlert', Logger::ALERT),
  290. // ZF/Sf2 compat methods
  291. array('debug', Logger::DEBUG),
  292. array('info', Logger::INFO),
  293. array('notice', Logger::INFO),
  294. array('warn', Logger::WARNING),
  295. array('err', Logger::ERROR),
  296. array('crit', Logger::CRITICAL),
  297. array('alert', Logger::ALERT),
  298. array('emerg', Logger::ALERT),
  299. );
  300. }
  301. }