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. }
  38. public function testWriteHeader()
  39. {
  40. $this->createHandler();
  41. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  42. fseek($this->res, 0);
  43. $content = fread($this->res, 1024);
  44. $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);
  45. }
  46. public function testWriteContent()
  47. {
  48. $this->createHandler();
  49. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  50. fseek($this->res, 0);
  51. $content = fread($this->res, 1024);
  52. $this->assertMatchesRegularExpression('/username=Monolog/', $content);
  53. $this->assertMatchesRegularExpression('/channel=channel1/', $content);
  54. $this->assertMatchesRegularExpression('/token=myToken/', $content);
  55. $this->assertMatchesRegularExpression('/attachments/', $content);
  56. }
  57. public function testWriteContentUsesFormatterIfProvided()
  58. {
  59. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  60. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  61. fseek($this->res, 0);
  62. $content = fread($this->res, 1024);
  63. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  64. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  65. $this->handler->handle($this->getRecord(Level::Critical, 'test2'));
  66. fseek($this->res, 0);
  67. $content2 = fread($this->res, 1024);
  68. $this->assertMatchesRegularExpression('/text=test1/', $content);
  69. $this->assertMatchesRegularExpression('/text=foo--test2/', $content2);
  70. }
  71. public function testWriteContentWithEmoji()
  72. {
  73. $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
  74. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  75. fseek($this->res, 0);
  76. $content = fread($this->res, 1024);
  77. $this->assertMatchesRegularExpression('/icon_emoji=%3Aalien%3A/', $content);
  78. }
  79. #[DataProvider('provideLevelColors')]
  80. public function testWriteContentWithColors($level, $expectedColor)
  81. {
  82. $this->createHandler();
  83. $this->handler->handle($this->getRecord($level, 'test1'));
  84. fseek($this->res, 0);
  85. $content = fread($this->res, 1024);
  86. $this->assertMatchesRegularExpression('/%22color%22%3A%22'.$expectedColor.'/', $content);
  87. }
  88. public function testWriteContentWithPlainTextMessage()
  89. {
  90. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  91. $this->handler->handle($this->getRecord(Level::Critical, 'test1'));
  92. fseek($this->res, 0);
  93. $content = fread($this->res, 1024);
  94. $this->assertMatchesRegularExpression('/text=test1/', $content);
  95. }
  96. public static function provideLevelColors()
  97. {
  98. return [
  99. [Level::Debug, urlencode(SlackRecord::COLOR_DEFAULT)],
  100. [Level::Info, SlackRecord::COLOR_GOOD],
  101. [Level::Notice, SlackRecord::COLOR_GOOD],
  102. [Level::Warning, SlackRecord::COLOR_WARNING],
  103. [Level::Error, SlackRecord::COLOR_DANGER],
  104. [Level::Critical, SlackRecord::COLOR_DANGER],
  105. [Level::Alert, SlackRecord::COLOR_DANGER],
  106. [Level::Emergency,SlackRecord::COLOR_DANGER],
  107. ];
  108. }
  109. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  110. {
  111. $constructorArgs = [$token, $channel, $username, $useAttachment, $iconEmoji, Level::Debug, true, $useShortAttachment, $includeExtra];
  112. $this->res = fopen('php://memory', 'a');
  113. $this->handler = $this->getMockBuilder('Monolog\Handler\SlackHandler')
  114. ->setConstructorArgs($constructorArgs)
  115. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  116. ->getMock();
  117. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  118. $reflectionProperty->setAccessible(true);
  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. }