2
0

SocketHandlerTest.php 9.4 KB

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