SocketHandlerTest.php 7.7 KB

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