TestHandlerTest.php 3.9 KB

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