HipChatHandlerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  25. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  26. fseek($this->res, 0);
  27. $content = fread($this->res, 1024);
  28. $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);
  29. return $content;
  30. }
  31. public function testWriteV2Notify()
  32. {
  33. $this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
  34. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  35. fseek($this->res, 0);
  36. $content = fread($this->res, 1024);
  37. $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);
  38. return $content;
  39. }
  40. public function testRoomSpaces()
  41. {
  42. $this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  43. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  44. fseek($this->res, 0);
  45. $content = fread($this->res, 1024);
  46. $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);
  47. return $content;
  48. }
  49. /**
  50. * @depends testWriteHeader
  51. */
  52. public function testWriteContent($content)
  53. {
  54. $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
  55. }
  56. /**
  57. * @depends testWriteCustomHostHeader
  58. */
  59. public function testWriteContentNotify($content)
  60. {
  61. $this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
  62. }
  63. /**
  64. * @depends testWriteV2
  65. */
  66. public function testWriteContentV2($content)
  67. {
  68. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  69. }
  70. /**
  71. * @depends testWriteV2Notify
  72. */
  73. public function testWriteContentV2Notify($content)
  74. {
  75. $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  76. }
  77. public function testWriteContentV2WithoutName()
  78. {
  79. $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
  80. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  81. fseek($this->res, 0);
  82. $content = fread($this->res, 1024);
  83. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
  84. return $content;
  85. }
  86. public function testWriteWithComplexMessage()
  87. {
  88. $this->createHandler();
  89. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  90. fseek($this->res, 0);
  91. $content = fread($this->res, 1024);
  92. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  93. }
  94. public function testWriteTruncatesLongMessage()
  95. {
  96. $this->createHandler();
  97. $this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
  98. fseek($this->res, 0);
  99. $content = fread($this->res, 12000);
  100. $this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
  101. }
  102. /**
  103. * @dataProvider provideLevelColors
  104. */
  105. public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
  106. {
  107. $this->createHandler();
  108. $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
  109. fseek($this->res, 0);
  110. $content = fread($this->res, 1024);
  111. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  112. }
  113. public function provideLevelColors()
  114. {
  115. return [
  116. [Logger::DEBUG, 'gray'],
  117. [Logger::INFO, 'green'],
  118. [Logger::WARNING, 'yellow'],
  119. [Logger::ERROR, 'red'],
  120. [Logger::CRITICAL, 'red'],
  121. [Logger::ALERT, 'red'],
  122. [Logger::EMERGENCY,'red'],
  123. [Logger::NOTICE, 'green'],
  124. ];
  125. }
  126. /**
  127. * @dataProvider provideBatchRecords
  128. */
  129. public function testHandleBatch($records, $expectedColor)
  130. {
  131. $this->createHandler();
  132. $this->handler->handleBatch($records);
  133. fseek($this->res, 0);
  134. $content = fread($this->res, 1024);
  135. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  136. }
  137. public function provideBatchRecords()
  138. {
  139. return [
  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. ['level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTimeImmutable()],
  145. ],
  146. 'red',
  147. ],
  148. [
  149. [
  150. ['level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTimeImmutable()],
  151. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  152. ],
  153. 'yellow',
  154. ],
  155. [
  156. [
  157. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  158. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  159. ],
  160. 'green',
  161. ],
  162. [
  163. [
  164. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  165. ],
  166. 'gray',
  167. ],
  168. ];
  169. }
  170. private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
  171. {
  172. $constructorArgs = [$token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version];
  173. $this->res = fopen('php://memory', 'a');
  174. $this->handler = $this->getMockBuilder('Monolog\Handler\HipChatHandler')
  175. ->setConstructorArgs($constructorArgs)
  176. ->setMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  177. ->getMock();
  178. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  179. $reflectionProperty->setAccessible(true);
  180. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  181. $this->handler->expects($this->any())
  182. ->method('fsockopen')
  183. ->will($this->returnValue($this->res));
  184. $this->handler->expects($this->any())
  185. ->method('streamSetTimeout')
  186. ->will($this->returnValue(true));
  187. $this->handler->expects($this->any())
  188. ->method('closeSocket')
  189. ->will($this->returnValue(true));
  190. $this->handler->setFormatter($this->getIdentityFormatter());
  191. }
  192. public function testCreateWithTooLongNameV2()
  193. {
  194. // creating a handler with too long of a name but using the v2 api doesn't matter.
  195. $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
  196. }
  197. }