SyslogUdpHandlerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  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\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", null, "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->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol'));
  37. $socket->expects($this->at(0))
  38. ->method('write')
  39. ->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ");
  40. $socket->expects($this->at(1))
  41. ->method('write')
  42. ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ");
  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->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol'));
  51. $socket->expects($this->never())
  52. ->method('write');
  53. $handler->setSocket($socket);
  54. $handler->handle($this->getRecordWithMessage(null));
  55. }
  56. public function testRfc()
  57. {
  58. $time = 'Mar 22 21:16:47';
  59. $pid = getmypid();
  60. $host = gethostname();
  61. $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler')
  62. ->setConstructorArgs(array("127.0.0.1", 514, "authpriv", null, null, "php", \Monolog\Handler\SyslogUdpHandler::RFC3164))
  63. ->setMethods(array('getDateTime'))
  64. ->getMock();
  65. $handler->method('getDateTime')
  66. ->willReturn($time);
  67. $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
  68. $socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol'));
  69. $socket->expects($this->at(0))
  70. ->method('write')
  71. ->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ");
  72. $socket->expects($this->at(1))
  73. ->method('write')
  74. ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: ");
  75. $handler->setSocket($socket);
  76. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  77. }
  78. protected function getRecordWithMessage($msg)
  79. {
  80. return array('message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => array(), 'channel' => 'lol');
  81. }
  82. }