SocketHandlerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Logger;
  13. use Monolog\Util\LocalSocket;
  14. /**
  15. * @author Pablo de Leon Belloc <pablolb@gmail.com>
  16. */
  17. class SocketHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var Monolog\Handler\SocketHandler
  21. */
  22. private $handler;
  23. /**
  24. * @var resource
  25. */
  26. private $res;
  27. /**
  28. * @expectedException UnexpectedValueException
  29. */
  30. public function testInvalidHostname()
  31. {
  32. $this->createHandler('garbage://here');
  33. $this->writeRecord('data');
  34. }
  35. /**
  36. * @expectedException \InvalidArgumentException
  37. */
  38. public function testBadConnectionTimeout()
  39. {
  40. $this->createHandler('localhost:1234');
  41. $this->handler->setConnectionTimeout(-1);
  42. }
  43. public function testSetConnectionTimeout()
  44. {
  45. $this->createHandler('localhost:1234');
  46. $this->handler->setConnectionTimeout(10.1);
  47. $this->assertEquals(10.1, $this->handler->getConnectionTimeout());
  48. }
  49. /**
  50. * @expectedException \InvalidArgumentException
  51. */
  52. public function testBadTimeout()
  53. {
  54. $this->createHandler('localhost:1234');
  55. $this->handler->setTimeout(-1);
  56. }
  57. public function testSetTimeout()
  58. {
  59. $this->createHandler('localhost:1234');
  60. $this->handler->setTimeout(10.25);
  61. $this->assertEquals(10.25, $this->handler->getTimeout());
  62. }
  63. public function testSetWritingTimeout()
  64. {
  65. $this->createHandler('localhost:1234');
  66. $this->handler->setWritingTimeout(10.25);
  67. $this->assertEquals(10.25, $this->handler->getWritingTimeout());
  68. }
  69. public function testSetConnectionString()
  70. {
  71. $this->createHandler('tcp://localhost:9090');
  72. $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
  73. }
  74. /**
  75. * @expectedException UnexpectedValueException
  76. */
  77. public function testExceptionIsThrownOnFsockopenError()
  78. {
  79. $this->createHandler('tcp://127.0.0.1:51985');
  80. $this->writeRecord('Hello world');
  81. }
  82. /**
  83. * @expectedException UnexpectedValueException
  84. */
  85. public function testExceptionIsThrownOnPfsockopenError()
  86. {
  87. $this->createHandler('tcp://127.0.0.1:51985');
  88. $this->handler->setPersistent(true);
  89. $this->writeRecord('Hello world');
  90. }
  91. /**
  92. * @expectedException RuntimeException
  93. */
  94. public function testWriteFailsOnIfFwriteReturnsFalse()
  95. {
  96. $this->initHandlerAndSocket();
  97. $this->writeRecord('Hello world');
  98. LocalSocket::shutdownSocket();
  99. $this->writeRecord('Hello world2');
  100. }
  101. public function testWriteRealSocket()
  102. {
  103. $this->initHandlerAndSocket();
  104. $this->writeRecord("foo bar baz content test1\n");
  105. $this->writeRecord("foo bar baz content test2\n");
  106. $this->writeRecord("foo bar baz content test3\n");
  107. $this->assertEquals("foo bar baz content test1\nfoo bar baz content test2\nfoo bar baz content test3\n", $this->socket->getOutput());
  108. }
  109. public function testClose()
  110. {
  111. $this->initHandlerAndSocket();
  112. $this->writeRecord('Hello world');
  113. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'resource');
  114. $reflectionProperty->setAccessible(true);
  115. $this->assertInternalType('resource', $reflectionProperty->getValue($this->handler));
  116. $this->handler->close();
  117. $this->assertFalse(is_resource($reflectionProperty->getValue($this->handler)), "Expected resource to be closed after closing handler");
  118. }
  119. public function testCloseDoesNotClosePersistentSocket()
  120. {
  121. $this->initHandlerAndSocket();
  122. $this->handler->setPersistent(true);
  123. $this->writeRecord('Hello world');
  124. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'resource');
  125. $reflectionProperty->setAccessible(true);
  126. $this->assertTrue(is_resource($reflectionProperty->getValue($this->handler)));
  127. $this->handler->close();
  128. $this->assertTrue(is_resource($reflectionProperty->getValue($this->handler)));
  129. }
  130. private function createHandler($connectionString)
  131. {
  132. $this->handler = new SocketHandler($connectionString);
  133. $this->handler->setFormatter($this->getIdentityFormatter());
  134. }
  135. private function writeRecord($string)
  136. {
  137. $this->handler->handle($this->getRecord(Logger::WARNING, $string));
  138. }
  139. private function initHandlerAndSocket()
  140. {
  141. $this->socket = LocalSocket::initSocket();
  142. $this->handler = new SocketHandler('tcp://127.0.0.1:51984');
  143. $this->handler->setFormatter($this->getIdentityFormatter());
  144. }
  145. public function tearDown()
  146. {
  147. unset($this->socket, $this->handler);
  148. }
  149. }