SlackHandlerTest.php 3.9 KB

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