mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 20:57:36 +02:00
Update SlackHandler tests && allow SlackRecord formatter change after construction
This commit is contained in:
committed by
Haralan Dobrev
parent
01a2ac25a2
commit
08b577c657
@@ -226,4 +226,14 @@ class SlackRecord
|
|||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the formatter
|
||||||
|
*
|
||||||
|
* @param FormatterInterface $formatter
|
||||||
|
*/
|
||||||
|
public function setFormatter(FormatterInterface $formatter)
|
||||||
|
{
|
||||||
|
$this->formatter = $formatter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
|
use Monolog\Formatter\FormatterInterface;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\Slack\SlackRecord;
|
use Monolog\Handler\Slack\SlackRecord;
|
||||||
|
|
||||||
@@ -178,4 +179,12 @@ class SlackHandler extends SocketHandler
|
|||||||
|
|
||||||
return $this->slackRecord->stringify($fields);
|
return $this->slackRecord->stringify($fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFormatter(FormatterInterface $formatter)
|
||||||
|
{
|
||||||
|
parent::setFormatter($formatter);
|
||||||
|
$this->slackRecord->setFormatter($formatter);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -181,12 +181,24 @@ class SlackRecordTest extends TestCase
|
|||||||
->method('format')
|
->method('format')
|
||||||
->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
|
->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
|
||||||
|
|
||||||
|
$formatter2 = $this->createMock(FormatterInterface::class);
|
||||||
|
$formatter2
|
||||||
|
->expects($this->any())
|
||||||
|
->method('format')
|
||||||
|
->will($this->returnCallback(function ($record) { return $record['message'] . 'test1'; }));
|
||||||
|
|
||||||
$message = 'Test message';
|
$message = 'Test message';
|
||||||
$record = new SlackRecord($this->channel, 'Monolog', false, null, false, false, $formatter);
|
$record = new SlackRecord($this->channel, 'Monolog', false, null, false, false, $formatter);
|
||||||
$data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
|
$data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
|
||||||
|
|
||||||
$this->assertArrayHasKey('text', $data);
|
$this->assertArrayHasKey('text', $data);
|
||||||
$this->assertSame($message . 'test', $data['text']);
|
$this->assertSame($message . 'test', $data['text']);
|
||||||
|
|
||||||
|
$record->setFormatter($formatter2);
|
||||||
|
$data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('text', $data);
|
||||||
|
$this->assertSame($message . 'test1', $data['text']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddsFallbackAndTextToAttachment()
|
public function testAddsFallbackAndTextToAttachment()
|
||||||
|
@@ -56,7 +56,10 @@ class SlackHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
|
$this->assertRegExp('/username=Monolog/', $content);
|
||||||
|
$this->assertRegExp('/channel=channel1/', $content);
|
||||||
|
$this->assertRegExp('/token=myToken/', $content);
|
||||||
|
$this->assertRegExp('/attachments/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteContentUsesFormatterIfProvided()
|
public function testWriteContentUsesFormatterIfProvided()
|
||||||
@@ -72,8 +75,8 @@ class SlackHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content2 = fread($this->res, 1024);
|
$content2 = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
|
$this->assertRegexp('/text=test1/', $content);
|
||||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
|
$this->assertRegexp('/text=foo--test2/', $content2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteContentWithEmoji()
|
public function testWriteContentWithEmoji()
|
||||||
@@ -83,7 +86,7 @@ class SlackHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
|
$this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,7 +99,7 @@ class SlackHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
|
$this->assertRegexp('/%5Bcolor%5D='.$expectedColor.'/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteContentWithPlainTextMessage()
|
public function testWriteContentWithPlainTextMessage()
|
||||||
|
Reference in New Issue
Block a user