2
0

SocketHandlerTest.php 9.4 KB

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