SlackWebhookHandlerTest.php 3.0 KB

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