SocketHandlerTest.php 9.4 KB

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