TestHandlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @covers Monolog\Handler\TestHandler
  15. */
  16. class TestHandlerTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider methodProvider
  20. */
  21. public function testHandler($method, $level)
  22. {
  23. $handler = new TestHandler;
  24. $record = $this->getRecord($level, 'test'.$method);
  25. $this->assertFalse($handler->hasRecords($level));
  26. $this->assertFalse($handler->hasRecord($record, $level));
  27. $this->assertFalse($handler->{'has'.$method}($record), 'has'.$method);
  28. $this->assertFalse($handler->{'has'.$method.'ThatContains'}('test'), 'has'.$method.'ThatContains');
  29. $this->assertFalse($handler->{'has'.$method.'ThatPasses'}(function ($rec) {
  30. return true;
  31. }), 'has'.$method.'ThatPasses');
  32. $this->assertFalse($handler->{'has'.$method.'ThatMatches'}('/test\w+/'));
  33. $this->assertFalse($handler->{'has'.$method.'Records'}(), 'has'.$method.'Records');
  34. $handler->handle($record);
  35. $this->assertFalse($handler->{'has'.$method}('bar'), 'has'.$method);
  36. $this->assertTrue($handler->hasRecords($level));
  37. $this->assertTrue($handler->hasRecord($record, $level));
  38. $this->assertTrue($handler->{'has'.$method}($record), 'has'.$method);
  39. $this->assertTrue($handler->{'has'.$method}('test'.$method), 'has'.$method);
  40. $this->assertTrue($handler->{'has'.$method.'ThatContains'}('test'), 'has'.$method.'ThatContains');
  41. $this->assertTrue($handler->{'has'.$method.'ThatPasses'}(function ($rec) {
  42. return true;
  43. }), 'has'.$method.'ThatPasses');
  44. $this->assertTrue($handler->{'has'.$method.'ThatMatches'}('/test\w+/'));
  45. $this->assertTrue($handler->{'has'.$method.'Records'}(), 'has'.$method.'Records');
  46. $records = $handler->getRecords();
  47. unset($records[0]['formatted']);
  48. $this->assertEquals(array($record), $records);
  49. }
  50. public function testHandlerAssertEmptyContext() {
  51. $handler = new TestHandler;
  52. $record = $this->getRecord(Logger::WARNING, 'test', array());
  53. $this->assertFalse($handler->hasWarning(array(
  54. 'message' => 'test',
  55. 'context' => array(),
  56. )));
  57. $handler->handle($record);
  58. $this->assertTrue($handler->hasWarning(array(
  59. 'message' => 'test',
  60. 'context' => array(),
  61. )));
  62. $this->assertFalse($handler->hasWarning(array(
  63. 'message' => 'test',
  64. 'context' => array(
  65. 'foo' => 'bar'
  66. ),
  67. )));
  68. }
  69. public function testHandlerAssertNonEmptyContext() {
  70. $handler = new TestHandler;
  71. $record = $this->getRecord(Logger::WARNING, 'test', array('foo' => 'bar'));
  72. $this->assertFalse($handler->hasWarning(array(
  73. 'message' => 'test',
  74. 'context' => array(
  75. 'foo' => 'bar'
  76. ),
  77. )));
  78. $handler->handle($record);
  79. $this->assertTrue($handler->hasWarning(array(
  80. 'message' => 'test',
  81. 'context' => array(
  82. 'foo' => 'bar'
  83. ),
  84. )));
  85. $this->assertFalse($handler->hasWarning(array(
  86. 'message' => 'test',
  87. 'context' => array(),
  88. )));
  89. }
  90. public function methodProvider()
  91. {
  92. return array(
  93. array('Emergency', Logger::EMERGENCY),
  94. array('Alert' , Logger::ALERT),
  95. array('Critical' , Logger::CRITICAL),
  96. array('Error' , Logger::ERROR),
  97. array('Warning' , Logger::WARNING),
  98. array('Info' , Logger::INFO),
  99. array('Notice' , Logger::NOTICE),
  100. array('Debug' , Logger::DEBUG),
  101. );
  102. }
  103. }