HipChatHandlerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @author Rafael Dohms <rafael@doh.ms>
  15. * @see https://www.hipchat.com/docs/api
  16. */
  17. class HipChatHandlerTest extends TestCase
  18. {
  19. private $res;
  20. /** @var HipChatHandler */
  21. private $handler;
  22. public function testWriteV2()
  23. {
  24. $this->initHandlerAndSocket('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  25. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  26. $content = $this->socket->getOutput();
  27. $this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  28. return $content;
  29. }
  30. public function testWriteV2Notify()
  31. {
  32. $this->initHandlerAndSocket('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
  33. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  34. $content = $this->socket->getOutput();
  35. $this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  36. return $content;
  37. }
  38. public function testRoomSpaces()
  39. {
  40. $this->initHandlerAndSocket('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  41. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  42. $content = $this->socket->getOutput();
  43. $this->assertRegexp('/POST \/v2\/room\/room%20name\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  44. return $content;
  45. }
  46. /**
  47. * @depends testWriteHeader
  48. */
  49. public function testWriteContent($content)
  50. {
  51. $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
  52. }
  53. /**
  54. * @depends testWriteCustomHostHeader
  55. */
  56. public function testWriteContentNotify($content)
  57. {
  58. $this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
  59. }
  60. /**
  61. * @depends testWriteV2
  62. */
  63. public function testWriteContentV2($content)
  64. {
  65. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  66. }
  67. /**
  68. * @depends testWriteV2Notify
  69. */
  70. public function testWriteContentV2Notify($content)
  71. {
  72. $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  73. }
  74. public function testWriteContentV2WithoutName()
  75. {
  76. $this->initHandlerAndSocket('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
  77. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  78. $content = $this->socket->getOutput();
  79. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
  80. return $content;
  81. }
  82. public function testWriteWithComplexMessage()
  83. {
  84. $this->initHandlerAndSocket();
  85. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  86. $content = $this->socket->getOutput();
  87. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  88. }
  89. public function testWriteTruncatesLongMessage()
  90. {
  91. $this->initHandlerAndSocket();
  92. $this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
  93. $content = $this->socket->getOutput();
  94. $this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
  95. }
  96. /**
  97. * @dataProvider provideLevelColors
  98. */
  99. public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
  100. {
  101. $this->initHandlerAndSocket();
  102. $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
  103. $content = $this->socket->getOutput();
  104. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  105. }
  106. public function provideLevelColors()
  107. {
  108. return [
  109. [Logger::DEBUG, 'gray'],
  110. [Logger::INFO, 'green'],
  111. [Logger::WARNING, 'yellow'],
  112. [Logger::ERROR, 'red'],
  113. [Logger::CRITICAL, 'red'],
  114. [Logger::ALERT, 'red'],
  115. [Logger::EMERGENCY,'red'],
  116. [Logger::NOTICE, 'green'],
  117. ];
  118. }
  119. /**
  120. * @dataProvider provideBatchRecords
  121. */
  122. public function testHandleBatch($records, $expectedColor)
  123. {
  124. $this->initHandlerAndSocket();
  125. $this->handler->handleBatch($records);
  126. $content = $this->socket->getOutput();
  127. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  128. }
  129. public function provideBatchRecords()
  130. {
  131. return [
  132. [
  133. [
  134. ['level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTimeImmutable()],
  135. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  136. ['level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTimeImmutable()],
  137. ],
  138. 'red',
  139. ],
  140. [
  141. [
  142. ['level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTimeImmutable()],
  143. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  144. ],
  145. 'yellow',
  146. ],
  147. [
  148. [
  149. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  150. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  151. ],
  152. 'green',
  153. ],
  154. [
  155. [
  156. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  157. ],
  158. 'gray',
  159. ],
  160. ];
  161. }
  162. public function testCreateWithTooLongNameV2()
  163. {
  164. // creating a handler with too long of a name but using the v2 api doesn't matter.
  165. $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
  166. }
  167. private function initHandlerAndSocket($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
  168. {
  169. $tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
  170. file_put_contents($tmpFile, <<<'SCRIPT'
  171. <?php
  172. $sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  173. socket_bind($sock, '127.0.0.1', 51984);
  174. socket_listen($sock);
  175. while (true) {
  176. $res = socket_accept($sock);
  177. socket_set_option($res, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 500));
  178. while ($read = socket_read($res, 1024)) {
  179. echo $read;
  180. }
  181. socket_close($res);
  182. }
  183. SCRIPT
  184. );
  185. $this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
  186. $this->socket->start();
  187. $this->handler = new HipChatHandler($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
  188. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  189. $reflectionProperty->setAccessible(true);
  190. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  191. $this->handler->setFormatter($this->getIdentityFormatter());
  192. }
  193. private function closeSocket()
  194. {
  195. $this->socket->stop();
  196. }
  197. public function tearDown()
  198. {
  199. if (isset($this->socket)) {
  200. $this->closeSocket();
  201. unset($this->socket);
  202. }
  203. }
  204. }