2
0

SlackWebhookHandlerTest.php 4.9 KB

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