2
0

SocketHandlerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. use Monolog\Handler\SocketHandler\Exception\ConnectionException;
  14. use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
  15. /**
  16. * @author Pablo de Leon Belloc <pablolb@gmail.com>
  17. */
  18. class SocketHandlerTest extends TestCase
  19. {
  20. /**
  21. * @var Monolog\Handler\SocketHandler
  22. */
  23. private $handler;
  24. /**
  25. * @var resource
  26. */
  27. private $res;
  28. /**
  29. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  30. */
  31. public function testInvalidHostname()
  32. {
  33. $this->createHandler('garbage://here');
  34. $this->writeRecord('data');
  35. }
  36. /**
  37. * @expectedException \InvalidArgumentException
  38. */
  39. public function testBadConnectionTimeout()
  40. {
  41. $this->createHandler('localhost:1234');
  42. $this->handler->setConnectionTimeout(-1);
  43. }
  44. public function testSetConnectionTimeout()
  45. {
  46. $this->createHandler('localhost:1234');
  47. $this->handler->setConnectionTimeout(10);
  48. $this->assertEquals(10, $this->handler->getConnectionTimeout());
  49. }
  50. /**
  51. * @expectedException \InvalidArgumentException
  52. */
  53. public function testBadTimeout()
  54. {
  55. $this->createHandler('localhost:1234');
  56. $this->handler->setTimeout(-1);
  57. }
  58. public function testSetTimeout()
  59. {
  60. $this->createHandler('localhost:1234');
  61. $this->handler->setTimeout(10);
  62. $this->assertEquals(10, $this->handler->getTimeout());
  63. }
  64. public function testSetConnectionString()
  65. {
  66. $this->createHandler('tcp://localhost:9090');
  67. $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
  68. }
  69. public function testConnectionRefuesed()
  70. {
  71. try {
  72. $this->createHandler('127.0.0.1:7894');
  73. $string = 'Hello world';
  74. $this->writeRecord($string);
  75. $this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?");
  76. } catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) {
  77. }
  78. }
  79. /**
  80. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  81. */
  82. public function testExceptionIsThrownOnFsockopenError()
  83. {
  84. $this->setMockHandler(array('fsockopen'));
  85. $this->handler->expects($this->once())
  86. ->method('fsockopen')
  87. ->will($this->returnValue(false));
  88. $this->writeRecord('Hello world');
  89. }
  90. /**
  91. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  92. */
  93. public function testExceptionIsThrownOnPfsockopenError()
  94. {
  95. $this->setMockHandler(array('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 Monolog\Handler\SocketHandler\Exception\ConnectionException
  104. */
  105. public function testExceptionIsThrownIfCannotSetTimeout()
  106. {
  107. $this->setMockHandler(array('stream_set_timeout'));
  108. $this->handler->expects($this->once())
  109. ->method('stream_set_timeout')
  110. ->will($this->returnValue(false));
  111. $this->writeRecord('Hello world');
  112. }
  113. /**
  114. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  115. */
  116. public function testWriteFailsOnIfFwriteReturnsFalse()
  117. {
  118. $this->setMockHandler(array('fwrite'));
  119. $map = array(
  120. array('Hello world', 6),
  121. array('world', false),
  122. );
  123. $this->handler->expects($this->exactly(2))
  124. ->method('fwrite')
  125. ->will($this->returnValueMap($map));
  126. $this->writeRecord('Hello world');
  127. }
  128. /**
  129. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  130. */
  131. public function testWriteFailsIfStreamTimesOut()
  132. {
  133. $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
  134. $map = array(
  135. array('Hello world', 6),
  136. array('world', 5),
  137. );
  138. $this->handler->expects($this->exactly(1))
  139. ->method('fwrite')
  140. ->will($this->returnValueMap($map));
  141. $this->handler->expects($this->exactly(1))
  142. ->method('stream_get_meta_data')
  143. ->will($this->returnValue(array('timed_out' => true)));
  144. $this->writeRecord('Hello world');
  145. }
  146. /**
  147. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  148. */
  149. public function testWriteFailsOnIncompleteWrite()
  150. {
  151. $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
  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('stream_get_meta_data')
  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. $map = array(
  178. array('Hello world', 6),
  179. array('world', 5),
  180. );
  181. $this->handler->expects($this->exactly(2))
  182. ->method('fwrite')
  183. ->will($this->returnValueMap($map));
  184. $this->writeRecord('Hello world');
  185. }
  186. public function testClose()
  187. {
  188. $this->setMockHandler();
  189. $this->writeRecord('Hello world');
  190. $this->assertTrue(is_resource($this->res));
  191. $this->handler->close();
  192. $this->assertFalse(is_resource($this->res));
  193. }
  194. public function testCloseDoesNotClosePersistentSocket()
  195. {
  196. $this->setMockHandler();
  197. $this->handler->setPersistent(true);
  198. $this->writeRecord('Hello world');
  199. $this->assertTrue(is_resource($this->res));
  200. $this->handler->close();
  201. $this->assertTrue(is_resource($this->res));
  202. }
  203. private function createHandler($connectionString)
  204. {
  205. $this->handler = new SocketHandler($connectionString);
  206. $this->handler->setFormatter($this->getIdentityFormatter());
  207. }
  208. private function writeRecord($string)
  209. {
  210. $this->handler->handle($this->getRecord(Logger::WARNING, $string));
  211. }
  212. private function setMockHandler(array $methods = array())
  213. {
  214. $this->res = fopen('php://memory', 'a');
  215. $defaultMethods = array('fsockopen', 'pfsockopen', 'stream_set_timeout');
  216. $newMethods = array_diff($methods, $defaultMethods);
  217. $finalMethods = array_merge($defaultMethods, $newMethods);
  218. $this->handler = $this->getMock(
  219. '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
  220. );
  221. if (!in_array('fsockopen', $methods)) {
  222. $this->handler->expects($this->any())
  223. ->method('fsockopen')
  224. ->will($this->returnValue($this->res));
  225. }
  226. if (!in_array('pfsockopen', $methods)) {
  227. $this->handler->expects($this->any())
  228. ->method('pfsockopen')
  229. ->will($this->returnValue($this->res));
  230. }
  231. if (!in_array('stream_set_timeout', $methods)) {
  232. $this->handler->expects($this->any())
  233. ->method('stream_set_timeout')
  234. ->will($this->returnValue(true));
  235. }
  236. $this->handler->setFormatter($this->getIdentityFormatter());
  237. }
  238. }