Skip to content

Commit d50d945

Browse files
committed
manually apply changes from @tomterl
1 parent 4a13211 commit d50d945

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

lib/ArangoDBClient/FoxxHandler.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: foxx upload
5+
*
6+
* @package ArangoDBClient
7+
* @author Tom Regner <thomas.regner@fb-research.de>
8+
* @copyright Copyright 2016, triagens GmbH, Cologne, Germany
9+
*/
10+
11+
namespace ArangoDBClient;
12+
13+
/**
14+
* A class for uploading Foxx application zips to a database
15+
*
16+
* @package ArangoDBClient
17+
* @since 3.1
18+
*/
19+
class FoxxHandler extends Handler
20+
{
21+
/**
22+
* Upload and install a foxx app.
23+
*
24+
* @throws ClientException
25+
*
26+
* @param string $localZip - the path to the local foxx-app zip-archive to upload/install
27+
* @param string mountPoint - the mountpoint for the app, must begin with a '/'
28+
* @param array $options - for future usage
29+
* @return array - the server response
30+
*
31+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
32+
*/
33+
public function installFoxxZip($localZip, $mountPoint, $options = array())
34+
{
35+
if (!file_exists($localZip)) {
36+
throw new ClientException("Foxx-Zip {$localZip} does not exist (or file is unreadable).");
37+
}
38+
39+
try {
40+
$post = file_get_contents($localZip);
41+
$response = $this->getConnection()->post(Urls::URL_UPLOAD, $post);
42+
43+
if ($response->getHttpCode() < 400) {
44+
$response = $this->getConnection()->put(Urls::URL_FOXX_INSTALL, json_encode(array('appInfo' => $response->getJson()['filename'], 'mount' => $mountPoint)));
45+
if ($response->getHttpCode() < 400) {
46+
return $response->getJson();
47+
} else {
48+
throw new ClientException('Foxx-Zip install failed');
49+
}
50+
} else {
51+
throw new ClientException('Foxx-Zip upload failed');
52+
}
53+
} catch (ServerException $e) {
54+
throw new ClientException($e->getMessage());
55+
}
56+
}
57+
58+
/**
59+
* Remove a foxx-app.
60+
*
61+
* @throws ClientException
62+
*
63+
* @param string mountPoint - the mountpoint for the app, must begin with a '/'
64+
* @param array $options - for future usage
65+
* @return array - the server response
66+
*
67+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
68+
*/
69+
public function removeFoxxApp($mountPoint, $options = array())
70+
{
71+
try {
72+
$response = $this->getConnection()->put(Urls::URL_FOXX_UNINSTALL, json_encode(array('mount' => $mountPoint)));
73+
if ($response->getHttpCode() < 400) {
74+
return $response->getJson();
75+
} else {
76+
throw new ClientException(sprintf('Foxx uninstall failed (Code: %d)', $response->getHttpCode()));
77+
}
78+
} catch(ServerException $e) {
79+
if ($e->getMessage() === 'Service not found') {
80+
throw new ClientException(sprintf('Mount point %s not present.', $mountPoint));
81+
}
82+
throw new ClientException($e->getMessage());
83+
}
84+
}
85+
}
86+
87+
class_alias(FoxxHandler::class, '\triagens\ArangoDb\FoxxHandler');

lib/ArangoDBClient/Urls.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,20 @@ abstract class Urls
228228
*/
229229
const URL_QUERY_CACHE = '/_api/query-cache';
230230

231+
/**
232+
* URL for file uploads
233+
*/
234+
const URL_UPLOAD = '/_api/upload';
235+
236+
/**
237+
* URL for foxx-app installations
238+
*/
239+
const URL_FOXX_INSTALL = '/_admin/foxx/install';
240+
241+
/**
242+
* URL for foxx-app deinstallation
243+
*/
244+
const URL_FOXX_UNINSTALL = '/_admin/foxx/uninstall';
231245
}
232246

233247
class_alias(Urls::class, '\triagens\ArangoDb\Urls');

tests/FoxxBasicTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client testsuite
4+
* File: FoxxBasicTest.php
5+
*
6+
* @package ArangoDBClient
7+
* @author Tom Regner <thomas.regner@fb-research.de>
8+
*/
9+
10+
namespace ArangoDBClient;
11+
12+
13+
/**
14+
* Class FoxxBasicTest
15+
*
16+
* @property Connection $connection
17+
* @property FoxxHandler $foxxHandler
18+
*
19+
* @package ArangoDBClient
20+
*/
21+
class FoxxBasicTest extends
22+
\PHPUnit_Framework_TestCase
23+
{
24+
public function setUp()
25+
{
26+
$this->connection = getConnection();
27+
$this->foxxHandler = new FoxxHandler($this->connection);
28+
}
29+
30+
31+
/**
32+
* Try to upload and install a demo app
33+
*/
34+
public function testUploadAndInstallFoxxApp()
35+
{
36+
$foxxHandler = $this->foxxHandler;
37+
$zip = __DIR__ . '/files_for_tests/demo-hello-foxx-master.zip';
38+
$response = $foxxHandler->installFoxxZip($zip, '/hello_world');
39+
static::assertEquals(200, (int)$response['code'], 'Status not 200');
40+
static::assertEquals('/hello_world', $response['mount'], 'Wrong mountpoint');
41+
}
42+
43+
/**
44+
* Try to upload and install a non-existing app
45+
* @expectedException \ArangoDBClient\ClientException
46+
*/
47+
public function testUploadAndInstallNonExistingFoxxApp()
48+
{
49+
$foxxHandler = $this->foxxHandler;
50+
$zip = __DIR__ . '/files_for_tests/move_along.zip';
51+
$foxxHandler->installFoxxZip($zip, '/move_along');
52+
}
53+
54+
55+
public function tearDown()
56+
{
57+
$foxxHandler = $this->foxxHandler;
58+
try{
59+
// ignore errors
60+
$foxxHandler->removeFoxxApp('/hello_world');
61+
} catch(ClientException $e){
62+
// ignore
63+
}
64+
unset($this->foxxHandler, $this->connection);
65+
}
66+
}
Binary file not shown.

0 commit comments

Comments
 (0)