SlackHandlerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Greg Kedzierski <greg@gregkedzierski.com>
  17. * @see https://api.slack.com/
  18. */
  19. class SlackHandlerTest extends TestCase
  20. {
  21. /**
  22. * @var resource
  23. */
  24. private $res;
  25. /**
  26. * @var SlackHandler
  27. */
  28. private $handler;
  29. public function setUp()
  30. {
  31. if (!extension_loaded('openssl')) {
  32. $this->markTestSkipped('This test requires openssl to run');
  33. }
  34. }
  35. public function testWriteHeader()
  36. {
  37. $this->createHandler();
  38. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  39. fseek($this->res, 0);
  40. $content = fread($this->res, 1024);
  41. $this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  42. }
  43. public function testWriteContent()
  44. {
  45. $this->createHandler();
  46. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  47. fseek($this->res, 0);
  48. $content = fread($this->res, 1024);
  49. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
  50. }
  51. public function testWriteContentUsesFormatterIfProvided()
  52. {
  53. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  54. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  55. fseek($this->res, 0);
  56. $content = fread($this->res, 1024);
  57. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  58. $this->handler->setFormatter(new LineFormatter('foo--%message%'));
  59. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
  60. fseek($this->res, 0);
  61. $content2 = fread($this->res, 1024);
  62. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
  63. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
  64. }
  65. public function testWriteContentWithEmoji()
  66. {
  67. $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
  68. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  69. fseek($this->res, 0);
  70. $content = fread($this->res, 1024);
  71. $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
  72. }
  73. /**
  74. * @dataProvider provideLevelColors
  75. */
  76. public function testWriteContentWithColors($level, $expectedColor)
  77. {
  78. $this->createHandler();
  79. $this->handler->handle($this->getRecord($level, 'test1'));
  80. fseek($this->res, 0);
  81. $content = fread($this->res, 1024);
  82. $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
  83. }
  84. public function testWriteContentWithPlainTextMessage()
  85. {
  86. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  87. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  88. fseek($this->res, 0);
  89. $content = fread($this->res, 1024);
  90. $this->assertRegexp('/text=test1/', $content);
  91. }
  92. public function provideLevelColors()
  93. {
  94. return array(
  95. array(Logger::DEBUG, urlencode(SlackRecord::COLOR_DEFAULT)),
  96. array(Logger::INFO, SlackRecord::COLOR_GOOD),
  97. array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
  98. array(Logger::WARNING, SlackRecord::COLOR_WARNING),
  99. array(Logger::ERROR, SlackRecord::COLOR_DANGER),
  100. array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
  101. array(Logger::ALERT, SlackRecord::COLOR_DANGER),
  102. array(Logger::EMERGENCY,SlackRecord::COLOR_DANGER),
  103. );
  104. }
  105. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  106. {
  107. $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
  108. $this->res = fopen('php://memory', 'a');
  109. $this->handler = $this->getMock(
  110. '\Monolog\Handler\SlackHandler',
  111. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  112. $constructorArgs
  113. );
  114. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  115. $reflectionProperty->setAccessible(true);
  116. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  117. $this->handler->expects($this->any())
  118. ->method('fsockopen')
  119. ->will($this->returnValue($this->res));
  120. $this->handler->expects($this->any())
  121. ->method('streamSetTimeout')
  122. ->will($this->returnValue(true));
  123. $this->handler->expects($this->any())
  124. ->method('closeSocket')
  125. ->will($this->returnValue(true));
  126. $this->handler->setFormatter($this->getIdentityFormatter());
  127. }
  128. }