Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add basic PHPUnit test. Rename example script.

  • Loading branch information...
commit 03fec4900f8ad571245538ba4ed22ed74e89a882 1 parent 8696174
@powdahound powdahound authored
View
62 HipChat.php
@@ -49,7 +49,7 @@ function __construct($api_token, $api_target = self::DEFAULT_TARGET,
/////////////////////////////////////////////////////////////////////////////
- // Public functions
+ // Room functions
/////////////////////////////////////////////////////////////////////////////
/**
@@ -75,6 +75,26 @@ public function get_rooms() {
}
/**
+ * Send a message to a room
+ *
+ * @see http://api.hipchat.com/docs/api/method/rooms/message
+ */
+ public function message_room($room_id, $from, $message) {
+ $args = array(
+ 'room_id' => $room_id,
+ 'from' => $from,
+ 'message' => utf8_encode($message)
+ );
+ $response = $this->make_request("rooms/message", $args);
+ return ($response->status == 'sent');
+ }
+
+
+ /////////////////////////////////////////////////////////////////////////////
+ // User functions
+ /////////////////////////////////////////////////////////////////////////////
+
+ /**
* Get information about a user
*
* @see http://api.hipchat.com/docs/api/method/users/show
@@ -96,27 +116,19 @@ public function get_users() {
return $response->users;
}
- /**
- * Send a message to a room
- *
- * @see http://api.hipchat.com/docs/api/method/rooms/message
- */
- public function message_room($room_id, $from, $message) {
- $args = array(
- 'room_id' => $room_id,
- 'from' => $from,
- 'message' => utf8_encode($message)
- );
- $response = $this->make_request("rooms/message", $args);
- return ($response->status == 'sent');
- }
-
/////////////////////////////////////////////////////////////////////////////
- // Private functions
+ // Helper functions
/////////////////////////////////////////////////////////////////////////////
- private function curl_request($url, $headers = array(), $post_data = null) {
+ /**
+ * Performs a curl request
+ *
+ * @param $url URL to hit.
+ * @param $headers Array of HTTP headers.
+ * @param $post_data Data to send via POST. Leave null for GET request.
+ */
+ public function curl_request($url, $headers = array(), $post_data = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@@ -135,11 +147,11 @@ private function curl_request($url, $headers = array(), $post_data = null) {
throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
"CURL error: $errno - $error", $url);
}
-
+
// make sure we got a 200
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != self::STATUS_OK) {
- throw new HipChat_Exception(self::STATUS_BAD_RESPONSE,
+ throw new HipChat_Exception($code,
"HTTP status code: $code, response=$response", $url);
}
@@ -151,12 +163,12 @@ private function curl_request($url, $headers = array(), $post_data = null) {
/**
* Make an API request using curl
*
- * @param $api_method Which API method to hit, like 'rooms/show'.
- * @param $args Data to send.
- * @param $http_method HTTP method (GET or POST).
+ * @param $api_method Which API method to hit, like 'rooms/show'.
+ * @param $args Data to send.
+ * @param $http_method HTTP method (GET or POST).
*/
- private function make_request($api_method, $args = array(),
- $http_method = 'GET') {
+ public function make_request($api_method, $args = array(),
+ $http_method = 'GET') {
$args['format'] = 'json';
$headers = array("Authorization: HipChat $this->api_token");
$url = "$this->api_target/$this->api_version/$api_method";
View
14 README.md
@@ -5,18 +5,24 @@ A PHP library for interacting with the [HipChat][hc] REST API.
Testing
-------
-You can test this library (and your API key) using the test.php script as follows. It should print a list of your rooms and users.
+You can test this library (and your API key) using the example.php script as follows. It should print a list of your rooms and users.
- ./test.php <your api token>
+ ./example.php <your api token>
+
+To test the library itself, run the PHPUnit tests:
+
+ phpunit tests/
Usage
-----
require 'HipChat.php';
+
$token = '<your api token>';
$hc = new HipChat($token);
- $rooms = $hc->get_rooms();
- foreach ($rooms->rooms as $room) {
+
+ foreach ($hc->get_rooms() as $room) {
echo " - $room->id = $room->name\n";
}
+
[hc]: http://www.hipchat.com
View
0  test.php → example.php
File renamed without changes
View
36 tests/HipChatPHPTest.php
@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * Some simple unit tests to help test this library. These tests are not
+ * intended to be used to verify the API response data.
+ *
+ * TODO: Test valid requests. What API token to use?
+ */
+
+require_once dirname(__FILE__).'/../HipChat.php';
+require_once 'PHPUnit/Framework.php';
+
+class HipChatPHPTest extends PHPUnit_Framework_TestCase {
+
+ private $target = 'http://api.hipchat.com';
+
+ public function testBadToken() {
+ $hc = new HipChat('hipchat-php-test-bad-token', $this->target);
+ $this->setExpectedException('HipChat_Exception');
+ $hc->get_rooms();
+ }
+
+ public function testBadTargetHost() {
+ $bad_target = 'http://does-not-exist.hipchat.com';
+ $hc = new HipChat('hipchat-php-test-token', $bad_target);
+ $this->setExpectedException('HipChat_Exception');
+ $hc->get_rooms();
+ }
+
+ public function testBadApiMethod() {
+ $hc = new HipChat('hipchat-php-test-token', $this->target);
+ $this->setExpectedException('HipChat_Exception');
+ $hc->make_request('bad/method');
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.