SlackRecordTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. $formatter2 = $this->createMock(FormatterInterface::class);
  155. $formatter2
  156. ->expects($this->any())
  157. ->method('format')
  158. ->will($this->returnCallback(function ($record) { return $record['message'] . 'test1'; }));
  159. $message = 'Test message';
  160. $record = new SlackRecord($this->channel, 'Monolog', false, null, false, false, $formatter);
  161. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  162. $this->assertArrayHasKey('text', $data);
  163. $this->assertSame($message . 'test', $data['text']);
  164. $record->setFormatter($formatter2);
  165. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  166. $this->assertArrayHasKey('text', $data);
  167. $this->assertSame($message . 'test1', $data['text']);
  168. }
  169. public function testAddsFallbackAndTextToAttachment()
  170. {
  171. $message = 'Test message';
  172. $record = new SlackRecord($this->channel);
  173. $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
  174. $this->assertSame($message, $data['attachments'][0]['text']);
  175. $this->assertSame($message, $data['attachments'][0]['fallback']);
  176. }
  177. public function testMapsLevelToColorAttachmentColor()
  178. {
  179. $record = new SlackRecord($this->channel);
  180. $errorLoggerRecord = $this->getRecord(Logger::ERROR);
  181. $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
  182. $warningLoggerRecord = $this->getRecord(Logger::WARNING);
  183. $infoLoggerRecord = $this->getRecord(Logger::INFO);
  184. $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
  185. $data = $record->getSlackData($errorLoggerRecord);
  186. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  187. $data = $record->getSlackData($emergencyLoggerRecord);
  188. $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
  189. $data = $record->getSlackData($warningLoggerRecord);
  190. $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
  191. $data = $record->getSlackData($infoLoggerRecord);
  192. $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
  193. $data = $record->getSlackData($debugLoggerRecord);
  194. $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
  195. }
  196. public function testAddsShortAttachmentWithoutContextAndExtra()
  197. {
  198. $level = Logger::ERROR;
  199. $levelName = Logger::getLevelName($level);
  200. $record = new SlackRecord($this->channel, 'Monolog', true, null, true);
  201. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  202. $attachment = $data['attachments'][0];
  203. $this->assertArrayHasKey('title', $attachment);
  204. $this->assertArrayHasKey('fields', $attachment);
  205. $this->assertSame($levelName, $attachment['title']);
  206. $this->assertSame(array(), $attachment['fields']);
  207. }
  208. public function testAddsShortAttachmentWithContextAndExtra()
  209. {
  210. $level = Logger::ERROR;
  211. $levelName = Logger::getLevelName($level);
  212. $record = new SlackRecord($this->channel, 'Monolog', true, null, true, true);
  213. $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
  214. $loggerRecord['extra'] = array('tags' => array('web'));
  215. $data = $record->getSlackData($loggerRecord);
  216. $attachment = $data['attachments'][0];
  217. $this->assertArrayHasKey('title', $attachment);
  218. $this->assertArrayHasKey('fields', $attachment);
  219. $this->assertCount(2, $attachment['fields']);
  220. $this->assertSame($levelName, $attachment['title']);
  221. $this->assertSame(
  222. array(
  223. array(
  224. 'title' => 'Extra',
  225. 'value' => 'tags: ["web"]',
  226. 'short' => true
  227. ),
  228. array(
  229. 'title' => 'Context',
  230. 'value' => 'test: 1',
  231. 'short' => true
  232. )
  233. ),
  234. $attachment['fields']
  235. );
  236. }
  237. public function testAddsLongAttachmentWithoutContextAndExtra()
  238. {
  239. $level = Logger::ERROR;
  240. $levelName = Logger::getLevelName($level);
  241. $record = new SlackRecord($this->channel, 'Monolog', true, null);
  242. $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
  243. $attachment = $data['attachments'][0];
  244. $this->assertArrayHasKey('title', $attachment);
  245. $this->assertArrayHasKey('fields', $attachment);
  246. $this->assertCount(1, $attachment['fields']);
  247. $this->assertSame('Message', $attachment['title']);
  248. $this->assertSame(
  249. array(array(
  250. 'title' => 'Level',
  251. 'value' => $levelName,
  252. 'short' => true
  253. )),
  254. $attachment['fields']
  255. );
  256. }
  257. public function testAddsLongAttachmentWithContextAndExtra()
  258. {
  259. $level = Logger::ERROR;
  260. $levelName = Logger::getLevelName($level);
  261. $record = new SlackRecord($this->channel, 'Monolog', true, null, false, true);
  262. $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
  263. $loggerRecord['extra'] = array('tags' => array('web'));
  264. $data = $record->getSlackData($loggerRecord);
  265. $expectedFields = array(
  266. array(
  267. 'title' => 'Level',
  268. 'value' => $levelName,
  269. 'short' => true,
  270. ),
  271. array(
  272. 'title' => 'tags',
  273. 'value' => '["web"]',
  274. 'short' => false
  275. ),
  276. array(
  277. 'title' => 'test',
  278. 'value' => 1,
  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. }