Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 208d8dc

Browse files
LaravelFreelancerNLDeploy
and
Deploy
authored
42 add disconnect (#43)
* chore: added .phpunit.cache * test: improved test * test: removed only. * feat: added disconnect functionality * chore: simplified disconnect * test: added rawRequest test * docs: improved disconnect docs * test: added connect test * fix: connect returns true upon success * docs: added connect method --------- Co-authored-by: Deploy <deploy@wedevise.nl>
1 parent 4b8118c commit 208d8dc

File tree

6 files changed

+176
-10
lines changed

6 files changed

+176
-10
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.php_cs
55
.php_cs.cache
66
.phpunit.result.cache
7+
.phpunit.cache
78
.vscode
89
clover.xml
910
composer.lock

‎docs/arangodb-client.md‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,28 @@ Send a request to ArangoDB's HTTP REST API. This is mostly for internal use but
6666
$arangoClient->request(
6767
'get',
6868
'/_api/version',
69-
'query' => [
70-
'details' => $details
69+
[
70+
'query' => [
71+
'details' => $details
72+
]
73+
]
74+
]);
75+
```
76+
77+
### rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
78+
Returns the raw response of the request.
79+
*Note* that the request itself is made against the configured endpoint but the databasename is _not_ automatically
80+
prepended to the uri as opposed to a regular request.
81+
82+
83+
```
84+
$arangoClient->rawRequest(
85+
'get',
86+
'/_api/version',
87+
[
88+
'query' => [
89+
'details' => $details
90+
]
7191
]
7292
]);
7393
```
@@ -106,4 +126,27 @@ $arangoClient->schema()->createCollection('users');
106126
Pass chained method to the admin manager.
107127
```
108128
$arangoClient->admin()->getVersion();
129+
```
130+
131+
### connect(array $config = [], ?GuzzleClient $httpClient = null): void
132+
You can update the config by calling the connect method. This replaces the underlying connection
133+
and prepares the connection for any requests that follow.
134+
135+
```
136+
$config = [
137+
'host' => 'http://localhost',
138+
'port' => '8529',
139+
'username' => 'your-other-database-username',
140+
'password' => 'your-other-database-password',
141+
'database'=> 'your-other-database'
142+
];
143+
144+
$arangoClient->connect($config): void
145+
```
146+
147+
### disconnect(): bool
148+
Disconnect from the current keep-alive connection, if any.
149+
150+
```
151+
$arangoClient->disconnect();
109152
```

‎src/ArangoClient.php‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,41 @@ class ArangoClient
4343
* @throws UnknownProperties
4444
*/
4545
public function __construct(array $config = [], ?GuzzleClient $httpClient = null)
46+
{
47+
$this->connect($config, $httpClient);
48+
}
49+
50+
/**
51+
* ArangoClient constructor.
52+
*
53+
* @param array<string|numeric|null> $config
54+
* @param GuzzleClient|null $httpClient
55+
*
56+
* @throws UnknownProperties
57+
*/
58+
public function connect(array $config = [], ?GuzzleClient $httpClient = null): bool
4659
{
4760
$config['endpoint'] = $this->generateEndpoint($config);
4861
$this->config = new HttpClientConfig($config);
4962

5063
$this->httpClient = $httpClient ?? new GuzzleClient($this->config->mapGuzzleHttpClientConfig());
64+
65+
return true;
5166
}
5267

68+
/**
69+
* We disconnect by creating a new guzzle client. The old client will remove the current connection upon destruction.
70+
*
71+
* @return bool
72+
*/
73+
public function disconnect(): bool
74+
{
75+
$this->httpClient = new GuzzleClient($this->config->mapGuzzleHttpClientConfig());
76+
77+
return true;
78+
}
79+
80+
5381
/**
5482
* @param array<mixed> $config
5583
*/
@@ -58,10 +86,12 @@ public function generateEndpoint(array $config): string
5886
if (isset($config['endpoint'])) {
5987
return (string) $config['endpoint'];
6088
}
89+
6190
$endpoint = 'http://localhost:8529';
6291
if (isset($config['host'])) {
6392
$endpoint = (string) $config['host'];
6493
}
94+
6595
if (isset($config['port'])) {
6696
$endpoint .= ':' . (string) $config['port'];
6797
}
@@ -96,6 +126,28 @@ public function request(string $method, string $uri, array|HttpRequestOptions $o
96126
return new stdClass();
97127
}
98128

129+
/**
130+
* @param array<mixed>|HttpRequestOptions $options
131+
*
132+
* @throws ArangoException
133+
*/
134+
public function rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
135+
{
136+
if (is_array($options)) {
137+
$options = $this->prepareRequestOptions($options);
138+
}
139+
140+
$response = null;
141+
try {
142+
$response = $this->httpClient->request($method, $uri, $options->all());
143+
} catch (Throwable $e) {
144+
$this->handleGuzzleException($e);
145+
}
146+
147+
return $response;
148+
}
149+
150+
99151
/**
100152
* @param array<mixed> $options
101153
*

‎tests/ArangoClientTest.php‎

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
use ArangoClient\Admin\AdminManager;
66
use ArangoClient\ArangoClient;
7+
use ArangoClient\Http\HttpClientConfig;
78
use ArangoClient\Schema\SchemaManager;
89
use ArangoClient\Statement\Statement;
910
use GuzzleHttp\Client;
11+
use GuzzleHttp\Client as GuzzleClient;
1012
use GuzzleHttp\Handler\MockHandler;
1113
use GuzzleHttp\HandlerStack;
1214
use GuzzleHttp\Middleware;
1315
use GuzzleHttp\Psr7\Response;
1416

17+
use function PHPUnit\Framework\assertTrue;
18+
1519
uses(Tests\TestCase::class);
1620

1721
test('get config', function () {
@@ -46,21 +50,21 @@
4650
test('client with host port config', function () {
4751
$config = [
4852
'host' => 'http://127.0.0.1',
49-
'port' => '1234',
53+
'port' => '8529',
5054
'username' => 'root',
5155
];
5256
$client = new ArangoClient($config);
5357
$retrievedConfig = $client->getConfig();
5458

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

5862
test('config with alien properties', function () {
5963
$config = [
6064
'name' => 'arangodb',
6165
'driver' => 'arangodb',
6266
'host' => 'http://127.0.0.1',
63-
'port' => '1234',
67+
'port' => '8529',
6468
'username' => 'root',
6569
];
6670
$client = new ArangoClient($config);
@@ -73,8 +77,26 @@
7377
test('set and get http client', function () {
7478
$oldClient = $this->arangoClient->getHttpClient();
7579

76-
$newClient = Mockery::mock(Client::class);
80+
$defaultConfig = [
81+
'endpoint' => 'http://localhost:8529',
82+
'host' => null,
83+
'port' => null,
84+
'version' => 1.1,
85+
'connection' => 'Keep-Alive',
86+
'allow_redirects' => false,
87+
'connect_timeout' => 0.0,
88+
'username' => 'root',
89+
'password' => null,
90+
'database' => $this->testDatabaseName,
91+
'jsonStreamDecoderThreshold' => 1048576,
92+
];
93+
94+
$config = new HttpClientConfig($defaultConfig);
95+
96+
$newClient = new GuzzleClient($config->mapGuzzleHttpClientConfig());
97+
7798
$this->arangoClient->setHttpClient($newClient);
99+
78100
$retrievedClient = $this->arangoClient->getHttpClient();
79101

80102
expect($oldClient)->toBeInstanceOf(Client::class);
@@ -89,6 +111,14 @@
89111
expect($result->version)->toBeString();
90112
});
91113

114+
115+
test('rawRequest', function () {
116+
$response = $this->arangoClient->rawRequest('get', '/_api/version', []);
117+
118+
expect($response->getStatusCode())->toBe(200);
119+
expect($response->getHeader('Connection')[0])->toBe('Keep-Alive');
120+
});
121+
92122
test('get user', function () {
93123
$user = $this->arangoClient->getUser();
94124
expect($user)->toBe('root');
@@ -103,10 +133,13 @@
103133

104134
$database = $this->arangoClient->getDatabase();
105135
expect($database)->toBe($newDatabaseName);
136+
137+
// Reset DB name
138+
$this->arangoClient->setDatabase($this->testDatabaseName);
106139
});
107140

108141
test('database name is used in requests', function () {
109-
$database = 'some_database';
142+
$database = 'arangodb_php_client__test';
110143
if (!$this->arangoClient->schema()->hasDatabase($database)) {
111144
$this->arangoClient->schema()->createDatabase($database);
112145
}
@@ -234,3 +267,37 @@
234267

235268
$this->schemaManager->deleteCollection($collection);
236269
});
270+
271+
272+
test('connect', function () {
273+
$oldHttpClient = $this->arangoClient->getHttpClient();
274+
$oldHttpClientObjectId = spl_object_id($oldHttpClient);
275+
276+
$newConfig = [
277+
'endpoint' => 'http://localhost:8529',
278+
'version' => 2,
279+
'connection' => 'Close',
280+
'username' => 'root',
281+
'password' => null,
282+
'database' => $this->testDatabaseName,
283+
'jsonStreamDecoderThreshold' => 1048576,
284+
];
285+
286+
$response = $this->arangoClient->connect($newConfig);
287+
288+
$newHttpClient = $this->arangoClient->getHttpClient();
289+
$newHttpClientObjectId = spl_object_id($newHttpClient);
290+
291+
expect($oldHttpClientObjectId)->not()->toBe($newHttpClientObjectId);
292+
expect($response)->toBeTrue();
293+
294+
$this->arangoClient->setHttpClient($oldHttpClient);
295+
});
296+
297+
298+
299+
test('disconnect', function () {
300+
$disconnected = $this->arangoClient->disconnect();
301+
302+
assertTrue($disconnected);
303+
});

‎tests/ExceptionsTest.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use ArangoClient\Exceptions\ArangoException;
6+
57
uses(Tests\TestCase::class);
68

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

1921
test('calls to none existing db throw', function () {
2022
$this->arangoClient->setDatabase('NoneExistingDb');
21-
$this->expectExceptionCode(404);
2223
$this->schemaManager->hasCollection('dummy');
23-
});
24+
25+
$this->arangoClient->setDatabase($this->testDatabaseName);
26+
})->throws(ArangoException::class);

‎tests/SchemaManagerGraphsTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,4 @@
345345
expect($result)->toBeTrue();
346346
expect(count($createdGraphs))->toBe(2);
347347
expect(count($finalGraphs))->toBe(0);
348-
})->only();
348+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /