SlackWebhookHandlerTest.php 4.9 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. /**
  16. * @author Haralan Dobrev <hkdobrev@gmail.com>
  17. * @see https://api.slack.com/incoming-webhooks
  18. * @coversDefaultClass Monolog\Handler\SlackWebhookHandler
  19. */
  20. class SlackWebhookHandlerTest extends TestCase
  21. {
  22. const WEBHOOK_URL = 'https://hooks.slack.com/services/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E';
  23. /**
  24. * @covers ::__construct
  25. * @covers ::getSlackRecord
  26. */
  27. public function testConstructorMinimal()
  28. {
  29. $handler = new SlackWebhookHandler(self::WEBHOOK_URL);
  30. $record = $this->getRecord();
  31. $slackRecord = $handler->getSlackRecord();
  32. $this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
  33. $this->assertEquals([
  34. 'attachments' => [
  35. [
  36. 'fallback' => 'test',
  37. 'text' => 'test',
  38. 'color' => SlackRecord::COLOR_WARNING,
  39. 'fields' => [
  40. [
  41. 'title' => 'Level',
  42. 'value' => 'WARNING',
  43. 'short' => false,
  44. ],
  45. ],
  46. 'title' => 'Message',
  47. 'mrkdwn_in' => ['fields'],
  48. 'ts' => $record->datetime->getTimestamp(),
  49. 'footer' => null,
  50. 'footer_icon' => null,
  51. ],
  52. ],
  53. ], $slackRecord->getSlackData($record));
  54. }
  55. /**
  56. * @covers ::__construct
  57. * @covers ::getSlackRecord
  58. */
  59. public function testConstructorFull()
  60. {
  61. $handler = new SlackWebhookHandler(
  62. self::WEBHOOK_URL,
  63. 'test-channel',
  64. 'test-username',
  65. false,
  66. ':ghost:',
  67. false,
  68. false,
  69. Level::Debug,
  70. false
  71. );
  72. $slackRecord = $handler->getSlackRecord();
  73. $this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
  74. $this->assertEquals([
  75. 'username' => 'test-username',
  76. 'text' => 'test',
  77. 'channel' => 'test-channel',
  78. 'icon_emoji' => ':ghost:',
  79. ], $slackRecord->getSlackData($this->getRecord()));
  80. }
  81. /**
  82. * @covers ::__construct
  83. * @covers ::getSlackRecord
  84. */
  85. public function testConstructorFullWithAttachment()
  86. {
  87. $handler = new SlackWebhookHandler(
  88. self::WEBHOOK_URL,
  89. 'test-channel-with-attachment',
  90. 'test-username-with-attachment',
  91. true,
  92. 'https://www.example.com/example.png',
  93. false,
  94. false,
  95. Level::Debug,
  96. false
  97. );
  98. $record = $this->getRecord();
  99. $slackRecord = $handler->getSlackRecord();
  100. $this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
  101. $this->assertEquals([
  102. 'username' => 'test-username-with-attachment',
  103. 'channel' => 'test-channel-with-attachment',
  104. 'attachments' => [
  105. [
  106. 'fallback' => 'test',
  107. 'text' => 'test',
  108. 'color' => SlackRecord::COLOR_WARNING,
  109. 'fields' => [
  110. [
  111. 'title' => 'Level',
  112. 'value' => Level::Warning->getName(),
  113. 'short' => false,
  114. ],
  115. ],
  116. 'mrkdwn_in' => ['fields'],
  117. 'ts' => $record['datetime']->getTimestamp(),
  118. 'footer' => 'test-username-with-attachment',
  119. 'footer_icon' => 'https://www.example.com/example.png',
  120. 'title' => 'Message',
  121. ],
  122. ],
  123. 'icon_url' => 'https://www.example.com/example.png',
  124. ], $slackRecord->getSlackData($record));
  125. }
  126. /**
  127. * @covers ::getFormatter
  128. */
  129. public function testGetFormatter()
  130. {
  131. $handler = new SlackWebhookHandler(self::WEBHOOK_URL);
  132. $formatter = $handler->getFormatter();
  133. $this->assertInstanceOf('Monolog\Formatter\FormatterInterface', $formatter);
  134. }
  135. /**
  136. * @covers ::setFormatter
  137. */
  138. public function testSetFormatter()
  139. {
  140. $handler = new SlackWebhookHandler(self::WEBHOOK_URL);
  141. $formatter = new LineFormatter();
  142. $handler->setFormatter($formatter);
  143. $this->assertSame($formatter, $handler->getFormatter());
  144. }
  145. }