SyslogUdpHandlerTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. protected function getRecordWithMessage($msg)
  57. {
  58. return array('message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => array(), 'channel' => 'lol');
  59. }
  60. }