SyslogUdpHandlerTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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", 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->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. protected function getRecordWithMessage($msg)
  63. {
  64. return ['message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => [], 'channel' => 'lol'];
  65. }
  66. }