HipChatHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false)
  70. {
  71. $constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG);
  72. $this->res = fopen('php://memory', 'a');
  73. $this->handler = $this->getMock(
  74. '\Monolog\Handler\HipChatHandler',
  75. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  76. $constructorArgs
  77. );
  78. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  79. $reflectionProperty->setAccessible(true);
  80. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  81. $this->handler->expects($this->any())
  82. ->method('fsockopen')
  83. ->will($this->returnValue($this->res));
  84. $this->handler->expects($this->any())
  85. ->method('streamSetTimeout')
  86. ->will($this->returnValue(true));
  87. $this->handler->expects($this->any())
  88. ->method('closeSocket')
  89. ->will($this->returnValue(true));
  90. $this->handler->setFormatter($this->getIdentityFormatter());
  91. }
  92. }