SlackRecordTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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\Formatter\FormatterInterface;
  12. use Monolog\Logger;
  13. use Monolog\TestCase;
  14. /**
  15. * @coversDefaultClass Monolog\Handler\Slack\SlackRecord
  16. */
  17. class SlackRecordTest extends TestCase
  18. {
  19. private $channel;
  20. protected function setUp()
  21. {
  22. $this->channel = 'monolog_alerts';
  23. }
  24. public function dataGetAttachmentColor()
  25. {
  26. return array(
  27. array(Logger::DEBUG, SlackRecord::COLOR_DEFAULT),
  28. array(Logger::INFO, SlackRecord::COLOR_GOOD),
  29. array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
  30. array(Logger::WARNING, SlackRecord::COLOR_WARNING),
  31. array(Logger::ERROR, SlackRecord::COLOR_DANGER),
  32. array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
  33. array(Logger::ALERT, SlackRecord::COLOR_DANGER),
  34. array(Logger::EMERGENCY, SlackRecord::COLOR_DANGER),
  35. );
  36. }
  37. /**
  38. * @dataProvider dataGetAttachmentColor
  39. * @param int $logLevel
  40. * @param string $expectedColour RGB hex color or name of Slack color
  41. * @covers ::getAttachmentColor
  42. */
  43. public function testGetAttachmentColor($logLevel, $expectedColour)
  44. {
  45. $slackRecord = new SlackRecord('#test');
  46. $this->assertSame(
  47. $expectedColour,
  48. $slackRecord->getAttachmentColor($logLevel)
  49. );
  50. }
  51. public function testAddsChannel()
  52. {
  53. $record = new SlackRecord($this->channel);
  54. $data = $record->getSlackData($this->getRecord());
  55. $this->assertArrayHasKey('channel', $data);
  56. $this->assertSame($this->channel, $data['channel']);
  57. }
  58. public function testStringifyReturnsNullWithNoLineFormatter()
  59. {
  60. $slackRecord = new SlackRecord('#test');
  61. $this->assertNull($slackRecord->stringify(array('foo' => 'bar')));
  62. }
  63. public function testAddsDefaultUsername()
  64. {
  65. $record = new SlackRecord($this->channel);
  66. $data = $record->getSlackData($this->getRecord());
  67. $this->assertArrayHasKey('username', $data);
  68. $this->assertSame('Monolog', $data['username']);
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function dataStringify()
  74. {
  75. return array(
  76. array(array(), ''),
  77. array(array('foo' => 'bar'), 'foo: bar'),
  78. array(array('Foo' => 'bAr'), 'Foo: bAr'),
  79. );
  80. }
  81. /**
  82. * @dataProvider dataStringify
  83. */
  84. public function testStringifyWithLineFormatter($fields, $expectedResult)
  85. {
  86. $slackRecord = new SlackRecord(
  87. '#test',
  88. 'test',
  89. true,
  90. null,
  91. true,
  92. true
  93. );
  94. $this->assertSame($expectedResult, $slackRecord->stringify($fields));
  95. }
  96. public function testAddsCustomUsername()
  97. {
  98. $username = 'Monolog bot';
  99. $record = new SlackRecord($this->channel, $username);
  100. $data = $record->getSlackData($this->getRecord());
  101. $this->assertArrayHasKey('username', $data);
  102. $this->assertSame($username, $data['username']);
  103. }
  104. public function testNoIcon()
  105. {
  106. $record = new SlackRecord($this->channel);
  107. $data = $record->getSlackData($this->getRecord());
  108. $this->assertArrayNotHasKey('icon_emoji', $data);
  109. }
  110. public function testAddsIcon()
  111. {
  112. $record = new SlackRecord($this->channel, 'Monolog', true, 'ghost');
  113. $data = $record->getSlackData($this->getRecord());
  114. $this->assertArrayHasKey('icon_emoji', $data);
  115. $this->assertSame(':ghost:', $data['icon_emoji']);
  116. }
  117. public function testAddsEmptyTextIfUseAttachment()
  118. {
  119. $record = new SlackRecord($this->channel);
  120. $data = $record->getSlackData($this->getRecord());
  121. $this->assertArrayHasKey('text', $data);
  122. $this->assertSame('', $data['text']);
  123. }
  124. public function testAttachmentsEmptyIfNoAttachment()
  125. {
  126. $record = new SlackRecord($this->channel, 'Monolog', false);
  127. $data = $record->getSlackData($this->getRecord());
  128. $this->assertArrayHasKey('attachments', $data);
  129. $this->assertSame(array(), $data['attachments']);
  130. }
  131. public function testAddsOneAttachment()
  132. {
  133. $record = new SlackRecord($this->channel);
  134. $data = $record->getSlackData($this->getRecord());
  135. $this->assertArrayHasKey('attachments', $data);
  136. $this->assertArrayHasKey(0, $data['attachments']);
  137. $this->assertInternalType('array', $data['attachments'][0]);
  138. }
  139. public function testTextEqualsMessageIfNoFormatter()
  140. {
  141. $message = 'Test message';
  142. $record = new SlackRecord($this->channel, 'Monolog', false);
  143. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  144. $this->assertArrayHasKey('text', $data);
  145. $this->assertSame($message, $data['text']);
  146. }
  147. public function testTextEqualsFormatterOutput()
  148. {
  149. $formatter = $this->createMock(FormatterInterface::class);
  150. $formatter
  151. ->expects($this->any())
  152. ->method('format')
  153. ->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
  154. $message = 'Test message';
  155. $record = new SlackRecord($this->channel, 'Monolog', false, null, false, false, $formatter);
  156. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  157. $this->assertArrayHasKey('text', $data);
  158. $this->assertSame($message . 'test', $data['text']);
  159. }
  160. public function testAddsFallbackAndTextToAttachment()
  161. {
  162. $message = 'Test message';
  163. $record = new SlackRecord($this->channel);
  164. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  165. $this->assertSame($message, $data['attachments'][0]['text']);
  166. $this->assertSame($message, $data['attachments'][0]['fallback']);
  167. }
  168. public function testMapsLevelToColorAttachmentColor()
  169. {
  170. $record = new SlackRecord($this->channel);
  171. $errorLoggerRecord = $this->getRecord(Logger::ERROR);
  172. $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
  173. $warningLoggerRecord = $this->getRecord(Logger::WARNING);
  174. $infoLoggerRecord = $this->getRecord(Logger::INFO);
  175. $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
  176. $data = $record->getSlackData($errorLoggerRecord);
  177. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  178. $data = $record->getSlackData($emergencyLoggerRecord);
  179. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  180. $data = $record->getSlackData($warningLoggerRecord);
  181. $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
  182. $data = $record->getSlackData($infoLoggerRecord);
  183. $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
  184. $data = $record->getSlackData($debugLoggerRecord);
  185. $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
  186. }
  187. public function testAddsShortAttachmentWithoutContextAndExtra()
  188. {
  189. $level = Logger::ERROR;
  190. $levelName = Logger::getLevelName($level);
  191. $record = new SlackRecord($this->channel, 'Monolog', true, null, true);
  192. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  193. $attachment = $data['attachments'][0];
  194. $this->assertArrayHasKey('title', $attachment);
  195. $this->assertArrayHasKey('fields', $attachment);
  196. $this->assertSame($levelName, $attachment['title']);
  197. $this->assertSame(array(), $attachment['fields']);
  198. }
  199. public function testAddsShortAttachmentWithContextAndExtra()
  200. {
  201. $level = Logger::ERROR;
  202. $levelName = Logger::getLevelName($level);
  203. $record = new SlackRecord($this->channel, 'Monolog', true, null, true, true);
  204. $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
  205. $loggerRecord['extra'] = array('tags' => array('web'));
  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. array(
  214. array(
  215. 'title' => 'Extra',
  216. 'value' => 'tags: ["web"]',
  217. 'short' => true
  218. ),
  219. array(
  220. 'title' => 'Context',
  221. 'value' => 'test: 1',
  222. 'short' => true
  223. )
  224. ),
  225. $attachment['fields']
  226. );
  227. }
  228. public function testAddsLongAttachmentWithoutContextAndExtra()
  229. {
  230. $level = Logger::ERROR;
  231. $levelName = Logger::getLevelName($level);
  232. $record = new SlackRecord($this->channel, 'Monolog', true, null);
  233. $data = $record->getSlackData($this->getRecord($level, 'test', array('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. array(array(
  241. 'title' => 'Level',
  242. 'value' => $levelName,
  243. 'short' => true
  244. )),
  245. $attachment['fields']
  246. );
  247. }
  248. public function testAddsLongAttachmentWithContextAndExtra()
  249. {
  250. $level = Logger::ERROR;
  251. $levelName = Logger::getLevelName($level);
  252. $record = new SlackRecord($this->channel, 'Monolog', true, null, false, true);
  253. $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
  254. $loggerRecord['extra'] = array('tags' => array('web'));
  255. $data = $record->getSlackData($loggerRecord);
  256. $expectedFields = array(
  257. array(
  258. 'title' => 'Level',
  259. 'value' => $levelName,
  260. 'short' => true,
  261. ),
  262. array(
  263. 'title' => 'tags',
  264. 'value' => array('web'),
  265. 'short' => false
  266. ),
  267. array(
  268. 'title' => 'test',
  269. 'value' => 1,
  270. 'short' => false
  271. )
  272. );
  273. $attachment = $data['attachments'][0];
  274. $this->assertArrayHasKey('title', $attachment);
  275. $this->assertArrayHasKey('fields', $attachment);
  276. $this->assertCount(3, $attachment['fields']);
  277. $this->assertSame('Message', $attachment['title']);
  278. $this->assertSame(
  279. $expectedFields,
  280. $attachment['fields']
  281. );
  282. }
  283. }