SlackRecordTest.php 13 KB

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