diff --git a/src/Monolog/Handler/HipChatHandler.php b/src/Monolog/Handler/HipChatHandler.php index 0074348e..2e0da0e6 100644 --- a/src/Monolog/Handler/HipChatHandler.php +++ b/src/Monolog/Handler/HipChatHandler.php @@ -146,7 +146,12 @@ class HipChatHandler extends SocketHandler // if we are using the legacy API then we need to send some additional information if ($this->version == self::API_V1) { $dataArray['room_id'] = $this->room; - $dataArray['from'] = $this->name; + } + + // append the sender name if it is set + // always append it if we use the v1 api (it is required in v1) + if ($this->version == self::API_V1 || $this->name !== null) { + $dataArray['from'] = (string) $this->name; } return http_build_query($dataArray); diff --git a/tests/Monolog/Handler/HipChatHandlerTest.php b/tests/Monolog/Handler/HipChatHandlerTest.php index 7a3d1b07..e45f57de 100644 --- a/tests/Monolog/Handler/HipChatHandlerTest.php +++ b/tests/Monolog/Handler/HipChatHandlerTest.php @@ -21,6 +21,7 @@ use Monolog\Logger; class HipChatHandlerTest extends TestCase { private $res; + /** @var HipChatHandler */ private $handler; public function testWriteHeader() @@ -91,6 +92,18 @@ class HipChatHandlerTest extends TestCase $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content); } + public function testWriteContentV1WithoutName() + { + $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v1'); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=$/', $content); + + return $content; + } + /** * @depends testWriteCustomHostHeader */ @@ -104,7 +117,7 @@ class HipChatHandlerTest extends TestCase */ public function testWriteContentV2($content) { - $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content); + $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content); } /** @@ -112,7 +125,19 @@ class HipChatHandlerTest extends TestCase */ public function testWriteContentV2Notify($content) { - $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red$/', $content); + $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content); + } + + public function testWriteContentV2WithoutName() + { + $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2'); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content); + + return $content; } public function testWriteWithComplexMessage()