SyslogUdpHandlerTest.php 3.9 KB

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