SlackHandlerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  15. * @author Greg Kedzierski <greg@gregkedzierski.com>
  16. * @see https://api.slack.com/
  17. */
  18. class SlackHandlerTest extends TestCase
  19. {
  20. /**
  21. * @var resource
  22. */
  23. private $res;
  24. /**
  25. * @var SlackHandler
  26. */
  27. private $handler;
  28. public function setUp()
  29. {
  30. if (!extension_loaded('openssl')) {
  31. $this->markTestSkipped('This test requires openssl to run');
  32. }
  33. }
  34. public function testWriteHeader()
  35. {
  36. $this->initHandlerAndSocket();
  37. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  38. $content = $this->closeSocket();
  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->initHandlerAndSocket();
  44. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  45. $content = $this->closeSocket();
  46. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
  47. }
  48. public function testWriteContentUsesFormatterIfProvided()
  49. {
  50. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  51. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  52. $content = $this->closeSocket();
  53. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  54. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  55. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
  56. $content2 = $this->closeSocket();
  57. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
  58. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
  59. }
  60. public function testWriteContentWithEmoji()
  61. {
  62. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', true, 'alien');
  63. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  64. $content = $this->closeSocket();
  65. $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
  66. }
  67. /**
  68. * @dataProvider provideLevelColors
  69. */
  70. public function testWriteContentWithColors($level, $expectedColor)
  71. {
  72. $this->initHandlerAndSocket();
  73. $this->handler->handle($this->getRecord($level, 'test1'));
  74. $content = $this->closeSocket();
  75. $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
  76. }
  77. public function testWriteContentWithPlainTextMessage()
  78. {
  79. $this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
  80. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  81. $content = $this->closeSocket();
  82. $this->assertRegexp('/text=test1/', $content);
  83. }
  84. public function provideLevelColors()
  85. {
  86. return [
  87. [Logger::DEBUG, '%23e3e4e6'], // escaped #e3e4e6
  88. [Logger::INFO, 'good'],
  89. [Logger::NOTICE, 'good'],
  90. [Logger::WARNING, 'warning'],
  91. [Logger::ERROR, 'danger'],
  92. [Logger::CRITICAL, 'danger'],
  93. [Logger::ALERT, 'danger'],
  94. [Logger::EMERGENCY,'danger'],
  95. ];
  96. }
  97. private function initHandlerAndSocket($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  98. {
  99. $tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
  100. file_put_contents($tmpFile, <<<'SCRIPT'
  101. <?php
  102. $sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  103. socket_bind($sock, '127.0.0.1', 51984);
  104. socket_listen($sock);
  105. while (true) {
  106. $res = socket_accept($sock);
  107. socket_set_option($res, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 500));
  108. while ($read = socket_read($res, 1024)) {
  109. echo $read;
  110. }
  111. socket_close($res);
  112. }
  113. SCRIPT
  114. );
  115. $this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
  116. $this->socket->start();
  117. $this->handler = new SlackHandler($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
  118. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  119. $reflectionProperty->setAccessible(true);
  120. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  121. $this->handler->setFormatter($this->getIdentityFormatter());
  122. }
  123. private function closeSocket()
  124. {
  125. $this->socket->stop();
  126. return $this->socket->getOutput();
  127. }
  128. public function tearDown()
  129. {
  130. if (isset($this->socket)) {
  131. $this->closeSocket();
  132. unset($this->socket);
  133. }
  134. }
  135. }