SocketHandlerTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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\Level;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. /**
  15. * @author Pablo de Leon Belloc <pablolb@gmail.com>
  16. */
  17. class SocketHandlerTest extends TestCase
  18. {
  19. private SocketHandler&MockObject $handler;
  20. /**
  21. * @var resource
  22. */
  23. private $res;
  24. public function testInvalidHostname()
  25. {
  26. $this->expectException(\UnexpectedValueException::class);
  27. $handler = $this->createHandler('garbage://here');
  28. $handler->handle($this->getRecord(Level::Warning, 'data'));
  29. }
  30. public function testBadConnectionTimeout()
  31. {
  32. $this->expectException(\InvalidArgumentException::class);
  33. $handler = $this->createHandler('localhost:1234');
  34. $handler->setConnectionTimeout(-1);
  35. }
  36. public function testSetConnectionTimeout()
  37. {
  38. $handler = $this->createHandler('localhost:1234');
  39. $handler->setConnectionTimeout(10.1);
  40. $this->assertEquals(10.1, $handler->getConnectionTimeout());
  41. }
  42. public function testBadTimeout()
  43. {
  44. $this->expectException(\InvalidArgumentException::class);
  45. $handler = $this->createHandler('localhost:1234');
  46. $handler->setTimeout(-1);
  47. }
  48. public function testSetTimeout()
  49. {
  50. $handler = $this->createHandler('localhost:1234');
  51. $handler->setTimeout(10.25);
  52. $this->assertEquals(10.25, $handler->getTimeout());
  53. }
  54. public function testSetWritingTimeout()
  55. {
  56. $handler = $this->createHandler('localhost:1234');
  57. $handler->setWritingTimeout(10.25);
  58. $this->assertEquals(10.25, $handler->getWritingTimeout());
  59. }
  60. public function testSetChunkSize()
  61. {
  62. $handler = $this->createHandler('localhost:1234');
  63. $handler->setChunkSize(1025);
  64. $this->assertEquals(1025, $handler->getChunkSize());
  65. }
  66. public function testSetConnectionString()
  67. {
  68. $handler = $this->createHandler('tcp://localhost:9090');
  69. $this->assertEquals('tcp://localhost:9090', $handler->getConnectionString());
  70. }
  71. public function testExceptionIsThrownOnFsockopenError()
  72. {
  73. $this->setMockHandler(['fsockopen']);
  74. $this->handler->expects($this->once())
  75. ->method('fsockopen')
  76. ->will($this->returnValue(false));
  77. $this->expectException(\UnexpectedValueException::class);
  78. $this->writeRecord('Hello world');
  79. }
  80. public function testExceptionIsThrownOnPfsockopenError()
  81. {
  82. $this->setMockHandler(['pfsockopen']);
  83. $this->handler->expects($this->once())
  84. ->method('pfsockopen')
  85. ->will($this->returnValue(false));
  86. $this->handler->setPersistent(true);
  87. $this->expectException(\UnexpectedValueException::class);
  88. $this->writeRecord('Hello world');
  89. }
  90. public function testExceptionIsThrownIfCannotSetTimeout()
  91. {
  92. $this->setMockHandler(['streamSetTimeout']);
  93. $this->handler->expects($this->once())
  94. ->method('streamSetTimeout')
  95. ->will($this->returnValue(false));
  96. $this->expectException(\UnexpectedValueException::class);
  97. $this->writeRecord('Hello world');
  98. }
  99. public function testExceptionIsThrownIfCannotSetChunkSize()
  100. {
  101. $this->setMockHandler(['streamSetChunkSize']);
  102. $this->handler->setChunkSize(8192);
  103. $this->handler->expects($this->once())
  104. ->method('streamSetChunkSize')
  105. ->will($this->returnValue(false));
  106. $this->expectException(\UnexpectedValueException::class);
  107. $this->writeRecord('Hello world');
  108. }
  109. public function testWriteFailsOnIfFwriteReturnsFalse()
  110. {
  111. $this->setMockHandler(['fwrite']);
  112. $callback = function ($arg) {
  113. $map = [
  114. 'Hello world' => 6,
  115. 'world' => false,
  116. ];
  117. return $map[$arg];
  118. };
  119. $this->handler->expects($this->exactly(2))
  120. ->method('fwrite')
  121. ->will($this->returnCallback($callback));
  122. $this->expectException(\RuntimeException::class);
  123. $this->writeRecord('Hello world');
  124. }
  125. public function testWriteFailsIfStreamTimesOut()
  126. {
  127. $this->setMockHandler(['fwrite', 'streamGetMetadata']);
  128. $callback = function ($arg) {
  129. $map = [
  130. 'Hello world' => 6,
  131. 'world' => 5,
  132. ];
  133. return $map[$arg];
  134. };
  135. $this->handler->expects($this->exactly(1))
  136. ->method('fwrite')
  137. ->will($this->returnCallback($callback));
  138. $this->handler->expects($this->exactly(1))
  139. ->method('streamGetMetadata')
  140. ->will($this->returnValue(['timed_out' => true]));
  141. $this->expectException(\RuntimeException::class);
  142. $this->writeRecord('Hello world');
  143. }
  144. public function testWriteFailsOnIncompleteWrite()
  145. {
  146. $this->setMockHandler(['fwrite', 'streamGetMetadata']);
  147. $res = $this->res;
  148. $callback = function ($string) use ($res) {
  149. fclose($res);
  150. return strlen('Hello');
  151. };
  152. $this->handler->expects($this->exactly(1))
  153. ->method('fwrite')
  154. ->will($this->returnCallback($callback));
  155. $this->handler->expects($this->exactly(1))
  156. ->method('streamGetMetadata')
  157. ->will($this->returnValue(['timed_out' => false]));
  158. $this->expectException(\RuntimeException::class);
  159. $this->writeRecord('Hello world');
  160. }
  161. public function testWriteWithMemoryFile()
  162. {
  163. $this->setMockHandler();
  164. $this->writeRecord('test1');
  165. $this->writeRecord('test2');
  166. $this->writeRecord('test3');
  167. fseek($this->res, 0);
  168. $this->assertEquals('test1test2test3', fread($this->res, 1024));
  169. }
  170. public function testWriteWithMock()
  171. {
  172. $this->setMockHandler(['fwrite']);
  173. $callback = function ($arg) {
  174. $map = [
  175. 'Hello world' => 6,
  176. 'world' => 5,
  177. ];
  178. return $map[$arg];
  179. };
  180. $this->handler->expects($this->exactly(2))
  181. ->method('fwrite')
  182. ->will($this->returnCallback($callback));
  183. $this->writeRecord('Hello world');
  184. }
  185. public function testClose()
  186. {
  187. $this->setMockHandler();
  188. $this->writeRecord('Hello world');
  189. $this->assertIsResource($this->res);
  190. $this->handler->close();
  191. $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
  192. }
  193. public function testCloseDoesNotClosePersistentSocket()
  194. {
  195. $this->setMockHandler();
  196. $this->handler->setPersistent(true);
  197. $this->writeRecord('Hello world');
  198. $this->assertTrue(is_resource($this->res));
  199. $this->handler->close();
  200. $this->assertTrue(is_resource($this->res));
  201. }
  202. public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
  203. {
  204. $this->setMockHandler(['fwrite', 'streamGetMetadata']);
  205. $this->handler->expects($this->any())
  206. ->method('fwrite')
  207. ->will($this->returnValue(0));
  208. $this->handler->expects($this->any())
  209. ->method('streamGetMetadata')
  210. ->will($this->returnValue(['timed_out' => false]));
  211. $this->handler->setWritingTimeout(1);
  212. $this->expectException(\RuntimeException::class);
  213. $this->writeRecord('Hello world');
  214. }
  215. private function createHandler(string $connectionString): SocketHandler
  216. {
  217. $handler = new SocketHandler($connectionString);
  218. $handler->setFormatter($this->getIdentityFormatter());
  219. return $handler;
  220. }
  221. private function writeRecord($string)
  222. {
  223. $this->handler->handle($this->getRecord(Level::Warning, $string));
  224. }
  225. private function setMockHandler(array $methods = [])
  226. {
  227. $this->res = fopen('php://memory', 'a');
  228. $defaultMethods = ['fsockopen', 'pfsockopen', 'streamSetTimeout', 'streamSetChunkSize'];
  229. $newMethods = array_diff($methods, $defaultMethods);
  230. $finalMethods = array_merge($defaultMethods, $newMethods);
  231. $this->handler = $this->getMockBuilder('Monolog\Handler\SocketHandler')
  232. ->onlyMethods($finalMethods)
  233. ->setConstructorArgs(['localhost:1234'])
  234. ->getMock();
  235. if (!in_array('fsockopen', $methods)) {
  236. $this->handler->expects($this->any())
  237. ->method('fsockopen')
  238. ->will($this->returnValue($this->res));
  239. }
  240. if (!in_array('pfsockopen', $methods)) {
  241. $this->handler->expects($this->any())
  242. ->method('pfsockopen')
  243. ->will($this->returnValue($this->res));
  244. }
  245. if (!in_array('streamSetTimeout', $methods)) {
  246. $this->handler->expects($this->any())
  247. ->method('streamSetTimeout')
  248. ->will($this->returnValue(true));
  249. }
  250. if (!in_array('streamSetChunkSize', $methods)) {
  251. $this->handler->expects($this->any())
  252. ->method('streamSetChunkSize')
  253. ->will($this->returnValue(8192));
  254. }
  255. $this->handler->setFormatter($this->getIdentityFormatter());
  256. }
  257. }