SlackHandlerTest.php 5.1 KB

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