SlackRecordTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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\Slack;
  11. use Monolog\Logger;
  12. use Monolog\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) { return $record['message'] . 'test'; }));
  150. $formatter2 = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  151. $formatter2
  152. ->expects($this->any())
  153. ->method('format')
  154. ->will($this->returnCallback(function ($record) { return $record['message'] . 'test1'; }));
  155. $message = 'Test message';
  156. $record = new SlackRecord(null, null, false, null, false, false, array(), $formatter);
  157. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  158. $this->assertArrayHasKey('text', $data);
  159. $this->assertSame($message . 'test', $data['text']);
  160. $record->setFormatter($formatter2);
  161. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  162. $this->assertArrayHasKey('text', $data);
  163. $this->assertSame($message . 'test1', $data['text']);
  164. }
  165. public function testAddsFallbackAndTextToAttachment()
  166. {
  167. $message = 'Test message';
  168. $record = new SlackRecord(null);
  169. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  170. $this->assertSame($message, $data['attachments'][0]['text']);
  171. $this->assertSame($message, $data['attachments'][0]['fallback']);
  172. }
  173. public function testMapsLevelToColorAttachmentColor()
  174. {
  175. $record = new SlackRecord(null);
  176. $errorLoggerRecord = $this->getRecord(Logger::ERROR);
  177. $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
  178. $warningLoggerRecord = $this->getRecord(Logger::WARNING);
  179. $infoLoggerRecord = $this->getRecord(Logger::INFO);
  180. $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
  181. $data = $record->getSlackData($errorLoggerRecord);
  182. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  183. $data = $record->getSlackData($emergencyLoggerRecord);
  184. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  185. $data = $record->getSlackData($warningLoggerRecord);
  186. $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
  187. $data = $record->getSlackData($infoLoggerRecord);
  188. $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
  189. $data = $record->getSlackData($debugLoggerRecord);
  190. $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
  191. }
  192. public function testAddsShortAttachmentWithoutContextAndExtra()
  193. {
  194. $level = Logger::ERROR;
  195. $levelName = Logger::getLevelName($level);
  196. $record = new SlackRecord(null, null, true, null, true);
  197. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  198. $attachment = $data['attachments'][0];
  199. $this->assertArrayHasKey('title', $attachment);
  200. $this->assertArrayHasKey('fields', $attachment);
  201. $this->assertSame($levelName, $attachment['title']);
  202. $this->assertSame(array(), $attachment['fields']);
  203. }
  204. public function testAddsShortAttachmentWithContextAndExtra()
  205. {
  206. $level = Logger::ERROR;
  207. $levelName = Logger::getLevelName($level);
  208. $context = array('test' => 1);
  209. $extra = array('tags' => array('web'));
  210. $record = new SlackRecord(null, null, true, null, true, true);
  211. $loggerRecord = $this->getRecord($level, 'test', $context);
  212. $loggerRecord['extra'] = $extra;
  213. $data = $record->getSlackData($loggerRecord);
  214. $attachment = $data['attachments'][0];
  215. $this->assertArrayHasKey('title', $attachment);
  216. $this->assertArrayHasKey('fields', $attachment);
  217. $this->assertCount(2, $attachment['fields']);
  218. $this->assertSame($levelName, $attachment['title']);
  219. $this->assertSame(
  220. array(
  221. array(
  222. 'title' => 'Extra',
  223. 'value' => sprintf('```%s```', json_encode($extra, $this->jsonPrettyPrintFlag)),
  224. 'short' => false
  225. ),
  226. array(
  227. 'title' => 'Context',
  228. 'value' => sprintf('```%s```', json_encode($context, $this->jsonPrettyPrintFlag)),
  229. 'short' => false
  230. )
  231. ),
  232. $attachment['fields']
  233. );
  234. }
  235. public function testAddsLongAttachmentWithoutContextAndExtra()
  236. {
  237. $level = Logger::ERROR;
  238. $levelName = Logger::getLevelName($level);
  239. $record = new SlackRecord(null, null, true, null);
  240. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  241. $attachment = $data['attachments'][0];
  242. $this->assertArrayHasKey('title', $attachment);
  243. $this->assertArrayHasKey('fields', $attachment);
  244. $this->assertCount(1, $attachment['fields']);
  245. $this->assertSame('Message', $attachment['title']);
  246. $this->assertSame(
  247. array(array(
  248. 'title' => 'Level',
  249. 'value' => $levelName,
  250. 'short' => false
  251. )),
  252. $attachment['fields']
  253. );
  254. }
  255. public function testAddsLongAttachmentWithContextAndExtra()
  256. {
  257. $level = Logger::ERROR;
  258. $levelName = Logger::getLevelName($level);
  259. $context = array('test' => 1);
  260. $extra = array('tags' => array('web'));
  261. $record = new SlackRecord(null, null, true, null, false, true);
  262. $loggerRecord = $this->getRecord($level, 'test', $context);
  263. $loggerRecord['extra'] = $extra;
  264. $data = $record->getSlackData($loggerRecord);
  265. $expectedFields = array(
  266. array(
  267. 'title' => 'Level',
  268. 'value' => $levelName,
  269. 'short' => false,
  270. ),
  271. array(
  272. 'title' => 'tags',
  273. 'value' => sprintf('```%s```', json_encode($extra['tags'])),
  274. 'short' => false
  275. ),
  276. array(
  277. 'title' => 'test',
  278. 'value' => $context['test'],
  279. 'short' => false
  280. )
  281. );
  282. $attachment = $data['attachments'][0];
  283. $this->assertArrayHasKey('title', $attachment);
  284. $this->assertArrayHasKey('fields', $attachment);
  285. $this->assertCount(3, $attachment['fields']);
  286. $this->assertSame('Message', $attachment['title']);
  287. $this->assertSame(
  288. $expectedFields,
  289. $attachment['fields']
  290. );
  291. }
  292. public function testAddsTimestampToAttachment()
  293. {
  294. $record = $this->getRecord();
  295. $slackRecord = new SlackRecord();
  296. $data = $slackRecord->getSlackData($this->getRecord());
  297. $attachment = $data['attachments'][0];
  298. $this->assertArrayHasKey('ts', $attachment);
  299. $this->assertSame($record['datetime']->getTimestamp(), $attachment['ts']);
  300. }
  301. public function testContextHasException()
  302. {
  303. $record = $this->getRecord(Logger::CRITICAL, 'This is a critical message.', array('exception' => new \Exception()));
  304. $slackRecord = new SlackRecord(null, null, true, null, false, true);
  305. $data = $slackRecord->getSlackData($record);
  306. $this->assertInternalType('string', $data['attachments'][0]['fields'][1]['value']);
  307. }
  308. public function testExcludeExtraAndContextFields()
  309. {
  310. $record = $this->getRecord(
  311. Logger::WARNING,
  312. 'test',
  313. array('info' => array('library' => 'monolog', 'author' => 'Jordi'))
  314. );
  315. $record['extra'] = array('tags' => array('web', 'cli'));
  316. $slackRecord = new SlackRecord(null, null, true, null, false, true, array('context.info.library', 'extra.tags.1'));
  317. $data = $slackRecord->getSlackData($record);
  318. $attachment = $data['attachments'][0];
  319. $expected = array(
  320. array(
  321. 'title' => 'info',
  322. 'value' => sprintf('```%s```', json_encode(array('author' => 'Jordi'), $this->jsonPrettyPrintFlag)),
  323. 'short' => false
  324. ),
  325. array(
  326. 'title' => 'tags',
  327. 'value' => sprintf('```%s```', json_encode(array('web'))),
  328. 'short' => false
  329. ),
  330. );
  331. foreach ($expected as $field) {
  332. $this->assertNotFalse(array_search($field, $attachment['fields']));
  333. break;
  334. }
  335. }
  336. }