Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delabiejochen committed Oct 5, 2017
1 parent 8edb066 commit 53592fc
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: php
php:
- '5.6'
- '7.1'
notifications:
email: false
install:
- composer self-update
- composer update --prefer-dist
script:
- phpunit tests/
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/testingbot/testingbot-php.svg?branch=master)](https://travis-ci.org/testingbot/testingbot-php)

Testingbot-PHP
=======

Expand Down Expand Up @@ -34,6 +36,14 @@ To start, create a new `TestingBot\TestingBotAPI` object and pass in the key and

Now you can use the various methods we've made available to interact with the API:

### getBrowsers
Gets a list of browsers you can test on

```php
$api->getBrowsers();
```


### getUserInfo
Gets your user information

Expand Down Expand Up @@ -103,4 +113,11 @@ Gets a list of active tunnels for your account.

```php
$api->getTunnels();
```

### getAuthenticationHash
Calculates the hash necessary to share tests with other people

```php
$api->getAuthenticationHash($identifier);
```
21 changes: 15 additions & 6 deletions src/TestingBot/TestingBotAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ public function getBuild($buildID)

public function getTunnels()
{
return $this->_doRequest("tunnels/", "GET");
return $this->_doRequest("tunnel/list", "GET");
}

public function getUserInfo()
{
return $this->_doRequest("user", "GET");
}

public function getBrowsers()
{
return $this->_doRequest("browsers", "GET");
}

public function updateUserInfo(array $details)
{
$data = array();
Expand All @@ -132,6 +137,15 @@ public function updateUserInfo(array $details)
return $this->_doRequest("user", "PUT", $data);
}

public function getAuthenticationHash($identifier = null) {
if (!is_null($identifier))
{
return md5($this->_key . ":" . $this->_secret . ":" . $identifier);
}

return md5($this->_key . ":" . $this->_secret);
}

private function _doRequest($path, $method = 'POST', array $data = array())
{
$curl = curl_init();
Expand All @@ -153,11 +167,6 @@ private function _doRequest($path, $method = 'POST', array $data = array())
private function _parseResult($response)
{
$result = json_decode($response, true);
if (empty($result))
{
throw new \Exception("An API error occurred: " . print_r($response, true));
}

return $result;
}
}
66 changes: 66 additions & 0 deletions tests/TestingBotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

require_once('src/TestingBot/TestingBotAPI.php');

class TestingBotTest extends PHPUnit_Framework_TestCase {
private $api;

public function setup() {
$key = getenv("TB_KEY");
$secret = getenv("TB_SECRET");

$this->api = new TestingBot\TestingBotAPI($key, $secret);
}

public function testGetSingleTest() {
$sessionID = "6344353dcee24694bf39d5ee5e6e5b11";
$test = $this->api->getJob($sessionID);
$this->assertEquals($test['session_id'], $sessionID);
}

public function testGetUnknownTest() {
$sessionID = "unknown";
$test = $this->api->getJob($sessionID);
$this->assertArrayHasKey('error', $test);
}

public function testDeleteUnknownTest() {
$sessionID = "unknown";
$test = $this->api->deleteJob($sessionID);
$this->assertArrayHasKey('error', $test);
}

public function testGetTests() {
$tests = $this->api->getJobs(0, 10);
$this->assertCount(10, $tests['data']);
}

public function testGetUser() {
$user = $this->api->getUserInfo();
$this->assertEquals("bot", $user['last_name']);
}

public function testUnauthorizedCall() {
try {
$api = new TestingBot\TestingBotAPI('', '');
$user = $api->getUserInfo();
$this->assertEquals(true, false);
} catch (Exception $e) {
$this->assertEquals(true, true);
}
}

public function testGetTunnels() {
$tunnels = $this->api->getTunnels();
$this->assertCount(0, $tunnels);
}

public function testGetBrowsers() {
$browsers = $this->api->getBrowsers();
$this->assertEquals(sizeof($browsers) > 0, true);
}

public function testCalculateAuthentication() {
$this->assertEquals($this->api->getAuthenticationHash("test"), "344ebf07233168c4882adf953a8a8c9b");
}
}
9 changes: 0 additions & 9 deletions tests/test.php

This file was deleted.

0 comments on commit 53592fc

Please sign in to comment.