SlackHandlerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Greg Kedzierski <greg@gregkedzierski.com>
  15. * @see https://api.slack.com/
  16. */
  17. class SlackHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $res;
  23. /**
  24. * @var SlackHandler
  25. */
  26. private $handler;
  27. public function setUp()
  28. {
  29. if (!extension_loaded('openssl')) {
  30. $this->markTestSkipped('This test requires openssl to run');
  31. }
  32. }
  33. public function testWriteHeader()
  34. {
  35. $this->createHandler();
  36. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  37. fseek($this->res, 0);
  38. $content = fread($this->res, 1024);
  39. $this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  40. }
  41. public function testWriteContent()
  42. {
  43. $this->createHandler();
  44. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  45. fseek($this->res, 0);
  46. $content = fread($this->res, 1024);
  47. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&attachments=.*$/', $content);
  48. }
  49. /**
  50. * @dataProvider provideLevelColors
  51. */
  52. public function testWriteContentWithColors($level, $expectedColor)
  53. {
  54. $this->createHandler();
  55. $this->handler->handle($this->getRecord($level, 'test1'));
  56. fseek($this->res, 0);
  57. $content = fread($this->res, 1024);
  58. $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
  59. }
  60. public function provideLevelColors()
  61. {
  62. return array(
  63. array(Logger::DEBUG, '%23e3e4e6'), // escaped #e3e4e6
  64. array(Logger::INFO, 'good'),
  65. array(Logger::NOTICE, 'good'),
  66. array(Logger::WARNING, 'warning'),
  67. array(Logger::ERROR, 'danger'),
  68. array(Logger::CRITICAL, 'danger'),
  69. array(Logger::ALERT, 'danger'),
  70. array(Logger::EMERGENCY,'danger'),
  71. );
  72. }
  73. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog')
  74. {
  75. $constructorArgs = array($token, $channel, $username, Logger::DEBUG);
  76. $this->res = fopen('php://memory', 'a');
  77. $this->handler = $this->getMock(
  78. '\Monolog\Handler\SlackHandler',
  79. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  80. $constructorArgs
  81. );
  82. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  83. $reflectionProperty->setAccessible(true);
  84. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  85. $this->handler->expects($this->any())
  86. ->method('fsockopen')
  87. ->will($this->returnValue($this->res));
  88. $this->handler->expects($this->any())
  89. ->method('streamSetTimeout')
  90. ->will($this->returnValue(true));
  91. $this->handler->expects($this->any())
  92. ->method('closeSocket')
  93. ->will($this->returnValue(true));
  94. $this->handler->setFormatter($this->getIdentityFormatter());
  95. }
  96. }