HipChatHandlerTest.php 9.0 KB

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