PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/monolog/monolog/tests/Monolog/Handler/SocketHandlerTest.php

https://gitlab.com/judielsm/Handora
PHP | 282 lines | 191 code | 47 blank | 44 comment | 3 complexity | cf8f5201637ec782a0bad6cf73127ba4 MD5 | raw file
  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.1);
  46. $this->assertEquals(10.1, $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.25);
  60. $this->assertEquals(10.25, $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('streamSetTimeout'));
  96. $this->handler->expects($this->once())
  97. ->method('streamSetTimeout')
  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. $map = array(
  109. 'Hello world' => 6,
  110. 'world' => false,
  111. );
  112. return $map[$arg];
  113. };
  114. $this->handler->expects($this->exactly(2))
  115. ->method('fwrite')
  116. ->will($this->returnCallback($callback));
  117. $this->writeRecord('Hello world');
  118. }
  119. /**
  120. * @expectedException RuntimeException
  121. */
  122. public function testWriteFailsIfStreamTimesOut()
  123. {
  124. $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
  125. $callback = function ($arg) {
  126. $map = array(
  127. 'Hello world' => 6,
  128. 'world' => 5,
  129. );
  130. return $map[$arg];
  131. };
  132. $this->handler->expects($this->exactly(1))
  133. ->method('fwrite')
  134. ->will($this->returnCallback($callback));
  135. $this->handler->expects($this->exactly(1))
  136. ->method('streamGetMetadata')
  137. ->will($this->returnValue(array('timed_out' => true)));
  138. $this->writeRecord('Hello world');
  139. }
  140. /**
  141. * @expectedException RuntimeException
  142. */
  143. public function testWriteFailsOnIncompleteWrite()
  144. {
  145. $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
  146. $res = $this->res;
  147. $callback = function ($string) use ($res) {
  148. fclose($res);
  149. return strlen('Hello');
  150. };
  151. $this->handler->expects($this->exactly(1))
  152. ->method('fwrite')
  153. ->will($this->returnCallback($callback));
  154. $this->handler->expects($this->exactly(1))
  155. ->method('streamGetMetadata')
  156. ->will($this->returnValue(array('timed_out' => false)));
  157. $this->writeRecord('Hello world');
  158. }
  159. public function testWriteWithMemoryFile()
  160. {
  161. $this->setMockHandler();
  162. $this->writeRecord('test1');
  163. $this->writeRecord('test2');
  164. $this->writeRecord('test3');
  165. fseek($this->res, 0);
  166. $this->assertEquals('test1test2test3', fread($this->res, 1024));
  167. }
  168. public function testWriteWithMock()
  169. {
  170. $this->setMockHandler(array('fwrite'));
  171. $callback = function ($arg) {
  172. $map = array(
  173. 'Hello world' => 6,
  174. 'world' => 5,
  175. );
  176. return $map[$arg];
  177. };
  178. $this->handler->expects($this->exactly(2))
  179. ->method('fwrite')
  180. ->will($this->returnCallback($callback));
  181. $this->writeRecord('Hello world');
  182. }
  183. public function testClose()
  184. {
  185. $this->setMockHandler();
  186. $this->writeRecord('Hello world');
  187. $this->assertInternalType('resource', $this->res);
  188. $this->handler->close();
  189. $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
  190. }
  191. public function testCloseDoesNotClosePersistentSocket()
  192. {
  193. $this->setMockHandler();
  194. $this->handler->setPersistent(true);
  195. $this->writeRecord('Hello world');
  196. $this->assertTrue(is_resource($this->res));
  197. $this->handler->close();
  198. $this->assertTrue(is_resource($this->res));
  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 setMockHandler(array $methods = array())
  210. {
  211. $this->res = fopen('php://memory', 'a');
  212. $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
  213. $newMethods = array_diff($methods, $defaultMethods);
  214. $finalMethods = array_merge($defaultMethods, $newMethods);
  215. $this->handler = $this->getMock(
  216. '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
  217. );
  218. if (!in_array('fsockopen', $methods)) {
  219. $this->handler->expects($this->any())
  220. ->method('fsockopen')
  221. ->will($this->returnValue($this->res));
  222. }
  223. if (!in_array('pfsockopen', $methods)) {
  224. $this->handler->expects($this->any())
  225. ->method('pfsockopen')
  226. ->will($this->returnValue($this->res));
  227. }
  228. if (!in_array('streamSetTimeout', $methods)) {
  229. $this->handler->expects($this->any())
  230. ->method('streamSetTimeout')
  231. ->will($this->returnValue(true));
  232. }
  233. $this->handler->setFormatter($this->getIdentityFormatter());
  234. }
  235. }