TestHandlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php declare(strict_types=1);
  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\Level;
  12. use Monolog\Test\TestCase;
  13. /**
  14. * @covers Monolog\Handler\TestHandler
  15. */
  16. class TestHandlerTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider methodProvider
  20. */
  21. public function testHandler($method, Level $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->message, $level));
  27. $this->assertFalse($handler->{'has'.$method}($record->message), '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->message, $level));
  38. $this->assertTrue($handler->{'has'.$method}($record->message), '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. $records[0]->formatted = null;
  48. $this->assertEquals([$record], $records);
  49. }
  50. public function testHandlerAssertEmptyContext()
  51. {
  52. $handler = new TestHandler;
  53. $record = $this->getRecord(Level::Warning, 'test', []);
  54. $this->assertFalse($handler->hasWarning([
  55. 'message' => 'test',
  56. 'context' => [],
  57. ]));
  58. $handler->handle($record);
  59. $this->assertTrue($handler->hasWarning([
  60. 'message' => 'test',
  61. 'context' => [],
  62. ]));
  63. $this->assertFalse($handler->hasWarning([
  64. 'message' => 'test',
  65. 'context' => [
  66. 'foo' => 'bar',
  67. ],
  68. ]));
  69. }
  70. public function testHandlerAssertNonEmptyContext()
  71. {
  72. $handler = new TestHandler;
  73. $record = $this->getRecord(Level::Warning, 'test', ['foo' => 'bar']);
  74. $this->assertFalse($handler->hasWarning([
  75. 'message' => 'test',
  76. 'context' => [
  77. 'foo' => 'bar',
  78. ],
  79. ]));
  80. $handler->handle($record);
  81. $this->assertTrue($handler->hasWarning([
  82. 'message' => 'test',
  83. 'context' => [
  84. 'foo' => 'bar',
  85. ],
  86. ]));
  87. $this->assertFalse($handler->hasWarning([
  88. 'message' => 'test',
  89. 'context' => [],
  90. ]));
  91. }
  92. public static function methodProvider()
  93. {
  94. return [
  95. ['Emergency', Level::Emergency],
  96. ['Alert' , Level::Alert],
  97. ['Critical' , Level::Critical],
  98. ['Error' , Level::Error],
  99. ['Warning' , Level::Warning],
  100. ['Info' , Level::Info],
  101. ['Notice' , Level::Notice],
  102. ['Debug' , Level::Debug],
  103. ];
  104. }
  105. }