Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/rcrowe/Hippy/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace rcrowe\Hippy;

use rcrowe\Hippy\Transport\Guzzle;
use rcrowe\Hippy\Transport\APIVersion1;
use rcrowe\Hippy\Client;
use rcrowe\Hippy\Queue;
use rcrowe\Hippy\Message;
Expand Down Expand Up @@ -42,7 +42,7 @@ class Facade
public static function init($token, $room, $from, $transport = null)
{
if ($transport === null) {
$transport = new Guzzle($token, $room, $from);
$transport = new APIVersion1($token, $room, $from);
}

static::$client = new Client($transport);
Expand Down
65 changes: 65 additions & 0 deletions src/rcrowe/Hippy/Transport/APIVersion1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* PHP client for HipChat. Designed for incidental notifications from an application.
*
* @author Iñaki Abete <inakiabt@gmail.com>
* @copyright Copyright (c) 2015, Iñaki Abete.
* @license MIT
*/

namespace rcrowe\Hippy\Transport;

use InvalidArgumentException;
use rcrowe\Hippy\Message\MessageInterface;
use rcrowe\Hippy\Transport\Guzzle;
use Guzzle\Http\Client as Http;
use Guzzle\Http\ClientInterface as HttpInterface;

/**
* Uses Guzzle to send the message to Hipchat. Uses cURL.
*/
class APIVersion1 extends Guzzle
{
/**
* {@inheritdoc}
*/
protected $headers = array(
'Content-type' => 'application/x-www-form-urlencoded'
);

/**
* {@inheritdoc}
*/
public function __construct($token, $room, $from, $endpoint = 'https://api.hipchat.com/v1/')
{
parent::__construct($token, $room, $from, $endpoint);
}

/**
* Uri of the request URL to the Hipchat API.
*
* @return string
*/
protected function getUri()
{
return 'rooms/message?format=json&auth_token='.$this->getToken();
}

/**
* {@inheritdoc}
*/
protected function buildData(MessageInterface $message)
{
// Build up the data we are sending to Hipchat
return http_build_query(array(
'room_id' => $this->getRoom(),
'from' => $this->getFrom(),
'message' => $message->getMessage(),
'message_format' => $message->getMessageFormat(),
'notify' => $message->getNotification(),
'color' => $message->getBackgroundColor(),
'format' => 'json'
), '', '&');
}
}
55 changes: 55 additions & 0 deletions src/rcrowe/Hippy/Transport/APIVersion2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* PHP client for HipChat. Designed for incidental notifications from an application.
*
* @author Iñaki Abete <inakiabt@gmail.com>
* @copyright Copyright (c) 2015, Iñaki Abete.
* @license MIT
*/

namespace rcrowe\Hippy\Transport;

use InvalidArgumentException;
use rcrowe\Hippy\Message\MessageInterface;
use rcrowe\Hippy\Transport\Guzzle;
use Guzzle\Http\Client as Http;
use Guzzle\Http\ClientInterface as HttpInterface;

/**
* Uses Guzzle to send the message to Hipchat. Uses cURL.
*/
class APIVersion2 extends Guzzle
{
/**
* {@inheritdoc}
*/
public function __construct($token, $room, $from, $endpoint = 'https://api.hipchat.com/v2/')
{
parent::__construct($token, $room, $from, $endpoint);
}

/**
* Uri of the request URL to the Hipchat API.
*
* @return string
*/
protected function getUri()
{
return 'room/'.$this->getRoom().'/notification?auth_token='.$this->getToken();
}

/**
* {@inheritdoc}
*/
protected function buildData(MessageInterface $message)
{
// Build up the data we are sending to Hipchat
return json_encode(array(
'message' => $message->getMessage(),
'message_format' => $message->getMessageFormat(),
'notify' => $message->getNotification(),
'color' => $message->getBackgroundColor()
));
}
}
45 changes: 19 additions & 26 deletions src/rcrowe/Hippy/Transport/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Uses Guzzle to send the message to Hipchat. Uses cURL.
*/
class Guzzle implements TransportInterface
abstract class Guzzle implements TransportInterface
{
/**
* @var string
Expand Down Expand Up @@ -49,13 +49,13 @@ class Guzzle implements TransportInterface
* @var array
*/
protected $headers = array(
'Content-type' => 'application/x-www-form-urlencoded'
'Content-type' => 'application/json'
);

/**
* {@inheritdoc}
*/
public function __construct($token, $room, $from, $endpoint = 'https://api.hipchat.com/v1/')
public function __construct($token, $room, $from, $endpoint = null)
{
$this->token = $token;
$this->room = $room;
Expand Down Expand Up @@ -171,16 +171,6 @@ public function setHttp(HttpInterface $http)
$this->http = $http;
}

/**
* Uri of the request URL to the Hipchat API.
*
* @return string
*/
protected function getUri()
{
return 'rooms/message?format=json&auth_token='.$this->getToken();
}

/**
* {@inheritdoc}
*/
Expand All @@ -193,18 +183,21 @@ public function send(MessageInterface $message)
}
}

// Build up the data we are sending to Hipchat
$data = array(
'room_id' => $this->getRoom(),
'from' => $this->getFrom(),
'message' => $message->getMessage(),
'message_format' => $message->getMessageFormat(),
'notify' => $message->getNotification(),
'color' => $message->getBackgroundColor(),
'format' => 'json',
);
$data = http_build_query($data, '', '&');

return $this->http->post($this->getUri(), $this->getHeaders(), $data)->send();
return $this->http->post($this->getUri(), $this->getHeaders(), $this->buildData($message))->send();
}

/**
* Uri of the request URL to the Hipchat API.
*
* @return string
*/
abstract protected function getUri();

