SocketHandlerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $this->createHandler('garbage://here');
  33. $this->writeRecord('data');
  34. }
  35. /**
  36. * @expectedException \InvalidArgumentException
  37. */
  38. public function testBadConnectionTimeout()
  39. {
  40. $this->createHandler('localhost:1234');
  41. $this->handler->setConnectionTimeout(-1);
  42. }
  43. public function testSetConnectionTimeout()
  44. {
  45. $this->createHandler('localhost:1234');
  46. $this->handler->setConnectionTimeout(10);
  47. $this->assertEquals(10, $this->handler->getConnectionTimeout());
  48. }
  49. /**
  50. * @expectedException \InvalidArgumentException
  51. */
  52. public function testBadTimeout()
  53. {
  54. $this->createHandler('localhost:1234');
  55. $this->handler->setTimeout(-1);
  56. }
  57. public function testSetTimeout()
  58. {
  59. $this->createHandler('localhost:1234');
  60. $this->handler->setTimeout(10);
  61. $this->assertEquals(10, $this->handler->getTimeout());
  62. }
  63. public function testSetConnectionString()
  64. {
  65. $this->createHandler('tcp://localhost:9090');
  66. $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
  67. }
  68. public function testConnectionRefuesed()
  69. {
  70. try {
  71. $this->createHandler('127.0.0.1:7894');
  72. $string = 'Hello world';
  73. $this->writeRecord($string);
  74. $this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?");
  75. } catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) {
  76. }
  77. }
  78. /**
  79. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  80. */
  81. public function testConnectionTimeoutWithMock()
  82. {
  83. $this->setMockHandler(array('createSocketResource'));
  84. $this->handler->expects($this->once())
  85. ->method('createSocketResource')
  86. ->will($this->throwException(new ConnectionException()));
  87. $this->writeRecord('Hello world');
  88. }
  89. /**
  90. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  91. */
  92. public function testWriteFailsOnIfFwriteReturnsFalse()
  93. {
  94. $this->setMockHandler(array('fwrite'));
  95. $map = array(
  96. array('Hello world', 6),
  97. array('world', false),
  98. );
  99. $this->handler->expects($this->exactly(2))
  100. ->method('fwrite')
  101. ->will($this->returnValueMap($map));
  102. $this->injectMemoryResource();
  103. $this->writeRecord('Hello world');
  104. }
  105. /**
  106. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  107. */
  108. public function testWriteFailsIfStreamTimesOut()
  109. {
  110. $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
  111. $map = array(
  112. array('Hello world', 6),
  113. array('world', 5),
  114. );
  115. $this->handler->expects($this->exactly(1))
  116. ->method('fwrite')
  117. ->will($this->returnValueMap($map));
  118. $this->handler->expects($this->exactly(1))
  119. ->method('stream_get_meta_data')
  120. ->will($this->returnValue(array('timed_out' => true)));
  121. $this->injectMemoryResource();
  122. $this->writeRecord('Hello world');
  123. }
  124. /**
  125. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  126. */
  127. public function testWriteFailsOnIncompleteWrite()
  128. {
  129. $this->setMockHandler(array('fwrite', 'isConnected'));
  130. $map = array(
  131. array('Hello world', 6),
  132. array('world', 5),
  133. );
  134. $this->handler->expects($this->exactly(1))
  135. ->method('fwrite')
  136. ->will($this->returnValueMap($map));
  137. $this->handler->expects($this->at(0))
  138. ->method('isConnected')
  139. ->will($this->returnValue(true));
  140. $this->handler->expects($this->at(1))
  141. ->method('isConnected')
  142. ->will($this->returnValue(true));
  143. $this->handler->expects($this->at(2))
  144. ->method('isConnected')
  145. ->will($this->returnValue(false));
  146. $this->injectMemoryResource();
  147. $this->writeRecord('Hello world');
  148. }
  149. public function testWriteWithMemoryFile()
  150. {
  151. $this->createHandler('localhost:54321');
  152. $this->injectMemoryResource();
  153. $this->writeRecord('test1');
  154. $this->writeRecord('test2');
  155. $this->writeRecord('test3');
  156. fseek($this->res, 0);
  157. $this->assertEquals('test1test2test3', fread($this->res, 1024));
  158. }
  159. public function testWriteWithMock()
  160. {
  161. $this->setMockHandler(array('fwrite'));
  162. $map = array(
  163. array('Hello world', 6),
  164. array('world', 5),
  165. );
  166. $this->handler->expects($this->exactly(2))
  167. ->method('fwrite')
  168. ->will($this->returnValueMap($map));
  169. $this->injectMemoryResource();
  170. $this->writeRecord('Hello world');
  171. }
  172. public function testClose()
  173. {
  174. $this->createHandler('localhost:54321');
  175. $this->injectMemoryResource();
  176. $this->writeRecord('Hello world');
  177. $this->assertTrue(is_resource($this->res));
  178. $this->handler->close();
  179. $this->assertFalse(is_resource($this->res));
  180. }
  181. public function testCloseDoesNotClosePersistentSocket()
  182. {
  183. $this->createHandler('localhost:54321');
  184. $this->handler->setPersistent(true);
  185. $this->injectMemoryResource();
  186. $this->writeRecord('Hello world');
  187. $this->assertTrue(is_resource($this->res));
  188. $this->handler->close();
  189. $this->assertTrue(is_resource($this->res));
  190. }
  191. /**
  192. * @expectedException \InvalidArgumentException
  193. */
  194. public function testInjectBadResourceThrowsException()
  195. {
  196. $this->createHandler('');
  197. $this->handler->setResource('');
  198. }
  199. private function createHandler($connectionString)
  200. {
  201. $this->handler = new SocketHandler($connectionString);
  202. $this->handler->setFormatter($this->getIdentityFormatter());
  203. }
  204. private function writeRecord($string)
  205. {
  206. $this->handler->handle($this->getRecord(Logger::WARNING, $string));
  207. }
  208. private function injectMemoryResource()
  209. {
  210. $this->res = fopen('php://memory', 'a');
  211. $this->handler->setResource($this->res);
  212. }
  213. private function setMockHandler(array $methods)
  214. {
  215. $this->handler = $this->getMock(
  216. '\Monolog\Handler\SocketHandler',
  217. $methods,
  218. array('localhost:1234')
  219. );
  220. $this->handler->setFormatter($this->getIdentityFormatter());
  221. }
  222. }