SocketHandlerTest.php 8.1 KB

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