SlackRecordTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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\Slack;
  11. use Monolog\Logger;
  12. use Monolog\Test\TestCase;
  13. /**
  14. * @coversDefaultClass Monolog\Handler\Slack\SlackRecord
  15. */
  16. class SlackRecordTest extends TestCase
  17. {
  18. private $jsonPrettyPrintFlag;
  19. protected function setUp()
  20. {
  21. $this->jsonPrettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
  22. }
  23. public function dataGetAttachmentColor()
  24. {
  25. return array(
  26. array(Logger::DEBUG, SlackRecord::COLOR_DEFAULT),
  27. array(Logger::INFO, SlackRecord::COLOR_GOOD),
  28. array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
  29. array(Logger::WARNING, SlackRecord::COLOR_WARNING),
  30. array(Logger::ERROR, SlackRecord::COLOR_DANGER),
  31. array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
  32. array(Logger::ALERT, SlackRecord::COLOR_DANGER),
  33. array(Logger::EMERGENCY, SlackRecord::COLOR_DANGER),
  34. );
  35. }
  36. /**
  37. * @dataProvider dataGetAttachmentColor
  38. * @param int $logLevel
  39. * @param string $expectedColour RGB hex color or name of Slack color
  40. * @covers ::getAttachmentColor
  41. */
  42. public function testGetAttachmentColor($logLevel, $expectedColour)
  43. {
  44. $slackRecord = new SlackRecord();
  45. $this->assertSame(
  46. $expectedColour,
  47. $slackRecord->getAttachmentColor($logLevel)
  48. );
  49. }
  50. public function testAddsChannel()
  51. {
  52. $channel = '#test';
  53. $record = new SlackRecord($channel);
  54. $data = $record->getSlackData($this->getRecord());
  55. $this->assertArrayHasKey('channel', $data);
  56. $this->assertSame($channel, $data['channel']);
  57. }
  58. public function testNoUsernameByDefault()
  59. {
  60. $record = new SlackRecord();
  61. $data = $record->getSlackData($this->getRecord());
  62. $this->assertArrayNotHasKey('username', $data);
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function dataStringify()
  68. {
  69. $jsonPrettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
  70. $multipleDimensions = array(array(1, 2));
  71. $numericKeys = array('library' => 'monolog');
  72. $singleDimension = array(1, 'Hello', 'Jordi');
  73. return array(
  74. array(array(), '[]'),
  75. array($multipleDimensions, json_encode($multipleDimensions, $jsonPrettyPrintFlag)),
  76. array($numericKeys, json_encode($numericKeys, $jsonPrettyPrintFlag)),
  77. array($singleDimension, json_encode($singleDimension)),
  78. );
  79. }
  80. /**
  81. * @dataProvider dataStringify
  82. */
  83. public function testStringify($fields, $expectedResult)
  84. {
  85. $slackRecord = new SlackRecord(
  86. '#test',
  87. 'test',
  88. true,
  89. null,
  90. true,
  91. true
  92. );
  93. $this->assertSame($expectedResult, $slackRecord->stringify($fields));
  94. }
  95. public function testAddsCustomUsername()
  96. {
  97. $username = 'Monolog bot';
  98. $record = new SlackRecord(null, $username);
  99. $data = $record->getSlackData($this->getRecord());
  100. $this->assertArrayHasKey('username', $data);
  101. $this->assertSame($username, $data['username']);
  102. }
  103. public function testNoIcon()
  104. {
  105. $record = new SlackRecord();
  106. $data = $record->getSlackData($this->getRecord());
  107. $this->assertArrayNotHasKey('icon_emoji', $data);
  108. }
  109. public function testAddsIcon()
  110. {
  111. $record = $this->getRecord();
  112. $slackRecord = new SlackRecord(null, null, false, 'ghost');
  113. $data = $slackRecord->getSlackData($record);
  114. $slackRecord2 = new SlackRecord(null, null, false, 'http://github.com/Seldaek/monolog');
  115. $data2 = $slackRecord2->getSlackData($record);
  116. $this->assertArrayHasKey('icon_emoji', $data);
  117. $this->assertSame(':ghost:', $data['icon_emoji']);
  118. $this->assertArrayHasKey('icon_url', $data2);
  119. $this->assertSame('http://github.com/Seldaek/monolog', $data2['icon_url']);
  120. }
  121. public function testAttachmentsNotPresentIfNoAttachment()
  122. {
  123. $record = new SlackRecord(null, null, false);
  124. $data = $record->getSlackData($this->getRecord());
  125. $this->assertArrayNotHasKey('attachments', $data);
  126. }
  127. public function testAddsOneAttachment()
  128. {
  129. $record = new SlackRecord();
  130. $data = $record->getSlackData($this->getRecord());
  131. $this->assertArrayHasKey('attachments', $data);
  132. $this->assertArrayHasKey(0, $data['attachments']);
  133. $this->assertInternalType('array', $data['attachments'][0]);
  134. }
  135. public function testTextEqualsMessageIfNoAttachment()
  136. {
  137. $message = 'Test message';
  138. $record = new SlackRecord(null, null, false);
  139. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  140. $this->assertArrayHasKey('text', $data);
  141. $this->assertSame($message, $data['text']);
  142. }
  143. public function testTextEqualsFormatterOutput()
  144. {
  145. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  146. $formatter
  147. ->expects($this->any())
  148. ->method('format')
  149. ->will($this->returnCallback(function ($record) {
  150. return $record['message'] . 'test';
  151. }));
  152. $formatter2 = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  153. $formatter2
  154. ->expects($this->any())
  155. ->method('format')
  156. ->will($this->returnCallback(function ($record) {
  157. return $record['message'] . 'test1';
  158. }));
  159. $message = 'Test message';
  160. $record = new SlackRecord(null, null, false, null, false, false, array(), $formatter);
  161. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  162. $this->assertArrayHasKey('text', $data);
  163. $this->assertSame($message . 'test', $data['text']);
  164. $record->setFormatter($formatter2);
  165. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  166. $this->assertArrayHasKey('text', $data);
  167. $this->assertSame($message . 'test1', $data['text']);
  168. }
  169. public function testAddsFallbackAndTextToAttachment()
  170. {
  171. $message = 'Test message';
  172. $record = new SlackRecord(null);
  173. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  174. $this->assertSame($message, $data['attachments'][0]['text']);
  175. $this->assertSame($message, $data['attachments'][0]['fallback']);
  176. }
  177. public function testMapsLevelToColorAttachmentColor()
  178. {
  179. $record = new SlackRecord(null);
  180. $errorLoggerRecord = $this->getRecord(Logger::ERROR);
  181. $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
  182. $warningLoggerRecord = $this->getRecord(Logger::WARNING);
  183. $infoLoggerRecord = $this->getRecord(Logger::INFO);
  184. $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
  185. $data = $record->getSlackData($errorLoggerRecord);
  186. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  187. $data = $record->getSlackData($emergencyLoggerRecord);
  188. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  189. $data = $record->getSlackData($warningLoggerRecord);
  190. $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
  191. $data = $record->getSlackData($infoLoggerRecord);
  192. $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
  193. $data = $record->getSlackData($debugLoggerRecord);
  194. $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
  195. }
  196. public function testAddsShortAttachmentWithoutContextAndExtra()
  197. {
  198. $level = Logger::ERROR;
  199. $levelName = Logger::getLevelName($level);
  200. $record = new SlackRecord(null, null, true, null, true);
  201. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  202. $attachment = $data['attachments'][0];
  203. $this->assertArrayHasKey('title', $attachment);
  204. $this->assertArrayHasKey('fields', $attachment);
  205. $this->assertSame($levelName, $attachment['title']);
  206. $this->assertSame(array(), $attachment['fields']);
  207. }
  208. public function testAddsShortAttachmentWithContextAndExtra()
  209. {
  210. $level = Logger::ERROR;
  211. $levelName = Logger::getLevelName($level);
  212. $context = array('test' => 1);
  213. $extra = array('tags' => array('web'));
  214. $record = new SlackRecord(null, null, true, null, true, true);
  215. $loggerRecord = $this->getRecord($level, 'test', $context);
  216. $loggerRecord['extra'] = $extra;
  217. $data = $record->getSlackData($loggerRecord);
  218. $attachment = $data['attachments'][0];
  219. $this->assertArrayHasKey('title', $attachment);
  220. $this->assertArrayHasKey('fields', $attachment);
  221. $this->assertCount(2, $attachment['fields']);
  222. $this->assertSame($levelName, $attachment['title']);
  223. $this->assertSame(
  224. array(
  225. array(
  226. 'title' => 'Extra',
  227. 'value' => sprintf('```%s```', json_encode($extra, $this->jsonPrettyPrintFlag)),
  228. 'short' => false,
  229. ),
  230. array(
  231. 'title' => 'Context',
  232. 'value' => sprintf('```%s```', json_encode($context, $this->jsonPrettyPrintFlag)),
  233. 'short' => false,
  234. ),
  235. ),
  236. $attachment['fields']
  237. );
  238. }
  239. public function testAddsLongAttachmentWithoutContextAndExtra()
  240. {
  241. $level = Logger::ERROR;
  242. $levelName = Logger::getLevelName($level);
  243. $record = new SlackRecord(null, null, true, null);
  244. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  245. $attachment = $data['attachments'][0];
  246. $this->assertArrayHasKey('title', $attachment);
  247. $this->assertArrayHasKey('fields', $attachment);
  248. $this->assertCount(1, $attachment['fields']);
  249. $this->assertSame('Message', $attachment['title']);
  250. $this->assertSame(
  251. array(array(
  252. 'title' => 'Level',
  253. 'value' => $levelName,
  254. 'short' => false,
  255. )),
  256. $attachment['fields']
  257. );
  258. }
  259. public function testAddsLongAttachmentWithContextAndExtra()
  260. {
  261. $level = Logger::ERROR;
  262. $levelName = Logger::getLevelName($level);
  263. $context = array('test' => 1);
  264. $extra = array('tags' => array('web'));
  265. $record = new SlackRecord(null, null, true, null, false, true);
  266. $loggerRecord = $this->getRecord($level, 'test', $context);
  267. $loggerRecord['extra'] = $extra;
  268. $data = $record->getSlackData($loggerRecord);
  269. $expectedFields = array(
  270. array(
  271. 'title' => 'Level',
  272. 'value' => $levelName,
  273. 'short' => false,
  274. ),
  275. array(
  276. 'title' => 'tags',
  277. 'value' => sprintf('```%s```', json_encode($extra['tags'])),
  278. 'short' => false,
  279. ),
  280. array(
  281. 'title' => 'test',
  282. 'value' => $context['test'],
  283. 'short' => false,
  284. ),
  285. );
  286. $attachment = $data['attachments'][0];
  287. $this->assertArrayHasKey('title', $attachment);
  288. $this->assertArrayHasKey('fields', $attachment);
  289. $this->assertCount(3, $attachment['fields']);
  290. $this->assertSame('Message', $attachment['title']);
  291. $this->assertSame(
  292. $expectedFields,
  293. $attachment['fields']
  294. );
  295. }
  296. public function testAddsTimestampToAttachment()
  297. {
  298. $record = $this->getRecord();
  299. $slackRecord = new SlackRecord();
  300. $data = $slackRecord->getSlackData($this->getRecord());
  301. $attachment = $data['attachments'][0];
  302. $this->assertArrayHasKey('ts', $attachment);
  303. $this->assertSame($record['datetime']->getTimestamp(), $attachment['ts']);
  304. }
  305. public function testExcludeExtraAndContextFields()
  306. {
  307. $record = $this->getRecord(
  308. Logger::WARNING,
  309. 'test',
  310. array('info' => array('library' => 'monolog', 'author' => 'Jordi'))
  311. );
  312. $record['extra'] = array('tags' => array('web', 'cli'));
  313. $slackRecord = new SlackRecord(null, null, true, null, false, true, array('context.info.library', 'extra.tags.1'));
  314. $data = $slackRecord->getSlackData($record);
  315. $attachment = $data['attachments'][0];
  316. $expected = array(
  317. array(
  318. 'title' => 'info',
  319. 'value' => sprintf('```%s```', json_encode(array('author' => 'Jordi'), $this->jsonPrettyPrintFlag)),
  320. 'short' => false,
  321. ),
  322. array(
  323. 'title' => 'tags',
  324. 'value' => sprintf('```%s```', json_encode(array('web'))),
  325. 'short' => false,
  326. ),
  327. );
  328. foreach ($expected as $field) {
  329. $this->assertNotFalse(array_search($field, $attachment['fields']));
  330. break;
  331. }
  332. }
  333. }