SlackRecordTest.php 13 KB

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