SocketHandlerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 testConnectionTimeoutWithMock()
  83. {
  84. $this->setMockHandler(array('createSocketResource'));
  85. $this->handler->expects($this->once())
  86. ->method('createSocketResource')
  87. ->will($this->throwException(new ConnectionException()));
  88. $this->writeRecord('Hello world');
  89. }
  90. /**
  91. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  92. */
  93. public function testWriteFailsOnIfFwriteReturnsFalse()
  94. {
  95. $this->setMockHandler(array('fwrite'));
  96. $map = array(
  97. array('Hello world', 6),
  98. array('world', false),
  99. );
  100. $this->handler->expects($this->exactly(2))
  101. ->method('fwrite')
  102. ->will($this->returnValueMap($map));
  103. $this->injectMemoryResource();
  104. $this->writeRecord('Hello world');
  105. }
  106. /**
  107. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  108. */
  109. public function testWriteFailsIfStreamTimesOut()
  110. {
  111. $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
  112. $map = array(
  113. array('Hello world', 6),
  114. array('world', 5),
  115. );
  116. $this->handler->expects($this->exactly(1))
  117. ->method('fwrite')
  118. ->will($this->returnValueMap($map));
  119. $this->handler->expects($this->exactly(1))
  120. ->method('stream_get_meta_data')
  121. ->will($this->returnValue(array('timed_out' => true)));
  122. $this->injectMemoryResource();
  123. $this->writeRecord('Hello world');
  124. }
  125. /**
  126. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  127. */
  128. public function testWriteFailsOnIncompleteWrite()
  129. {
  130. $this->setMockHandler(array('fwrite', 'isConnected'));
  131. $map = array(
  132. array('Hello world', 6),
  133. array('world', 5),
  134. );
  135. $this->handler->expects($this->exactly(1))
  136. ->method('fwrite')
  137. ->will($this->returnValueMap($map));
  138. $this->handler->expects($this->at(0))
  139. ->method('isConnected')
  140. ->will($this->returnValue(true));
  141. $this->handler->expects($this->at(1))
  142. ->method('isConnected')
  143. ->will($this->returnValue(true));
  144. $this->handler->expects($this->at(2))
  145. ->method('isConnected')
  146. ->will($this->returnValue(false));
  147. $this->injectMemoryResource();
  148. $this->writeRecord('Hello world');
  149. }
  150. public function testWriteWithMemoryFile()
  151. {
  152. $this->createHandler('localhost:54321');
  153. $this->injectMemoryResource();
  154. $this->writeRecord('test1');
  155. $this->writeRecord('test2');
  156. $this->writeRecord('test3');
  157. fseek($this->res, 0);
  158. $this->assertEquals('test1test2test3', fread($this->res, 1024));
  159. }
  160. public function testWriteWithMock()
  161. {
  162. $this->setMockHandler(array('fwrite'));
  163. $map = array(
  164. array('Hello world', 6),
  165. array('world', 5),
  166. );
  167. $this->handler->expects($this->exactly(2))
  168. ->method('fwrite')
  169. ->will($this->returnValueMap($map));
  170. $this->injectMemoryResource();
  171. $this->writeRecord('Hello world');
  172. }
  173. public function testClose()
  174. {
  175. $this->createHandler('localhost:54321');
  176. $this->injectMemoryResource();
  177. $this->writeRecord('Hello world');
  178. $this->assertTrue(is_resource($this->res));
  179. $this->handler->close();
  180. $this->assertFalse(is_resource($this->res));
  181. }
  182. public function testCloseDoesNotClosePersistentSocket()
  183. {
  184. $this->createHandler('localhost:54321');
  185. $this->handler->setPersistent(true);
  186. $this->injectMemoryResource();
  187. $this->writeRecord('Hello world');
  188. $this->assertTrue(is_resource($this->res));
  189. $this->handler->close();
  190. $this->assertTrue(is_resource($this->res));
  191. }
  192. /**
  193. * @expectedException \InvalidArgumentException
  194. */
  195. public function testInjectBadResourceThrowsException()
  196. {
  197. $this->createHandler('');
  198. $this->handler->setResource('');
  199. }
  200. private function createHandler($connectionString)
  201. {
  202. $this->handler = new SocketHandler($connectionString);
  203. $this->handler->setFormatter($this->getIdentityFormatter());
  204. }
  205. private function writeRecord($string)
  206. {
  207. $this->handler->handle($this->getRecord(Logger::WARNING, $string));
  208. }
  209. private function injectMemoryResource()
  210. {
  211. $this->res = fopen('php://memory', 'a');
  212. $this->handler->setResource($this->res);
  213. }
  214. private function setMockHandler(array $methods)
  215. {
  216. $this->handler = $this->getMock(
  217. '\Monolog\Handler\SocketHandler', $methods, array('localhost:1234')
  218. );
  219. $this->handler->setFormatter($this->getIdentityFormatter());
  220. }
  221. }