SyslogUdpHandlerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $socket->expects($this->atLeast(2))
  35. ->method('write')
  36. ->withConsecutive(
  37. [$this->equalTo("lol"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ")],
  38. [$this->equalTo("hej"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ")],
  39. );
  40. $handler->setSocket($socket);
  41. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  42. }
  43. public function testSplitWorksOnEmptyMsg()
  44. {
  45. $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
  46. $handler->setFormatter($this->getIdentityFormatter());
  47. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  48. ->onlyMethods(['write'])
  49. ->setConstructorArgs(['lol'])
  50. ->getMock();
  51. $socket->expects($this->never())
  52. ->method('write');
  53. $handler->setSocket($socket);
  54. $handler->handle($this->getRecordWithMessage(''));
  55. }
  56. public function testRfc()
  57. {
  58. $time = 'Jan 07 12:34:56';
  59. $pid = getmypid();
  60. $host = gethostname();
  61. $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler')
  62. ->setConstructorArgs(["127.0.0.1", 514, "authpriv", 'debug', true, "php", \Monolog\Handler\SyslogUdpHandler::RFC3164])
  63. ->onlyMethods([])
  64. ->getMock();
  65. $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
  66. $socket = $this->getMockBuilder('\Monolog\Handler\SyslogUdp\UdpSocket')
  67. ->setConstructorArgs(['lol', 999])
  68. ->onlyMethods(['write'])
  69. ->getMock();
  70. $socket->expects($this->atLeast(2))
  71. ->method('write')
  72. ->withConsecutive(
  73. [$this->equalTo("lol"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ")],
  74. [$this->equalTo("hej"), $this->equalTo("<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ")],
  75. );
  76. $handler->setSocket($socket);
  77. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  78. }
  79. protected function getRecordWithMessage($msg)
  80. {
  81. return $this->getRecord(message: $msg, level: Level::Warning, channel: 'lol', datetime: new \DateTimeImmutable('2014-01-07 12:34:56'));
  82. }
  83. }