HipChatHandlerTest.php 5.8 KB

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