SlackRecordTest.php 13 KB

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