SocketHandlerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  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 testSetConnectionString()
  69. {
  70. $this->createHandler('tcp://localhost:9090');
  71. $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
  72. }
  73. /**
  74. * @expectedException UnexpectedValueException
  75. */
  76. public function testExceptionIsThrownOnFsockopenError()
  77. {
  78. $this->setMockHandler(array('fsockopen'));
  79. $this->handler->expects($this->once())
  80. ->method('fsockopen')
  81. ->will($this->returnValue(false));
  82. $this->writeRecord('Hello world');
  83. }
  84. /**
  85. * @expectedException UnexpectedValueException
  86. */
  87. public function testExceptionIsThrownOnPfsockopenError()
  88. {
  89. $this->setMockHandler(array('pfsockopen'));
  90. $this->handler->expects($this->once())
  91. ->method('pfsockopen')
  92. ->will($this->returnValue(false));
  93. $this->handler->setPersistent(true);
  94. $this->writeRecord('Hello world');
  95. }
  96. /**
  97. * @expectedException UnexpectedValueException
  98. */
  99. public function testExceptionIsThrownIfCannotSetTimeout()
  100. {
  101. $this->setMockHandler(array('streamSetTimeout'));
  102. $this->handler->expects($this->once())
  103. ->method('streamSetTimeout')
  104. ->will($this->returnValue(false));
  105. $this->writeRecord('Hello world');
  106. }
  107. /**
  108. * @expectedException RuntimeException
  109. */
  110. public function testWriteFailsOnIfFwriteReturnsFalse()
  111. {
  112. $this->setMockHandler(array('fwrite'));
  113. $callback = function ($arg) {
  114. $map = array(
  115. 'Hello world' => 6,
  116. 'world' => false,
  117. );
  118. return $map[$arg];
  119. };
  120. $this->handler->expects($this->exactly(2))
  121. ->method('fwrite')
  122. ->will($this->returnCallback($callback));
  123. $this->writeRecord('Hello world');
  124. }
  125. /**
  126. * @expectedException RuntimeException
  127. */
  128. public function testWriteFailsIfStreamTimesOut()
  129. {
  130. $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
  131. $callback = function ($arg) {
  132. $map = array(
  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(array('timed_out' => true)));
  144. $this->writeRecord('Hello world');
  145. }
  146. /**
  147. * @expectedException RuntimeException
  148. */
  149. public function testWriteFailsOnIncompleteWrite()
  150. {
  151. $this->setMockHandler(array('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. ->will($this->returnCallback($callback));
  160. $this->handler->expects($this->exactly(1))
  161. ->method('streamGetMetadata')
  162. ->will($this->returnValue(array('timed_out' => false)));
  163. $this->writeRecord('Hello world');
  164. }
  165. public function testWriteWithMemoryFile()
  166. {
  167. $this->setMockHandler();
  168. $this->writeRecord('test1');
  169. $this->writeRecord('test2');
  170. $this->writeRecord('test3');
  171. fseek($this->res, 0);
  172. $this->assertEquals('test1test2test3', fread($this->res, 1024));
  173. }
  174. public function testWriteWithMock()
  175. {
  176. $this->setMockHandler(array('fwrite'));
  177. $callback = function ($arg) {
  178. $map = array(
  179. 'Hello world' => 6,
  180. 'world' => 5,
  181. );
  182. return $map[$arg];
  183. };
  184. $this->handler->expects($this->exactly(2))
  185. ->method('fwrite')
  186. ->will($this->returnCallback($callback));
  187. $this->writeRecord('Hello world');
  188. }
  189. public function testClose()
  190. {
  191. $this->setMockHandler();
  192. $this->writeRecord('Hello world');
  193. $this->assertInternalType('resource', $this->res);
  194. $this->handler->close();
  195. $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
  196. }
  197. public function testCloseDoesNotClosePersistentSocket()
  198. {
  199. $this->setMockHandler();
  200. $this->handler->setPersistent(true);
  201. $this->writeRecord('Hello world');
  202. $this->assertTrue(is_resource($this->res));
  203. $this->handler->close();
  204. $this->assertTrue(is_resource($this->res));
  205. }
  206. /**
  207. * @expectedException \RuntimeException
  208. */
  209. public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
  210. {
  211. $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
  212. $this->handler->expects($this->any())
  213. ->method('fwrite')
  214. ->will($this->returnValue(0));
  215. $this->handler->expects($this->any())
  216. ->method('streamGetMetadata')
  217. ->will($this->returnValue(array('timed_out' => false)));
  218. $this->handler->setWritingTimeout(1);
  219. $this->writeRecord('Hello world');
  220. }
  221. private function createHandler($connectionString)
  222. {
  223. $this->handler = new SocketHandler($connectionString);
  224. $this->handler->setFormatter($this->getIdentityFormatter());
  225. }
  226. private function writeRecord($string)
  227. {
  228. $this->handler->handle($this->getRecord(Logger::WARNING, $string));
  229. }
  230. private function setMockHandler(array $methods = array())
  231. {
  232. $this->res = fopen('php://memory', 'a');
  233. $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
  234. $newMethods = array_diff($methods, $defaultMethods);
  235. $finalMethods = array_merge($defaultMethods, $newMethods);
  236. $this->handler = $this->getMock(
  237. '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
  238. );
  239. if (!in_array('fsockopen', $methods)) {
  240. $this->handler->expects($this->any())
  241. ->method('fsockopen')
  242. ->will($this->returnValue($this->res));
  243. }
  244. if (!in_array('pfsockopen', $methods)) {
  245. $this->handler->expects($this->any())
  246. ->method('pfsockopen')
  247. ->will($this->returnValue($this->res));
  248. }
  249. if (!in_array('streamSetTimeout', $methods)) {
  250. $this->handler->expects($this->any())
  251. ->method('streamSetTimeout')
  252. ->will($this->returnValue(true));
  253. }
  254. $this->handler->setFormatter($this->getIdentityFormatter());
  255. }
  256. }