2
0

HipChatHandlerTest.php 10 KB

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