2
0

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