SlackHandlerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Logger;
  13. use Monolog\Formatter\LineFormatter;
  14. use Monolog\Util\LocalSocket;
  15. /**
  16. * @author Greg Kedzierski <greg@gregkedzierski.com>
  17. * @see https://api.slack.com/
  18. */
  19. class SlackHandlerTest extends TestCase
  20. {
  21. /**
  22. * @var resource
  23. */
  24. private $res;
  25. /**
  26. * @var SlackHandler
  27. */
  28. private $handler;
  29. public function setUp()
  30. {
  31. if (!extension_loaded('openssl')) {
  32. $this->markTestSkipped('This test requires openssl to run');
  33. }
  34. }
  35. public function testWriteHeader()
  36. {
  37. $this->initHandlerAndSocket();
  38. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  39. $content = $this->socket->getOutput();
  40. $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);
  41. }
  42. public function testWriteContent()
  43. {
  44. $this->initHandlerAndSocket();
  45. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  46. $content = $this->socket->getOutput();
  47. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
  48. }
  49. public function testWriteContentUsesFormatterIfProvided()
  50. {
  51. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  52. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  53. $content = $this->socket->getOutput();
  54. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  55. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  56. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
  57. $content2 = $this->socket->getOutput();
  58. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
  59. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
  60. }
  61. public function testWriteContentWithEmoji()
  62. {
  63. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', true, 'alien');
  64. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  65. $content = $this->socket->getOutput();
  66. $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
  67. }
  68. /**
  69. * @dataProvider provideLevelColors
  70. */
  71. public function testWriteContentWithColors($level, $expectedColor)
  72. {
  73. $this->initHandlerAndSocket();
  74. $this->handler->handle($this->getRecord($level, 'test1'));
  75. $content = $this->socket->getOutput();
  76. $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
  77. }
  78. public function testWriteContentWithPlainTextMessage()
  79. {
  80. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  81. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  82. $content = $this->socket->getOutput();
  83. $this->assertRegexp('/text=test1/', $content);
  84. }
  85. public function provideLevelColors()
  86. {
  87. return [
  88. [Logger::DEBUG, '%23e3e4e6'], // escaped #e3e4e6
  89. [Logger::INFO, 'good'],
  90. [Logger::NOTICE, 'good'],
  91. [Logger::WARNING, 'warning'],
  92. [Logger::ERROR, 'danger'],
  93. [Logger::CRITICAL, 'danger'],
  94. [Logger::ALERT, 'danger'],
  95. [Logger::EMERGENCY,'danger'],
  96. ];
  97. }
  98. private function initHandlerAndSocket($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  99. {
  100. $this->socket = LocalSocket::initSocket();
  101. $this->handler = new SlackHandler($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
  102. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  103. $reflectionProperty->setAccessible(true);
  104. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  105. $this->handler->setFormatter($this->getIdentityFormatter());
  106. }
  107. public function tearDown()
  108. {
  109. unset($this->socket, $this->handler);
  110. }
  111. }