SyslogUdpHandlerTest.php 3.5 KB

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