SlackRecordTest.php 13 KB

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