2
0

HipChatHandlerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. use Monolog\Util\LocalSocket;
  14. /**
  15. * @author Rafael Dohms <rafael@doh.ms>
  16. * @see https://www.hipchat.com/docs/api
  17. */
  18. class HipChatHandlerTest extends TestCase
  19. {
  20. private $res;
  21. /** @var HipChatHandler */
  22. private $handler;
  23. public function testWriteV2()
  24. {
  25. $this->initHandlerAndSocket('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  26. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  27. $content = $this->socket->getOutput();
  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. /**
  32. * @depends testWriteV2
  33. */
  34. public function testWriteContentV2($content)
  35. {
  36. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  37. }
  38. public function testWriteV2Notify()
  39. {
  40. $this->initHandlerAndSocket('myToken', 'room2', 'Monolog', true, 'hipchat.foo.bar', 'v2');
  41. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  42. $content = $this->socket->getOutput();
  43. $this->assertRegexp('{POST /v2/room/room2/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 testWriteV2Notify
  48. */
  49. public function testWriteContentV2Notify($content)
  50. {
  51. $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content);
  52. }
  53. public function testRoomSpaces()
  54. {
  55. $this->initHandlerAndSocket('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
  56. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  57. $content = $this->socket->getOutput();
  58. $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);
  59. return $content;
  60. }
  61. public function testWriteContentV2WithoutName()
  62. {
  63. $this->initHandlerAndSocket('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
  64. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  65. $content = $this->socket->getOutput();
  66. $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
  67. return $content;
  68. }
  69. public function testWriteWithComplexMessage()
  70. {
  71. $this->initHandlerAndSocket();
  72. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  73. $content = $this->socket->getOutput();
  74. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  75. }
  76. public function testWriteTruncatesLongMessage()
  77. {
  78. $this->initHandlerAndSocket();
  79. $this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
  80. $content = $this->socket->getOutput();
  81. $this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
  82. }
  83. /**
  84. * @dataProvider provideLevelColors
  85. */
  86. public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
  87. {
  88. $this->initHandlerAndSocket();
  89. $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
  90. $content = $this->socket->getOutput();
  91. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  92. }
  93. public function provideLevelColors()
  94. {
  95. return [
  96. [Logger::DEBUG, 'gray'],
  97. [Logger::INFO, 'green'],
  98. [Logger::WARNING, 'yellow'],
  99. [Logger::ERROR, 'red'],
  100. [Logger::CRITICAL, 'red'],
  101. [Logger::ALERT, 'red'],
  102. [Logger::EMERGENCY,'red'],
  103. [Logger::NOTICE, 'green'],
  104. ];
  105. }
  106. /**
  107. * @dataProvider provideBatchRecords
  108. */
  109. public function testHandleBatch($records, $expectedColor)
  110. {
  111. $this->initHandlerAndSocket();
  112. $this->handler->handleBatch($records);
  113. $content = $this->socket->getOutput();
  114. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  115. }
  116. public function provideBatchRecords()
  117. {
  118. return [
  119. [
  120. [
  121. ['level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTimeImmutable()],
  122. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  123. ['level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTimeImmutable()],
  124. ],
  125. 'red',
  126. ],
  127. [
  128. [
  129. ['level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTimeImmutable()],
  130. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  131. ],
  132. 'yellow',
  133. ],
  134. [
  135. [
  136. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  137. ['level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTimeImmutable()],
  138. ],
  139. 'green',
  140. ],
  141. [
  142. [
  143. ['level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTimeImmutable()],
  144. ],
  145. 'gray',
  146. ],
  147. ];
  148. }
  149. public function testCreateWithTooLongNameV2()
  150. {
  151. // creating a handler with too long of a name but using the v2 api doesn't matter.
  152. $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
  153. }
  154. private function initHandlerAndSocket($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
  155. {
  156. $this->socket = LocalSocket::initSocket();
  157. $this->handler = new HipChatHandler($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
  158. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  159. $reflectionProperty->setAccessible(true);
  160. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  161. $this->handler->setFormatter($this->getIdentityFormatter());
  162. }
  163. public function tearDown()
  164. {
  165. unset($this->socket, $this->handler);
  166. }
  167. }