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