UdpSocketTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Handler\SyslogUdp\UdpSocket;
  12. /**
  13. * @requires extension sockets
  14. */
  15. class UdpSocketTest extends \Monolog\Test\MonologTestCase
  16. {
  17. public function testWeDoNotTruncateShortMessages()
  18. {
  19. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  20. ->onlyMethods(['send'])
  21. ->setConstructorArgs(['lol'])
  22. ->getMock();
  23. $socket
  24. ->method('send')
  25. ->with("HEADER: The quick brown fox jumps over the lazy dog");
  26. $socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
  27. }
  28. public function testLongMessagesAreTruncated()
  29. {
  30. $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
  31. ->onlyMethods(['send'])
  32. ->setConstructorArgs(['lol'])
  33. ->getMock();
  34. $truncatedString = str_repeat("derp", 16254).'d';
  35. $socket->expects($this->exactly(1))
  36. ->method('send')
  37. ->with("HEADER" . $truncatedString);
  38. $longString = str_repeat("derp", 20000);
  39. $socket->write($longString, "HEADER");
  40. }
  41. public function testDoubleCloseDoesNotError()
  42. {
  43. $socket = new UdpSocket('127.0.0.1', 514);
  44. $socket->close();
  45. $socket->close();
  46. }
  47. public function testWriteAfterCloseReopened()
  48. {
  49. $socket = new UdpSocket('127.0.0.1', 514);
  50. $socket->close();
  51. $socket->write('foo', "HEADER");
  52. }
  53. }