SocketHandlerTest.php 9.3 KB

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