2
0

SlackWebhookHandlerTest.php 4.8 KB

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