SlackWebhookHandlerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  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\TestCase;
  12. use Monolog\Logger;
  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(array(
  34. 'attachments' => array(
  35. array(
  36. 'fallback' => 'test',
  37. 'text' => 'test',
  38. 'color' => SlackRecord::COLOR_WARNING,
  39. 'fields' => array(
  40. array(
  41. 'title' => 'Level',
  42. 'value' => 'WARNING',
  43. 'short' => false,
  44. ),
  45. ),
  46. 'title' => 'Message',
  47. 'mrkdwn_in' => array('fields'),
  48. 'ts' => $record['datetime']->getTimestamp(),
  49. ),
  50. ),
  51. ), $slackRecord->getSlackData($record));
  52. }
  53. /**
  54. * @covers ::__construct
  55. * @covers ::getSlackRecord
  56. */
  57. public function testConstructorFull()
  58. {
  59. $handler = new SlackWebhookHandler(
  60. self::WEBHOOK_URL,
  61. 'test-channel',
  62. 'test-username',
  63. false,
  64. ':ghost:',
  65. false,
  66. false,
  67. Logger::DEBUG,
  68. false
  69. );
  70. $slackRecord = $handler->getSlackRecord();
  71. $this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
  72. $this->assertEquals(array(
  73. 'username' => 'test-username',
  74. 'text' => 'test',
  75. 'channel' => 'test-channel',
  76. 'icon_emoji' => ':ghost:',
  77. ), $slackRecord->getSlackData($this->getRecord()));
  78. }
  79. /**
  80. * @covers ::getFormatter
  81. */
  82. public function testGetFormatter()
  83. {
  84. $handler = new SlackWebhookHandler(self::WEBHOOK_URL);
  85. $formatter = $handler->getFormatter();
  86. $this->assertInstanceOf('Monolog\Formatter\FormatterInterface', $formatter);
  87. }
  88. /**
  89. * @covers ::setFormatter
  90. */
  91. public function testSetFormatter()
  92. {
  93. $handler = new SlackWebhookHandler(self::WEBHOOK_URL);
  94. $formatter = new LineFormatter();
  95. $handler->setFormatter($formatter);
  96. $this->assertSame($formatter, $handler->getFormatter());
  97. }
  98. }