SyslogUdpHandlerTest.php 3.6 KB

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