SlackHandlerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Level;
  13. use Monolog\Formatter\LineFormatter;
  14. use Monolog\Handler\Slack\SlackRecord;
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. /**
  17. * @author Greg Kedzierski <greg@gregkedzierski.com>
  18. * @see https://api.slack.com/
  19. */
  20. class SlackHandlerTest extends TestCase
  21. {
  22. /**
  23. * @var resource
  24. */
  25. private $res;
  26. private SlackHandler $handler;
  27. public function setUp(): void
  28. {
  29. if (!\extension_loaded('openssl')) {
  30. $this->markTestSkipped('This test requires openssl to run');
  31. }
  32. }
  33. public function tearDown(): void
  34. {
  35. parent::tearDown();
  36. unset($this->res);
  37. unset($this->handler);
  38. }
  39. public function testWriteHeader()
  40. {
  41. $this->createHandler();
  42. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  43. fseek($this->res, 0);
  44. $content = fread($this->res, 1024);
  45. $this->assertMatchesRegularExpression('{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);
  46. }
  47. public function testWriteContent()
  48. {
  49. $this->createHandler();
  50. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  51. fseek($this->res, 0);
  52. $content = fread($this->res, 1024);
  53. $this->assertMatchesRegularExpression('/username=Monolog/', $content);
  54. $this->assertMatchesRegularExpression('/channel=channel1/', $content);
  55. $this->assertMatchesRegularExpression('/token=myToken/', $content);
  56. $this->assertMatchesRegularExpression('/attachments/', $content);
  57. }
  58. public function testWriteContentUsesFormatterIfProvided()
  59. {
  60. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  61. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  62. fseek($this->res, 0);
  63. $content = fread($this->res, 1024);
  64. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  65. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  66. $this->handler->handle($this->getRecord(Level::Critical, 'test2'));
  67. fseek($this->res, 0);
  68. $content2 = fread($this->res, 1024);
  69. $this->assertMatchesRegularExpression('/text=test1/', $content);
  70. $this->assertMatchesRegularExpression('/text=foo--test2/', $content2);
  71. }
  72. public function testWriteContentWithEmoji()
  73. {
  74. $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
  75. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  76. fseek($this->res, 0);
  77. $content = fread($this->res, 1024);
  78. $this->assertMatchesRegularExpression('/icon_emoji=%3Aalien%3A/', $content);
  79. }
  80. #[DataProvider('provideLevelColors')]
  81. public function testWriteContentWithColors($level, $expectedColor)
  82. {
  83. $this->createHandler();
  84. $this->handler->handle($this->getRecord($level, 'test1'));
  85. fseek($this->res, 0);
  86. $content = fread($this->res, 1024);
  87. $this->assertMatchesRegularExpression('/%22color%22%3A%22'.$expectedColor.'/', $content);
  88. }
  89. public function testWriteContentWithPlainTextMessage()
  90. {
  91. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  92. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  93. fseek($this->res, 0);
  94. $content = fread($this->res, 1024);
  95. $this->assertMatchesRegularExpression('/text=test1/', $content);
  96. }
  97. public static function provideLevelColors()
  98. {
  99. return [
  100. [Level::Debug, urlencode(SlackRecord::COLOR_DEFAULT)],
  101. [Level::Info, SlackRecord::COLOR_GOOD],
  102. [Level::Notice, SlackRecord::COLOR_GOOD],
  103. [Level::Warning, SlackRecord::COLOR_WARNING],
  104. [Level::Error, SlackRecord::COLOR_DANGER],
  105. [Level::Critical, SlackRecord::COLOR_DANGER],
  106. [Level::Alert, SlackRecord::COLOR_DANGER],
  107. [Level::Emergency,SlackRecord::COLOR_DANGER],
  108. ];
  109. }
  110. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  111. {
  112. $constructorArgs = [$token, $channel, $username, $useAttachment, $iconEmoji, Level::Debug, true, $useShortAttachment, $includeExtra];
  113. $this->res = fopen('php://memory', 'a');
  114. $this->handler = $this->getMockBuilder('Monolog\Handler\SlackHandler')
  115. ->setConstructorArgs($constructorArgs)
  116. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  117. ->getMock();
  118. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  119. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  120. $this->handler->expects($this->any())
  121. ->method('fsockopen')
  122. ->willReturn($this->res);
  123. $this->handler->expects($this->any())
  124. ->method('streamSetTimeout')
  125. ->willReturn(true);
  126. $this->handler->expects($this->any())
  127. ->method('closeSocket');
  128. $this->handler->setFormatter($this->getIdentityFormatter());
  129. }
  130. }