UdpSocketTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. use Monolog\Handler\SyslogUdp\UdpSocket;
  13. use Monolog\Util\LocalSocket;
  14. /**
  15. * @requires extension sockets
  16. */
  17. class UdpSocketTest extends TestCase
  18. {
  19. public function testWeDoNotTruncateShortMessages()
  20. {
  21. $this->initSocket();
  22. $socket = new UdpSocket('127.0.0.1', 51983);
  23. $socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
  24. $this->assertEquals('HEADER: The quick brown fox jumps over the lazy dog', $this->socket->getOutput());
  25. }
  26. public function testLongMessagesAreTruncated()
  27. {
  28. $this->initSocket();
  29. $socket = new UdpSocket('127.0.0.1', 51983);
  30. $longString = str_repeat("derp", 20000);
  31. $socket->write($longString, "HEADER");
  32. $truncatedString = str_repeat("derp", 16254).'d';
  33. $this->assertEquals('HEADER'.$truncatedString, $this->socket->getOutput());
  34. }
  35. public function testDoubleCloseDoesNotError()
  36. {
  37. $socket = new UdpSocket('127.0.0.1', 514);
  38. $socket->close();
  39. $socket->close();
  40. }
  41. /**
  42. * @expectedException LogicException
  43. */
  44. public function testWriteAfterCloseErrors()
  45. {
  46. $socket = new UdpSocket('127.0.0.1', 514);
  47. $socket->close();
  48. $socket->write('foo', "HEADER");
  49. }
  50. private function initSocket()
  51. {
  52. $this->socket = LocalSocket::initSocket(51983, LocalSocket::UDP);
  53. }
  54. public function tearDown()
  55. {
  56. unset($this->socket, $this->handler);
  57. }
  58. }