Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache
.vscode
clover.xml
composer.lock
Expand Down
47 changes: 45 additions & 2 deletions docs/arangodb-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,28 @@ Send a request to ArangoDB's HTTP REST API. This is mostly for internal use but
$arangoClient->request(
'get',
'/_api/version',
'query' => [
'details' => $details
[
'query' => [
'details' => $details
]
]
]);
```

### rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
Returns the raw response of the request.
*Note* that the request itself is made against the configured endpoint but the databasename is _not_ automatically
prepended to the uri as opposed to a regular request.


```
$arangoClient->rawRequest(
'get',
'/_api/version',
[
'query' => [
'details' => $details
]
]
]);
```
Expand Down Expand Up @@ -106,4 +126,27 @@ $arangoClient->schema()->createCollection('users');
Pass chained method to the admin manager.
```
$arangoClient->admin()->getVersion();
```

### connect(array $config = [], ?GuzzleClient $httpClient = null): void
You can update the config by calling the connect method. This replaces the underlying connection
and prepares the connection for any requests that follow.

```
$config = [
'host' => 'http://localhost',
'port' => '8529',
'username' => 'your-other-database-username',
'password' => 'your-other-database-password',
'database'=> 'your-other-database'
];

$arangoClient->connect($config): void
```

### disconnect(): bool
Disconnect from the current keep-alive connection, if any.

```
$arangoClient->disconnect();
```
52 changes: 52 additions & 0 deletions src/ArangoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,41 @@ class ArangoClient
* @throws UnknownProperties
*/
public function __construct(array $config = [], ?GuzzleClient $httpClient = null)
{
$this->connect($config, $httpClient);
}

/**
* ArangoClient constructor.
*
* @param array<string|numeric|null> $config
* @param GuzzleClient|null $httpClient
*
* @throws UnknownProperties
*/
public function connect(array $config = [], ?GuzzleClient $httpClient = null): bool
{
$config['endpoint'] = $this->generateEndpoint($config);
$this->config = new HttpClientConfig($config);

$this->httpClient = $httpClient ?? new GuzzleClient($this->config->mapGuzzleHttpClientConfig());

return true;
}

/**
* We disconnect by creating a new guzzle client. The old client will remove the current connection upon destruction.
*
* @return bool
*/
public function disconnect(): bool
{
$this->httpClient = new GuzzleClient($this->config->mapGuzzleHttpClientConfig());

return true;
}


/**
* @param array<mixed> $config
*/
Expand All @@ -58,10 +86,12 @@ public function generateEndpoint(array $config): string
if (isset($config['endpoint'])) {
return (string) $config['endpoint'];
}

$endpoint = 'http://localhost:8529';
if (isset($config['host'])) {
$endpoint = (string) $config['host'];
}

if (isset($config['port'])) {
$endpoint .= ':' . (string) $config['port'];
}
Expand Down Expand Up @@ -96,6 +126,28 @@ public function request(string $method, string $uri, array|HttpRequestOptions $o
return new stdClass();
}

/**
* @param array<mixed>|HttpRequestOptions $options
*
* @throws ArangoException
*/
public function rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
{
if (is_array($options)) {
$options = $this->prepareRequestOptions($options);
}

$response = null;
try {
$response = $this->httpClient->request($method, $uri, $options->all());
} catch (Throwable $e) {
$this->handleGuzzleException($e);
}

return $response;
}


/**
* @param array<mixed> $options
*
Expand Down
77 changes: 72 additions & 5 deletions tests/ArangoClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use ArangoClient\Admin\AdminManager;
use ArangoClient\ArangoClient;
use ArangoClient\Http\HttpClientConfig;
use ArangoClient\Schema\SchemaManager;
use ArangoClient\Statement\Statement;
use GuzzleHttp\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;

use function PHPUnit\Framework\assertTrue;

uses(Tests\TestCase::class);

test('get config', function () {
Expand Down Expand Up @@ -46,21 +50,21 @@
test('client with host port config', function () {
$config = [
'host' => 'http://127.0.0.1',
'port' => '1234',
'port' => '8529',
'username' => 'root',
];
$client = new ArangoClient($config);
$retrievedConfig = $client->getConfig();

expect($retrievedConfig['endpoint'])->toEqual('http://127.0.0.1:1234');
expect($retrievedConfig['endpoint'])->toEqual('http://127.0.0.1:8529');
});

test('config with alien properties', function () {
$config = [
'name' => 'arangodb',
'driver' => 'arangodb',
'host' => 'http://127.0.0.1',
'port' => '1234',
'port' => '8529',
'username' => 'root',
];
$client = new ArangoClient($config);
Expand All @@ -73,8 +77,26 @@
test('set and get http client', function () {
$oldClient = $this->arangoClient->getHttpClient();

$newClient = Mockery::mock(Client::class);
$defaultConfig = [
'endpoint' => 'http://localhost:8529',
'host' => null,
'port' => null,
'version' => 1.1,
'connection' => 'Keep-Alive',
'allow_redirects' => false,
'connect_timeout' => 0.0,
'username' => 'root',
'password' => null,
'database' => $this->testDatabaseName,
'jsonStreamDecoderThreshold' => 1048576,
];

$config = new HttpClientConfig($defaultConfig);

$newClient = new GuzzleClient($config->mapGuzzleHttpClientConfig());

$this->arangoClient->setHttpClient($newClient);

$retrievedClient = $this->arangoClient->getHttpClient();

expect($oldClient)->toBeInstanceOf(Client::class);
Expand All @@ -89,6 +111,14 @@
expect($result->version)->toBeString();
});


test('rawRequest', function () {
$response = $this->arangoClient->rawRequest('get', '/_api/version', []);

expect($response->getStatusCode())->toBe(200);
expect($response->getHeader('Connection')[0])->toBe('Keep-Alive');
});

test('get user', function () {
$user = $this->arangoClient->getUser();
expect($user)->toBe('root');
Expand All @@ -103,10 +133,13 @@

$database = $this->arangoClient->getDatabase();
expect($database)->toBe($newDatabaseName);

// Reset DB name
$this->arangoClient->setDatabase($this->testDatabaseName);
});

test('database name is used in requests', function () {
$database = 'some_database';
$database = 'arangodb_php_client__test';
if (!$this->arangoClient->schema()->hasDatabase($database)) {
$this->arangoClient->schema()->createDatabase($database);
}
Expand Down Expand Up @@ -234,3 +267,37 @@

$this->schemaManager->deleteCollection($collection);
});


test('connect', function () {
$oldHttpClient = $this->arangoClient->getHttpClient();
$oldHttpClientObjectId = spl_object_id($oldHttpClient);

$newConfig = [
'endpoint' => 'http://localhost:8529',
'version' => 2,
'connection' => 'Close',
'username' => 'root',
'password' => null,
'database' => $this->testDatabaseName,
'jsonStreamDecoderThreshold' => 1048576,
];

$response = $this->arangoClient->connect($newConfig);

$newHttpClient = $this->arangoClient->getHttpClient();
$newHttpClientObjectId = spl_object_id($newHttpClient);

expect($oldHttpClientObjectId)->not()->toBe($newHttpClientObjectId);
expect($response)->toBeTrue();

$this->arangoClient->setHttpClient($oldHttpClient);
});



test('disconnect', function () {
$disconnected = $this->arangoClient->disconnect();

assertTrue($disconnected);
});
7 changes: 5 additions & 2 deletions tests/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use ArangoClient\Exceptions\ArangoException;

uses(Tests\TestCase::class);

test('test409 conflict exception', function () {
Expand All @@ -18,6 +20,7 @@

test('calls to none existing db throw', function () {
$this->arangoClient->setDatabase('NoneExistingDb');
$this->expectExceptionCode(404);
$this->schemaManager->hasCollection('dummy');
});

$this->arangoClient->setDatabase($this->testDatabaseName);
})->throws(ArangoException::class);
2 changes: 1 addition & 1 deletion tests/SchemaManagerGraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,4 @@
expect($result)->toBeTrue();
expect(count($createdGraphs))->toBe(2);
expect(count($finalGraphs))->toBe(0);
})->only();
});
Loading