SlackHandlerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Handler\Slack\SlackRecord;
  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(): void
  30. {
  31. if (!extension_loaded('openssl')) {
  32. $this->markTestSkipped('This test requires openssl to run');
  33. }
  34. }
  35. public function testWriteHeader()
  36. {
  37. $this->createHandler();
  38. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  39. fseek($this->res, 0);
  40. $content = fread($this->res, 1024);
  41. $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);
  42. }
  43. public function testWriteContent()
  44. {
  45. $this->createHandler();
  46. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  47. fseek($this->res, 0);
  48. $content = fread($this->res, 1024);
  49. $this->assertRegExp('/username=Monolog/', $content);
  50. $this->assertRegExp('/channel=channel1/', $content);
  51. $this->assertRegExp('/token=myToken/', $content);
  52. $this->assertRegExp('/attachments/', $content);
  53. }
  54. public function testWriteContentUsesFormatterIfProvided()
  55. {
  56. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  57. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  58. fseek($this->res, 0);
  59. $content = fread($this->res, 1024);
  60. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  61. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  62. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
  63. fseek($this->res, 0);
  64. $content2 = fread($this->res, 1024);
  65. $this->assertRegexp('/text=test1/', $content);
  66. $this->assertRegexp('/text=foo--test2/', $content2);
  67. }
  68. public function testWriteContentWithEmoji()
  69. {
  70. $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
  71. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  72. fseek($this->res, 0);
  73. $content = fread($this->res, 1024);
  74. $this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
  75. }
  76. /**
  77. * @dataProvider provideLevelColors
  78. */
  79. public function testWriteContentWithColors($level, $expectedColor)
  80. {
  81. $this->createHandler();
  82. $this->handler->handle($this->getRecord($level, 'test1'));
  83. fseek($this->res, 0);
  84. $content = fread($this->res, 1024);
  85. $this->assertRegexp('/%22color%22%3A%22'.$expectedColor.'/', $content);
  86. }
  87. public function testWriteContentWithPlainTextMessage()
  88. {
  89. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  90. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  91. fseek($this->res, 0);
  92. $content = fread($this->res, 1024);
  93. $this->assertRegexp('/text=test1/', $content);
  94. }
  95. public function provideLevelColors()
  96. {
  97. return array(
  98. array(Logger::DEBUG, urlencode(SlackRecord::COLOR_DEFAULT)),
  99. array(Logger::INFO, SlackRecord::COLOR_GOOD),
  100. array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
  101. array(Logger::WARNING, SlackRecord::COLOR_WARNING),
  102. array(Logger::ERROR, SlackRecord::COLOR_DANGER),
  103. array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
  104. array(Logger::ALERT, SlackRecord::COLOR_DANGER),
  105. array(Logger::EMERGENCY,SlackRecord::COLOR_DANGER),
  106. );
  107. }
  108. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  109. {
  110. $constructorArgs = [$token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra];
  111. $this->res = fopen('php://memory', 'a');
  112. $this->handler = $this->getMockBuilder('Monolog\Handler\SlackHandler')
  113. ->setConstructorArgs($constructorArgs)
  114. ->setMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  115. ->getMock();
  116. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  117. $reflectionProperty->setAccessible(true);
  118. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  119. $this->handler->expects($this->any())
  120. ->method('fsockopen')
  121. ->will($this->returnValue($this->res));
  122. $this->handler->expects($this->any())
  123. ->method('streamSetTimeout')
  124. ->will($this->returnValue(true));
  125. $this->handler->expects($this->any())
  126. ->method('closeSocket')
  127. ->will($this->returnValue(true));
  128. $this->handler->setFormatter($this->getIdentityFormatter());
  129. }
  130. }