Skip to content

Commit 394a989

Browse files
committed
first commit
0 parents  commit 394a989

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.phar
2+
vendor/
3+
composer.lock

LICENSE.APACHE2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Apache License 2.0
2+
3+
Copyright 2016 TestingBot
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Testingbot-PHP
2+
=======
3+
4+
This is the TestingBot PHP client which makes it easy to
5+
interact with the [TestingBot API](https://testingbot.com/support/api)
6+
7+
License
8+
-------
9+
Testingbot-PHP is available under the Apache 2 license. See `LICENSE.APACHE2` for more
10+
details.
11+
12+
Usage
13+
----------
14+
15+
TestingBot-PHP is distributed with Composer, which means you can include it in your project by adding `testingbot-php` to your composer.json
16+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "testingbot/testingbot-php",
3+
"type": "testingbot-php",
4+
"description": "PHP version of the TestingBot API",
5+
"keywords": ["testingbot", "api", "testing", "phpunit", "selenium", "appium"],
6+
"homepage": "http://github.com/testingbot/testingbot-php",
7+
"license": "Apache-2.0",
8+
"authors": [{
9+
"name": "Jochen Delabie",
10+
"email": "info@testingbot.com",
11+
"homepage": "https://testingbot.com"
12+
}],
13+
"repositories": [
14+
{
15+
"type": "vcs",
16+
"url": "https://github.com/testingbot/testingbot-php"
17+
}
18+
],
19+
"require-dev": {
20+
"phpunit/phpunit": ">=4.5.1"
21+
},
22+
"require": {
23+
"php": ">=5.4.0",
24+
"phpunit/phpunit-selenium": ">=1.4.1",
25+
"brianium/paratest": ">=0.12.1",
26+
"appium/php-client": ">=0.1.0"
27+
}
28+
"autoload": {
29+
"psr-0": {"TestingBot": "src/"}
30+
},
31+
"support": {
32+
"email": "info@testingbot.com"
33+
}
34+
}

src/TestingBot/TestingBotAPI.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
namespace TestingBot;
4+
5+
class TestingBotAPI
6+
{
7+
private $_key;
8+
private $_secret;
9+
private $_options = array();
10+
11+
public function __construct($key, $secret, array $options = array())
12+
{
13+
$this->_key = $key;
14+
$this->_secret = $secret;
15+
16+
if (empty($key))
17+
{
18+
throw new \Exception("Key is required to use the TestingBot API");
19+
}
20+
21+
if (empty($secret))
22+
{
23+
throw new \Exception("Secret is required to use the TestingBot API");
24+
}
25+
26+
$this->_options = $options;
27+
}
28+
29+
public function updateJob($sessionID, array $details)
30+
{
31+
if (empty($sessionID))
32+
{
33+
throw new \Exception("Please use a valid WebDriver sessionID");
34+
}
35+
36+
$data = array();
37+
38+
foreach ($details as $k => $v)
39+
{
40+
$data['test[' . $k . ']'] = $v;
41+
}
42+
43+
return $this->_doRequest("tests/" . $sessionID, "PUT", $data);
44+
}
45+
46+
public function getJob($sessionID, array $details = array())
47+
{
48+
if (empty($sessionID))
49+
{
50+
throw new \Exception("Please use a valid WebDriver sessionID");
51+
}
52+
53+
$extra = "";
54+
if (!empty($details))
55+
{
56+
$extra = "?" . http_build_query($details);
57+
}
58+
return $this->_doRequest("tests/" . $sessionID . $extra, "GET");
59+
}
60+
61+
public function getJobs($offset = 0, $count = 10, array $extraOptions = array())
62+
{
63+
$queryData = array(
64+
'offset' => $offset,
65+
'count' => $count
66+
);
67+
68+
$queryData = array_merge($queryData, $extraOptions);
69+
70+
return $this->_doRequest("tests/?" . http_build_query($queryData), "GET");
71+
}
72+
73+
public function deleteJob($sessionID)
74+
{
75+
if (empty($sessionID))
76+
{
77+
throw new \Exception("Please use a valid WebDriver sessionID");
78+
}
79+
80+
return $this->_doRequest("tests/" . $sessionID, "DELETE");
81+
}
82+
83+
public function stopJob($sessionID)
84+
{
85+
if (empty($sessionID))
86+
{
87+
throw new \Exception("Please use a valid WebDriver sessionID");
88+
}
89+
90+
return $this->_doRequest("tests/" . $sessionID . "/stop", "PUT");
91+
}
92+
93+
public function getBuilds($offset = 0, $count = 10)
94+
{
95+
$queryData = array(
96+
'offset' => $offset,
97+
'count' => $count
98+
);
99+
100+
return $this->_doRequest("builds/?" . http_build_query($queryData), "GET");
101+
}
102+
103+
public function getBuild($buildID)
104+
{
105+
if (empty($buildID))
106+
{
107+
throw new \Exception("Please use a valid buildID");
108+
}
109+
110+
return $this->_doRequest("builds/" . $buildID, "GET");
111+
}
112+
113+
public function getTunnels()
114+
{
115+
return $this->_doRequest("tunnels/", "GET");
116+
}
117+
118+
public function getUserInfo()
119+
{
120+
return $this->_doRequest("user", "GET");
121+
}
122+
123+
public function updateUserInfo(array $details)
124+
{
125+
$data = array();
126+
127+
foreach ($details as $k => $v)
128+
{
129+
$data['user[' . $k . ']'] = $v;
130+
}
131+
132+
return $this->_doRequest("user", "PUT", $data);
133+
}
134+
135+
private function _doRequest($path, $method = 'POST', array $data = array())
136+
{
137+
$curl = curl_init();
138+
curl_setopt($curl, CURLOPT_URL, "https://api.testingbot.com/v1/" . $path);
139+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
140+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
141+
curl_setopt($curl, CURLOPT_USERPWD, $this->_key . ":" . $this->_secret);
142+
if (!empty($data))
143+
{
144+
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
145+
}
146+
147+
$response = curl_exec($curl);
148+
curl_close($curl);
149+
150+
return $this->_parseResult($response);
151+
}
152+
153+
private function _parseResult($response)
154+
{
155+
$result = json_decode($response, true);
156+
if (empty($result))
157+
{
158+
throw new \Exception("An API error occurred: " . print_r($response, true));
159+
}
160+
161+
return $result;
162+
}
163+
}

0 commit comments

Comments
 (0)