SyslogUdpHandlerTest.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 Monolog\Test\TestCase;
  13. /**
  14. * @requires extension sockets
  15. */
  16. class SyslogUdpHandlerTest extends TestCase
  17. {
  18. public function testWeValidateFacilities()
  19. {
  20. $this->expectException(\UnexpectedValueException::class);
  21. $handler = new SyslogUdpHandler("ip", 514, "invalidFacility");
  22. }
  23. public function testWeSplitIntoLines()
  24. {
  25. $pid = getmypid();
  26. $host = gethostname();
  27. $handler = new \Monolog\Handler\SyslogUdpHandler("127.0.0.1", 514, "authpriv");
  28. $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
  29. $time = '2014-01-07T12:34:56+00:00';
  30. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  31. ->onlyMethods(['write'])
  32. ->setConstructorArgs(['lol'])
  33. ->getMock();
  34. $matcher = $this->atLeast(2);
  35. $socket->expects($matcher)
  36. ->method('write')
  37. ->willReturnCallback(function () use ($matcher, $time, $host, $pid) {
  38. match ($matcher->numberOfInvocations()) {
  39. 1 => $this->equalTo("lol") && $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - "),
  40. 2 => $this->equalTo("hej") && $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - "),
  41. default => $this->assertTrue(true)
  42. };
  43. });
  44. $handler->setSocket($socket);
  45. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  46. }
  47. public function testSplitWorksOnEmptyMsg()
  48. {
  49. $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
  50. $handler->setFormatter($this->getIdentityFormatter());
  51. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  52. ->onlyMethods(['write'])
  53. ->setConstructorArgs(['lol'])
  54. ->getMock();
  55. $socket->expects($this->never())
  56. ->method('write');
  57. $handler->setSocket($socket);
  58. $handler->handle($this->getRecordWithMessage(''));
  59. }
  60. public function testRfc()
  61. {
  62. $time = 'Jan 07 12:34:56';
  63. $pid = getmypid();
  64. $host = gethostname();
  65. $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler')
  66. ->setConstructorArgs(["127.0.0.1", 514, "authpriv", 'debug', true, "php", \Monolog\Handler\SyslogUdpHandler::RFC3164])
  67. ->onlyMethods([])
  68. ->getMock();
  69. $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
  70. $socket = $this->getMockBuilder('\Monolog\Handler\SyslogUdp\UdpSocket')
  71. ->setConstructorArgs(['lol', 999])
  72. ->onlyMethods(['write'])
  73. ->getMock();
  74. $matcher = $this->atLeast(2);
  75. $socket->expects($matcher)
  76. ->method('write')
  77. ->willReturnCallback(function () use ($matcher, $time, $host, $pid) {
  78. match ($matcher->numberOfInvocations()) {
  79. 1 => $this->equalTo("lol") && $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: "),
  80. 2 => $this->equalTo("hej") && $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: "),
  81. default => $this->assertTrue(true)
  82. };
  83. });
  84. $handler->setSocket($socket);
  85. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  86. }
  87. protected function getRecordWithMessage($msg)
  88. {
  89. return $this->getRecord(message: $msg, level: Level::Warning, channel: 'lol', datetime: new \DateTimeImmutable('2014-01-07 12:34:56'));
  90. }
  91. }