TestHandlerTest.php 3.9 KB

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