/**
* Build data message
*
* @param rcrowe\Hippy\Message\MessageInterface $message
* @return string
*/
abstract protected function buildData(MessageInterface $message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace rcrowe\Hippy\Tests\Client;

use rcrowe\Hippy\Client as Hippy;
use rcrowe\Hippy\Transport\Guzzle as Transport;
use rcrowe\Hippy\Transport\APIVersion1 as Transport;

class BasicTest extends \PHPUnit_Framework_TestCase
class APIVersion1BasicTest extends \PHPUnit_Framework_TestCase
{
public function testToken()
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testTransport()
{
$transport = new Transport(null, null, null);
$hippy = new Hippy($transport);
$this->assertEquals(get_class($hippy->getTransport()), 'rcrowe\Hippy\Transport\Guzzle');
$this->assertEquals(get_class($hippy->getTransport()), 'rcrowe\Hippy\Transport\APIVersion1');

$transport = new Transport(null, null, null);
$transport->helloWorld = 'egg';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Mockery as m;
use rcrowe\Hippy\Client as Hippy;
use rcrowe\Hippy\Transport\Guzzle;
use rcrowe\Hippy\Transport\APIVersion1;
use rcrowe\Hippy\Message;
use rcrowe\Hippy\Queue;

class SendTest extends \PHPUnit_Framework_TestCase
class APIVersion1SendTest extends \PHPUnit_Framework_TestCase
{
public function tearDown()
{
Expand All @@ -23,7 +23,7 @@ public function testSingleMessage()
$http = m::mock('Guzzle\Http\Client');
$http->shouldReceive('post')->andReturn($entity)->once();

$transport = new Guzzle('123', 'cog', 'vivalacrowe');
$transport = new APIVersion1('123', 'cog', 'vivalacrowe');
$transport->setHttp($http);
$guzzle = new Hippy($transport);

Expand All @@ -40,7 +40,7 @@ public function testQueuedMessages()
$http = m::mock('Guzzle\Http\Client');
$http->shouldReceive('post')->andReturn($entity)->twice();

$transport = new Guzzle('123', 'cog', 'vivalacrowe');
$transport = new APIVersion1('123', 'cog', 'vivalacrowe');
$transport->setHttp($http);
$hippy = new Hippy($transport);

Expand Down
62 changes: 62 additions & 0 deletions tests/client/APIVersion2BasicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace rcrowe\Hippy\Tests\Client;

use rcrowe\Hippy\Client as Hippy;
use rcrowe\Hippy\Transport\APIVersion2 as Transport;

class APIVersion2BasicTest extends \PHPUnit_Framework_TestCase
{
public function testToken()
{
$transport = new Transport(null, null, null);
$hippy = new Hippy($transport);
$this->assertNull($hippy->getToken());
$hippy->setToken('12345');
$this->assertEquals($hippy->getToken(), '12345');

$transport = new Transport('54321', null, null);
$hippy = new Hippy($transport);
$this->assertEquals($hippy->getToken(), '54321');
}

public function testRoom()
{
$transport = new Transport(null, null, null);
$hippy = new Hippy($transport);
$this->assertNull($hippy->getRoom());
$hippy->setRoom('general');
$this->assertEquals($hippy->getRoom(), 'general');

$transport = new Transport(null, 'chilli', null);
$hippy = new Hippy($transport);
$this->assertEquals($hippy->getRoom(), 'chilli');
}

public function testFrom()
{
$transport = new Transport(null, null, null);
$hippy = new Hippy($transport);
$this->assertNull($hippy->getFrom());
$hippy->setFrom('rcrowe');
$this->assertEquals($hippy->getFrom(), 'rcrowe');

$transport = new Transport(null, null, 'vivalacrowe');
$hippy = new Hippy($transport);
$this->assertEquals($hippy->getFrom(), 'vivalacrowe');
}

public function testTransport()
{
$transport = new Transport(null, null, null);
$hippy = new Hippy($transport);
$this->assertEquals(get_class($hippy->getTransport()), 'rcrowe\Hippy\Transport\APIVersion2');

$transport = new Transport(null, null, null);
$transport->helloWorld = 'egg';
$hippy = new Hippy($transport);
$hippy->setTransport($transport);

$this->assertEquals($hippy->getTransport()->helloWorld, 'egg');
}
}
53 changes: 53 additions & 0 deletions tests/client/APIVersion2SendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace rcrowe\Hippy\Tests\Client;

use Mockery as m;
use rcrowe\Hippy\Client as Hippy;
use rcrowe\Hippy\Transport\APIVersion2;
use rcrowe\Hippy\Message;
use rcrowe\Hippy\Queue;

class APIVersion2SendTest extends \PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}

public function testSingleMessage()
{
$entity = m::mock('Guzzle\Http\Message\EntityEnclosingRequest');
$entity->shouldReceive('send')->once();

$http = m::mock('Guzzle\Http\Client');
$http->shouldReceive('post')->andReturn($entity)->once();

$transport = new APIVersion2('123', 'cog', 'vivalacrowe');
$transport->setHttp($http);
$guzzle = new Hippy($transport);

$message = new Message(true, 'red');

$guzzle->send($message);
}

public function testQueuedMessages()
{
$entity = m::mock('Guzzle\Http\Message\EntityEnclosingRequest');
$entity->shouldReceive('send')->twice();

$http = m::mock('Guzzle\Http\Client');
$http->shouldReceive('post')->andReturn($entity)->twice();

$transport = new APIVersion2('123', 'cog', 'vivalacrowe');
$transport->setHttp($http);
$hippy = new Hippy($transport);

$queue = new Queue;
$queue->add(new Message(true, 'red'));
$queue->add(new Message(false, 'random'));

$hippy->send($queue);
}
}
Loading