HipChatHandlerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 \/v2\/room\/room1\/notification\?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', false, '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 \/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);
  37. return $content;
  38. }
  39. /**
  40. * @depends testWriteHeader
  41. */
  42. public function testWriteContent($content)
  43. {
  44. $this->assertRegexp('/from=Monolog&notify=0&message=test1&message_format=text&color=red$/', $content);
  45. }
  46. public function testWriteWithComplexMessage()
  47. {
  48. $this->createHandler();
  49. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  50. fseek($this->res, 0);
  51. $content = fread($this->res, 1024);
  52. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  53. }
  54. /**
  55. * @dataProvider provideLevelColors
  56. */
  57. public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
  58. {
  59. $this->createHandler();
  60. $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
  61. fseek($this->res, 0);
  62. $content = fread($this->res, 1024);
  63. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  64. }
  65. public function provideLevelColors()
  66. {
  67. return array(
  68. array(Logger::DEBUG, 'gray'),
  69. array(Logger::INFO, 'green'),
  70. array(Logger::WARNING, 'yellow'),
  71. array(Logger::ERROR, 'red'),
  72. array(Logger::CRITICAL, 'red'),
  73. array(Logger::ALERT, 'red'),
  74. array(Logger::EMERGENCY,'red'),
  75. array(Logger::NOTICE, 'green'),
  76. );
  77. }
  78. /**
  79. * @dataProvider provideBatchRecords
  80. */
  81. public function testHandleBatch($records, $expectedColor)
  82. {
  83. $this->createHandler();
  84. $this->handler->handleBatch($records);
  85. fseek($this->res, 0);
  86. $content = fread($this->res, 1024);
  87. $this->assertRegexp('/color='.$expectedColor.'/', $content);
  88. }
  89. public function provideBatchRecords()
  90. {
  91. return array(
  92. array(
  93. array(
  94. array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
  95. array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
  96. array('level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTime())
  97. ),
  98. 'red',
  99. ),
  100. array(
  101. array(
  102. array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
  103. array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
  104. ),
  105. 'yellow',
  106. ),
  107. array(
  108. array(
  109. array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
  110. array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
  111. ),
  112. 'green',
  113. ),
  114. array(
  115. array(
  116. array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
  117. ),
  118. 'gray',
  119. ),
  120. );
  121. }
  122. private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com')
  123. {
  124. $constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host);
  125. $this->res = fopen('php://memory', 'a');
  126. $this->handler = $this->getMock(
  127. '\Monolog\Handler\HipChatHandler',
  128. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  129. $constructorArgs
  130. );
  131. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  132. $reflectionProperty->setAccessible(true);
  133. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  134. $this->handler->expects($this->any())
  135. ->method('fsockopen')
  136. ->will($this->returnValue($this->res));
  137. $this->handler->expects($this->any())
  138. ->method('streamSetTimeout')
  139. ->will($this->returnValue(true));
  140. $this->handler->expects($this->any())
  141. ->method('closeSocket')
  142. ->will($this->returnValue(true));
  143. $this->handler->setFormatter($this->getIdentityFormatter());
  144. }
  145. /**
  146. * @expectedException InvalidArgumentException
  147. */
  148. public function testCreateWithTooLongName()
  149. {
  150. $hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere');
  151. }
  152. }