/vendor/monolog/monolog/tests/Monolog/Handler/SlackHandlerTest.php

https://gitlab.com/ealexis.t/trends · PHP · 133 lines · 91 code · 21 blank · 21 comment · 1 complexity · cb81ae1907dffd47af232dd5df530fa6 MD5 · raw file

  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=.*$/', $content);
  48. }
  49. public function testWriteContentWithEmoji()
  50. {
  51. $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
  52. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  53. fseek($this->res, 0);
  54. $content = fread($this->res, 1024);
  55. $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
  56. }
  57. /**
  58. * @dataProvider provideLevelColors
  59. */
  60. public function testWriteContentWithColors($level, $expectedColor)
  61. {
  62. $this->createHandler();
  63. $this->handler->handle($this->getRecord($level, 'test1'));
  64. fseek($this->res, 0);
  65. $content = fread($this->res, 1024);
  66. $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
  67. }
  68. public function testWriteContentWithPlainTextMessage()
  69. {
  70. $this->createHandler('myToken', 'channel1', 'Monolog', false);
  71. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  72. fseek($this->res, 0);
  73. $content = fread($this->res, 1024);
  74. $this->assertRegexp('/text=test1/', $content);
  75. }
  76. public function provideLevelColors()
  77. {
  78. return array(
  79. array(Logger::DEBUG, '%23e3e4e6'), // escaped #e3e4e6
  80. array(Logger::INFO, 'good'),
  81. array(Logger::NOTICE, 'good'),
  82. array(Logger::WARNING, 'warning'),
  83. array(Logger::ERROR, 'danger'),
  84. array(Logger::CRITICAL, 'danger'),
  85. array(Logger::ALERT, 'danger'),
  86. array(Logger::EMERGENCY,'danger'),
  87. );
  88. }
  89. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
  90. {
  91. $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
  92. $this->res = fopen('php://memory', 'a');
  93. $this->handler = $this->getMock(
  94. '\Monolog\Handler\SlackHandler',
  95. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  96. $constructorArgs
  97. );
  98. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  99. $reflectionProperty->setAccessible(true);
  100. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  101. $this->handler->expects($this->any())
  102. ->method('fsockopen')
  103. ->will($this->returnValue($this->res));
  104. $this->handler->expects($this->any())
  105. ->method('streamSetTimeout')
  106. ->will($this->returnValue(true));
  107. $this->handler->expects($this->any())
  108. ->method('closeSocket')
  109. ->will($this->returnValue(true));
  110. $this->handler->setFormatter($this->getIdentityFormatter());
  111. }
  112. }