1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-12 16:14:08 +02:00

Merge pull request #526 from esalter/master

Add Hipchat V2 support
This commit is contained in:
Jordi Boggiano
2015-06-19 11:46:55 +01:00
2 changed files with 112 additions and 19 deletions

View File

@@ -37,7 +37,7 @@ class HipChatHandlerTest extends TestCase
public function testWriteCustomHostHeader()
{
$this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar');
$this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar');
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
@@ -47,12 +47,69 @@ class HipChatHandlerTest extends TestCase
return $content;
}
public function testWriteV2() {
$this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
return $content;
}
public function testWriteV2Notify() {
$this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
return $content;
}
public function testRoomSpaces() {
$this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/POST \/v2\/room\/room%20name\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
return $content;
}
/**
* @depends testWriteHeader
*/
public function testWriteContent($content)
{
$this->assertRegexp('/from=Monolog&room_id=room1&notify=0&message=test1&message_format=text&color=red$/', $content);
$this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
}
/**
* @depends testWriteCustomHostHeader
*/
public function testWriteContentNotify($content)
{
$this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
}
/**
* @depends testWriteV2
*/
public function testWriteContentV2($content)
{
$this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
}
/**
* @depends testWriteV2Notify
*/
public function testWriteContentV2Notify($content)
{
$this->assertRegexp('/notify=true&message=test1&message_format=text&color=red$/', $content);
}
public function testWriteWithComplexMessage()
@@ -141,9 +198,9 @@ class HipChatHandlerTest extends TestCase
);
}
private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com')
private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
{
$constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host);
$constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
$this->res = fopen('php://memory', 'a');
$this->handler = $this->getMock(
'\Monolog\Handler\HipChatHandler',
@@ -175,4 +232,9 @@ class HipChatHandlerTest extends TestCase
{
$hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere');
}
public function testCreateWithTooLongNameV2() {
// creating a handler with too long of a name but using the v2 api doesn't matter.
$hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
}
}