HipChatHandlerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 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. /**
  122. * @dataProvider provideLevelColors
  123. */
  124. public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
  125. {
  126. $this->createHandler();
  127. $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
  128. fseek($this->res, 0);
  129. $content = fread($this->res, 1024);
  130. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  131. }
  132. public function provideLevelColors()
  133. {
  134. return array(
  135. array(Logger::DEBUG, 'gray'),
  136. array(Logger::INFO, 'green'),
  137. array(Logger::WARNING, 'yellow'),
  138. array(Logger::ERROR, 'red'),
  139. array(Logger::CRITICAL, 'red'),
  140. array(Logger::ALERT, 'red'),
  141. array(Logger::EMERGENCY,'red'),
  142. array(Logger::NOTICE, 'green'),
  143. );
  144. }
  145. /**
  146. * @dataProvider provideBatchRecords
  147. */
  148. public function testHandleBatch($records, $expectedColor)
  149. {
  150. $this->createHandler();
  151. $this->handler->handleBatch($records);
  152. fseek($this->res, 0);
  153. $content = fread($this->res, 1024);
  154. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  155. }
  156. public function provideBatchRecords()
  157. {
  158. return array(
  159. array(
  160. array(
  161. array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
  162. array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
  163. array('level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTime()),
  164. ),
  165. 'red',
  166. ),
  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. ),
  172. 'yellow',
  173. ),
  174. array(
  175. array(
  176. array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
  177. array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
  178. ),
  179. 'green',
  180. ),
  181. array(
  182. array(
  183. array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
  184. ),
  185. 'gray',
  186. ),
  187. );
  188. }
  189. private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
  190. {
  191. $constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
  192. $this->res = fopen('php://memory', 'a');
  193. $this->handler = $this->getMock(
  194. '\Monolog\Handler\HipChatHandler',
  195. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  196. $constructorArgs
  197. );
  198. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  199. $reflectionProperty->setAccessible(true);
  200. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  201. $this->handler->expects($this->any())
  202. ->method('fsockopen')
  203. ->will($this->returnValue($this->res));
  204. $this->handler->expects($this->any())
  205. ->method('streamSetTimeout')
  206. ->will($this->returnValue(true));
  207. $this->handler->expects($this->any())
  208. ->method('closeSocket')
  209. ->will($this->returnValue(true));
  210. $this->handler->setFormatter($this->getIdentityFormatter());
  211. }
  212. /**
  213. * @expectedException InvalidArgumentException
  214. */
  215. public function testCreateWithTooLongName()
  216. {
  217. $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere');
  218. }
  219. public function testCreateWithTooLongNameV2()
  220. {
  221. // creating a handler with too long of a name but using the v2 api doesn't matter.
  222. $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
  223. }
  224. }