SyslogUdpHandlerTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
  27. $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
  28. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  29. ->setMethods(['write'])
  30. ->setConstructorArgs(['lol', 'lol'])
  31. ->getMock();
  32. $socket->expects($this->at(0))
  33. ->method('write')
  34. ->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 ");
  35. $socket->expects($this->at(1))
  36. ->method('write')
  37. ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 ");
  38. $handler->setSocket($socket);
  39. $handler->handle($this->getRecordWithMessage("hej\nlol"));
  40. }
  41. public function testSplitWorksOnEmptyMsg()
  42. {
  43. $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
  44. $handler->setFormatter($this->getIdentityFormatter());
  45. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  46. ->setMethods(['write'])
  47. ->setConstructorArgs(['lol', 'lol'])
  48. ->getMock();
  49. $socket->expects($this->never())
  50. ->method('write');
  51. $handler->setSocket($socket);
  52. $handler->handle($this->getRecordWithMessage(null));
  53. }
  54. protected function getRecordWithMessage($msg)
  55. {
  56. return ['message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => [], 'channel' => 'lol'];
  57. }
  58. }