Skip to content

Commit 3bba95e

Browse files
committed
Add TestingBot Storage calls
1 parent 976ff3b commit 3bba95e

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

LICENSE.APACHE2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Apache License 2.0
22

3-
Copyright 2016 TestingBot
3+
Copyright TestingBot
44

55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,43 @@ Gets a list of active tunnels for your account.
115115
$api->getTunnels();
116116
```
117117

118+
### uploadLocalFileToStorage
119+
Uploads a local file (.apk, .ipa, .zip) to TestingBot Storage.
120+
121+
```php
122+
$api->uploadLocalFileToStorage($pathToLocalFile);
123+
```
124+
125+
### uploadRemoteFileToStorage
126+
Uploads a remote file (.apk, .ipa, .zip) to TestingBot Storage.
127+
128+
```php
129+
$api->uploadRemoteFileToStorage($urlToRemoteFile);
130+
```
131+
132+
### getStorageFile
133+
Gets meta data from a file previously uploaded to TestingBot Storage.
134+
AppUrl is the `tb://` url you previously received from the TestingBot API.
135+
136+
```php
137+
$api->getStorageFile($appUrl);
138+
```
139+
140+
### getStorageFiles
141+
Gets meta data from all file previously uploaded to TestingBot Storage.
142+
143+
```php
144+
$api->getStorageFiles();
145+
```
146+
147+
### deleteStorageFile
148+
Deletes a file previously uploaded to TestingBot Storage.
149+
AppUrl is the `tb://` url you previously received from the TestingBot API.
150+
151+
```php
152+
$api->deleteStorageFile($appUrl);
153+
```
154+
118155
### getAuthenticationHash
119156
Calculates the hash necessary to share tests with other people
120157

src/TestingBot/TestingBotAPI.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,58 @@ public function getAuthenticationHash($identifier = null) {
146146
return md5($this->_key . ":" . $this->_secret);
147147
}
148148

149+
public function uploadLocalFileToStorage($localFile)
150+
{
151+
if (!file_exists($localFile))
152+
{
153+
throw new \Exception("Could not find file to upload: " . $localFile);
154+
}
155+
$mime = mime_content_type($localFile);
156+
$fileInfo = pathinfo($localFile);
157+
$fileName = $fileInfo['basename'];
158+
$cfile = curl_file_create($localFile, $mime, $fileName);
159+
$curl = curl_init();
160+
curl_setopt($curl, CURLOPT_URL, "https://api.testingbot.com/v1/storage");
161+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
162+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
163+
curl_setopt($curl, CURLOPT_USERPWD, $this->_key . ":" . $this->_secret);
164+
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
165+
'file' => $cfile
166+
));
167+
168+
$response = curl_exec($curl);
169+
curl_close($curl);
170+
171+
return $this->_parseResult($response);
172+
}
173+
174+
public function uploadRemoteFileToStorage($remoteFileUrl)
175+
{
176+
return $this->_doRequest("storage", "POST", array(
177+
'url' => $remoteFileUrl
178+
));
179+
}
180+
181+
public function getStorageFile($appUrl)
182+
{
183+
return $this->_doRequest("storage/" . str_replace("tb://", "", $appUrl), "GET");
184+
}
185+
186+
public function getStorageFiles($offset = 0, $count = 10)
187+
{
188+
$queryData = array(
189+
'offset' => $offset,
190+
'count' => $count
191+
);
192+
193+
return $this->_doRequest("storage/?" . http_build_query($queryData), "GET");
194+
}
195+
196+
public function deleteStorageFile($appUrl)
197+
{
198+
return $this->_doRequest("storage/" . str_replace("tb://", "", $appUrl), "DELETE");
199+
}
200+
149201
private function _doRequest($path, $method = 'POST', array $data = array())
150202
{
151203
$curl = curl_init();

tests/TestingBotTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,37 @@ public function testGetBrowsers() {
6969
public function testCalculateAuthentication() {
7070
$this->assertEquals($this->api->getAuthenticationHash("test"), "344ebf07233168c4882adf953a8a8c9b");
7171
}
72+
73+
public function testUploadLocalFileToStorage() {
74+
$response = $this->api->uploadLocalFileToStorage(realpath(dirname(__FILE__) . '/resources/test.apk'));
75+
$this->assertContains('tb://', $response['app_url']);
76+
}
77+
78+
public function testUploadRemoteFileToStorage() {
79+
$response = $this->api->uploadRemoteFileToStorage('https://testingbot.com/appium/sample.apk');
80+
$this->assertContains('tb://', $response['app_url']);
81+
}
82+
83+
public function testGetStoredFiles() {
84+
$response = $this->api->getStorageFiles();
85+
$currentTotal = $response['meta']['total'];
86+
$this->api->uploadLocalFileToStorage(realpath(dirname(__FILE__) . '/resources/test.apk'));
87+
$response = $this->api->getStorageFiles();
88+
$this->assertEquals($response['meta']['total'], $currentTotal + 1);
89+
}
90+
91+
public function testStoredFileMetaData() {
92+
$response = $this->api->uploadLocalFileToStorage(realpath(dirname(__FILE__) . '/resources/test.apk'));
93+
$meta = $this->api->getStorageFile($response['app_url']);
94+
$this->assertEquals($meta['app_url'], $response['app_url']);
95+
}
96+
97+
public function testDeleteStorageFile() {
98+
$response = $this->api->uploadLocalFileToStorage(realpath(dirname(__FILE__) . '/resources/test.apk'));
99+
$meta = $this->api->getStorageFile($response['app_url']);
100+
$this->assertEquals($meta['app_url'], $response['app_url']);
101+
$this->api->deleteStorageFile($response['app_url']);
102+
$meta = $this->api->getStorageFile($response['app_url']);
103+
$this->assertArrayHasKey('error', $meta);
104+
}
72105
}

tests/resources/test.apk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

0 commit comments

Comments
 (0)