Skip to content

Commit

Permalink
Added some simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JayBizzle committed Aug 14, 2015
1 parent d27761e commit 675f51b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 13 deletions.
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="importer-tests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
32 changes: 19 additions & 13 deletions src/DeployBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@

class DeployBot
{
private $api_key;
private $api_endpoint = 'https://<account>.deploybot.com/api/v1/';
private $client;
private $query = [];
public $api_key;
public $api_endpoint = 'https://<account>.deploybot.com/api/v1/';
public $client;
public $query = [];

public function __construct($api_key, $account)
public function __construct($api_key, $account, $client = null)
{
$this->api_key = $api_key;
$this->api_endpoint = str_replace('<account>', $account, $this->api_endpoint);
$this->api_endpoint = $this->parseApiEndpoint($account);

$this->client = new Client([
$this->client = ($client) ?: new Client([
'base_url' => $this->api_endpoint,
'defaults' => [
//'proxy' => 'http://localhost:8888',
'headers' => [
'X-Api-Token' => $this->api_key,
'Accept' => 'application/json',
Expand All @@ -29,6 +30,11 @@ public function __construct($api_key, $account)
]);
}

public function parseApiEndpoint($account)
{
return str_replace('<account>', $account, $this->api_endpoint);
}

/**
* Dynamically add query parameters or call API endpoints.
*
Expand Down Expand Up @@ -66,7 +72,7 @@ public function triggerDeployment()
*
* @return $this
*/
private function addQuery($name, $args)
public function addQuery($name, $args)
{
$name = $this->snakeCase($name);

Expand All @@ -84,11 +90,11 @@ private function addQuery($name, $args)
*
* @return object
*/
private function buildRequest($resource, $args = [], $method = 'get')
public function buildRequest($resource, $args = [], $method = 'get')
{
$query = [];

if (count($args[0]) == 1 && is_int($args[0])) {
if (isset($args[0]) && count($args[0]) == 1 && is_int($args[0])) {
$resource = $resource.'/'.$args[0];
}

Expand All @@ -108,7 +114,7 @@ private function buildRequest($resource, $args = [], $method = 'get')
*
* @return object
*/
private function sendRequest($resource, $query = [], $method = 'get')
public function sendRequest($resource, $query = [], $method = 'get')
{
$option_name = ($method == 'get') ? 'query' : 'json';

Expand All @@ -121,7 +127,7 @@ private function sendRequest($resource, $query = [], $method = 'get')
// Reset query parameters
$this->query = [];

return json_decode($response->getBody()->getContents());
return json_decode($response->getBody());
}

/**
Expand All @@ -132,7 +138,7 @@ private function sendRequest($resource, $query = [], $method = 'get')
*
* @return string
*/
private function snakeCase($value, $delimiter = '_')
public function snakeCase($value, $delimiter = '_')
{
$key = $value.$delimiter;

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

namespace DeployBot\Test;

use Jaybizzle\DeployBot;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Message\Response;

class DeployBotTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
//
}

/**
* @dataProvider providerParamNames
*/
public function testCamelCaseMethodsReturnSnakeCaseParams($original, $expected)
{
$db = new DeployBot('foo_api_key', 'bar_account_name', new \stdClass());

$result = $db->snakeCase($original);

$this->assertEquals($expected, $result);
}

public function testParseApiEndpoint()
{
$expected = 'https://foobar.deploybot.com/api/v1/';

$db = $this->getMockBuilder('Jaybizzle\DeployBot')
->setConstructorArgs(array('foo_api_key', 'bar_account_name'))
->disableOriginalConstructor()
->setMethods(null)
->getMock();

$result = $db->parseApiEndpoint('foobar');

$this->assertEquals($expected, $result);
}

public function testGetUsersResponse()
{
$client = new Client();

$mock = new Mock();
$mock->addResponse(__DIR__.'/responses/getUsersResponse.txt');

$client->getEmitter()->attach($mock);

$db = new DeployBot('foo_api_key', 'bar_account_name', $client);

$result = $db->getUsers();

$this->assertEquals(3, count($result->entries));
}

public function testAddingQueryParams()
{
$args = array(2);

$db = new DeployBot('foo_api_key', 'bar_account_name', new \stdClass());

$db->addQuery('limit', $args);

$this->assertTrue(array_key_exists('limit', $db->query));
$this->assertEquals(2, $db->query['limit']);
}

public function providerParamNames()
{
return array(
array('fooBar', 'foo_bar'),
array('FooBar', 'foo_bar'),
array('FOOBAR', 'f_o_o_b_a_r'),
);
}

public function tearDown()
{

}
}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
error_reporting(E_ALL | E_STRICT);
require dirname(__DIR__) . '/vendor/autoload.php';
19 changes: 19 additions & 0 deletions tests/responses/getUsersResponse.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 680
Connection: keep-alive
Status: 200 OK
X-Content-Type-Options: nosniff
ETag: "8de608c3368602f1ab3b0a83804a7ced"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 93e6ea9c-fbe1-4eca-81b6-933223fbf751
X-Runtime: 0.028322
X-Powered-By: BeanServ 1.1
Date: Thu, 13 Aug 2015 16:10:30 CDT
Server: BeanServ 1.1
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=15768000
X-XSS-Protection: 1; mode=block

{"meta":{"next":null,"next_uri":null,"total":3},"entries":[{"id":3189,"first_name":"Andrew","last_name":"Schofield","timezone":"Edinburgh","email":"[email protected]","created_at":"2014/04/22 11:16:17 +0100","updated_at":"2014/08/12 15:37:39 +0100","is_admin":false},{"id":3184,"first_name":"Mark","last_name":"Beech","timezone":"Edinburgh","email":"[email protected]","created_at":"2014/04/22 09:42:12 +0100","updated_at":"2015/08/12 13:41:08 +0100","is_admin":true},{"id":13818,"first_name":"Max","last_name":"King","timezone":"Edinburgh","email":"[email protected]","created_at":"2015/01/14 09:13:30 +0000","updated_at":"2015/02/23 15:29:25 +0000","is_admin":false}]}

0 comments on commit 675f51b

Please sign in to comment.