Skip to content

Commit c4755de

Browse files
committed
Added missing methods:
CollectionHandler -> checksum and revision Database -> listUserDatabases DocumentHandler -> head and added revision parameter to getById
1 parent 8e41927 commit c4755de

File tree

9 files changed

+308
-4
lines changed

9 files changed

+308
-4
lines changed

.idea/php.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ class CollectionHandler extends
169169
*/
170170
const OPTION_COUNT = 'count';
171171

172+
/**
173+
* checksum option
174+
*/
175+
const OPTION_CHECKSUM = 'checksum';
176+
177+
/**
178+
* revision option
179+
*/
180+
const OPTION_REVISION = 'revision';
181+
172182
/**
173183
* properties option
174184
*/
@@ -424,6 +434,54 @@ public function create($collection, $options = array())
424434
return $id;
425435
}
426436

437+
/**
438+
* Calculate a checksum of the collection.
439+
*
440+
* Will calculate a checksum of the meta-data (keys and optionally revision ids)
441+
* and optionally the document data in the collection.
442+
*
443+
* @throws Exception
444+
*
445+
* @param mixed $collectionId - collection id as a string or number
446+
* @param boolean $withRevisions - optional boolean whether or not to include document revision ids
447+
* in the checksum calculation.
448+
* @param boolean $withData - optional boolean whether or not to include document body data in the
449+
* checksum calculation.
450+
*
451+
* @return array - array containing keys "checksum" and "revision"
452+
*/
453+
public function getChecksum($collectionId, $withRevisions = false, $withData = false)
454+
{
455+
456+
$url = UrlHelper::buildUrl(Urls::URL_COLLECTION, array($collectionId, self::OPTION_CHECKSUM));
457+
$url = UrlHelper::appendParamsUrl($url, array('withRevisions' => $withRevisions, 'withData' => $withData));
458+
$response = $this->getConnection()->get($url);
459+
$data = $response->getJson();
460+
461+
return $data;
462+
}
463+
464+
/**
465+
* Returns the Collections revision ID
466+
*
467+
* The revision id is a server-generated string that clients can use to check whether data in a collection has
468+
* changed since the last revision check.
469+
*
470+
* @throws Exception
471+
*
472+
* @param mixed $collectionId - collection id as a string or number
473+
*
474+
* @return array - containing a key revision
475+
*/
476+
public function getRevision($collectionId)
477+
{
478+
479+
$url = UrlHelper::buildUrl(Urls::URL_COLLECTION, array($collectionId, self::OPTION_REVISION));
480+
$response = $this->getConnection()->get($url);
481+
$data = $response->getJson();
482+
483+
return $data;
484+
}
427485

428486
/**
429487
* Create a cap constraint

lib/triagens/ArangoDb/Connection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ public function put($url, $data)
181181
return $this->parseResponse($response);
182182
}
183183

184+
/**
185+
* Issue an HTTP Head request with the data provided
186+
*
187+
* @throws Exception
188+
*
189+
* @param string $url - PUT URL
190+
*
191+
* @return HttpResponse
192+
*/
193+
public function head($url)
194+
{
195+
$response = $this->executeRequest(HttpHelper::METHOD_HEAD, $url, '');
196+
197+
return $this->parseResponse($response);
198+
}
199+
184200
/**
185201
* Issue an HTTP PATCH request with the data provided
186202
*

lib/triagens/ArangoDb/Database.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ public static function listDatabases(Connection $connection)
9595
return $responseArray;
9696
}
9797

98+
/**
99+
* List user databases
100+
*
101+
* Retrieves the list of all databases the current user can access without
102+
* specifying a different username or password.
103+
*
104+
* @param Connection $connection - the connection to be used
105+
*
106+
* @link http://www.arangodb.org/manuals/1.4/HttpDatabase.html
107+
*
108+
* @return array $responseArray - The response array.
109+
*/
110+
public static function listUserDatabases(Connection $connection)
111+
{
112+
113+
$url = UrlHelper::buildUrl(Urls::URL_DATABASE, array('user'));
114+
115+
$response = $connection->get($url);
116+
117+
$responseArray = $response->getJson();
118+
119+
return $responseArray;
120+
}
121+
98122

99123
/**
100124
* Retrieves information about the current database

lib/triagens/ArangoDb/DocumentHandler.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class DocumentHandler extends
5858
* <li>'includeInternals' - Deprecated, please use '_includeInternals'.</li>
5959
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
6060
* <li>'ignoreHiddenAttributes' - Deprecated, please use '_ignoreHiddenAttributes'.</li>
61+
* <li>'revision' - the documents revision</li>
6162
* </p>
6263
*
6364
* @return Document - the document fetched from the server
@@ -83,13 +84,19 @@ public function get($collectionId, $documentId, array $options = array())
8384
* <li>'includeInternals' - Deprecated, please use '_includeInternals'.</li>
8485
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
8586
* <li>'ignoreHiddenAttributes' - Deprecated, please use '_ignoreHiddenAttributes'.</li>
87+
* <li>'revision' - the documents revision</li>
8688
* </p>
8789
*
8890
* @return Document - the document fetched from the server
8991
*/
9092
public function getById($collectionId, $documentId, array $options = array())
9193
{
9294
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($collectionId, $documentId));
95+
if (array_key_exists("revision", $options)) {
96+
$url = UrlHelper::appendParamsUrl($url, array('rev' => $options["revision"]));
97+
unset($options["revision"]);
98+
}
99+
93100
$response = $this->getConnection()->get($url);
94101

95102
$data = $response->getJson();
@@ -100,6 +107,32 @@ public function getById($collectionId, $documentId, array $options = array())
100107
}
101108

102109

110+
/**
111+
* Gets information about a single documents from a collection
112+
*
113+
* This will throw if the document cannot be fetched from the server
114+
*
115+
*
116+
* @throws Exception
117+
*
118+
* @param mixed $collectionId - collection id as a string or number
119+
* @param mixed $documentId - document identifier
120+
* @param string $revision - optional,a certain revision
121+
*
122+
* @return array - an array containing the key "etag".
123+
*/
124+
public function getHead($collectionId, $documentId, $revision = null)
125+
{
126+
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($collectionId, $documentId));
127+
if ($revision === null) {
128+
$url = UrlHelper::appendParamsUrl($url, array('rev' => $revision));
129+
}
130+
131+
$response = $this->getConnection()->head($url);
132+
133+
return $response->getHeaders();
134+
}
135+
103136
/**
104137
* Intermediate function to call the createFromArray function from the right context
105138
*

lib/triagens/ArangoDb/Urls.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,5 @@ abstract class Urls
202202
* base URL part for database management
203203
*/
204204
const URL_DATABASE = '/_api/database';
205+
205206
}

tests/CollectionExtendedTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,106 @@ public function testGetAllNonSystemCollections()
156156
}
157157
}
158158

159+
/**
160+
* test for getting the Checksum for a collection containing 3 documents in different varieties
161+
*/
162+
public function testGetChecksum()
163+
{
164+
$collectionHandler = $this->collectionHandler;
165+
$documentHandler = $this->documentHandler;
166+
167+
$collection = new Collection();
168+
$collection->setName("ArangoDB_PHP_TestSuite_TestCollection_01");
169+
170+
$collection->setId($collectionHandler->create($collection));
171+
172+
$document = Document::createFromArray(
173+
array('someAttribute' => 'someValue1', 'someOtherAttribute' => 'someOtherValue')
174+
);
175+
$documentHandler->save($collection->getId(), $document);
176+
$document2 = Document::createFromArray(
177+
array('someAttribute' => 'someValue2', 'someOtherAttribute' => 'someOtherValue2')
178+
);
179+
$documentHandler->save($collection->getId(), $document2);
180+
$document3 = Document::createFromArray(
181+
array('someAttribute' => 'someValue3', 'someOtherAttribute' => 'someOtherValue')
182+
);
183+
$documentHandler->save($collection->getId(), $document3);
184+
185+
$checksum1 = $collectionHandler->getChecksum($collection->getName(), true, true);
186+
$checksum2 = $collectionHandler->getChecksum($collection->getName());
187+
$checksum3 = $collectionHandler->getChecksum($collection->getName(), false, true);
188+
$checksum4 = $collectionHandler->getChecksum($collection->getName(), true);
189+
$revision = $checksum1['revision'];
190+
$this->assertEquals($revision, $checksum2['revision']);
191+
$this->assertEquals($revision, $checksum3['revision']);
192+
$this->assertEquals($revision, $checksum4['revision']);
193+
194+
$this->assertNotEquals($checksum1['checksum'], $checksum2['checksum']);
195+
$this->assertNotEquals($checksum1['checksum'], $checksum3['checksum']);
196+
$this->assertNotEquals($checksum1['checksum'], $checksum4['checksum']);
197+
$this->assertNotEquals($checksum2['checksum'], $checksum3['checksum']);
198+
$this->assertNotEquals($checksum2['checksum'], $checksum4['checksum']);
199+
$this->assertNotEquals($checksum3['checksum'], $checksum4['checksum']);
200+
201+
$collectionHandler->drop($collection);
202+
}
203+
204+
/**
205+
*
206+
* test for getting the Checksum for a non existing collection
207+
*/
208+
public function testGetChecksumWithException()
209+
{
210+
$collectionHandler = $this->collectionHandler;
211+
try {
212+
$collectionHandler->getChecksum("nonExisting", true, true);
213+
} catch (\Exception $e) {
214+
$this->assertEquals($e->getCode() , 404);
215+
}
216+
}
217+
218+
/**
219+
* test for getting the , true, true for a collection
220+
*/
221+
public function testGetRevision()
222+
{
223+
$collectionHandler = $this->collectionHandler;
224+
$documentHandler = $this->documentHandler;
225+
226+
$collection = new Collection();
227+
$collection->setName("ArangoDB_PHP_TestSuite_TestCollection_01");
228+
229+
$collection->setId($collectionHandler->create($collection));
230+
$revision = $collectionHandler->getRevision($collection->getName());
231+
$this->assertArrayHasKey('revision', $revision);
232+
233+
$document = Document::createFromArray(
234+
array('someAttribute' => 'someValue1', 'someOtherAttribute' => 'someOtherValue')
235+
);
236+
$documentHandler->save($collection->getId(), $document);
237+
238+
$revision2 = $collectionHandler->getRevision($collection->getName());
239+
240+
$this->assertNotEquals($revision2['revision'], $revision['revision']);
241+
242+
$collectionHandler->drop($collection);
243+
}
244+
245+
/**
246+
*
247+
* test for getting the revision for a non existing collection
248+
*/
249+
public function testGetRevisionWithException()
250+
{
251+
$collectionHandler = $this->collectionHandler;
252+
try {
253+
$collectionHandler->getRevision("nonExisting");
254+
} catch (\Exception $e) {
255+
$this->assertEquals($e->getCode() , 404);
256+
}
257+
}
258+
159259

160260
/**
161261
* test for creation, rename, and delete of a collection

tests/DatabaseTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ public function testCreateDatabaseDeleteIt()
5858
);
5959

6060
$response = Database::listDatabases($this->connection);
61-
62-
$this->assertCount(1, $response['result']);
61+
$this->assertArrayNotHasKey($database, array_flip($response['result']));
6362
}
6463

6564

6665
/**
67-
* Test if Databases can be created, if they can be listed and deleted again
66+
* Test if Databases can be created, if they can be listed, if they can be listed for the current user and deleted again
6867
*/
6968
public function testCreateDatabaseGetListOfDatabasesAndDeleteItAgain()
7069
{
@@ -81,7 +80,11 @@ public function testCreateDatabaseGetListOfDatabasesAndDeleteItAgain()
8180

8281
$response = Database::listDatabases($this->connection);
8382

84-
$this->assertCount(2, $response['result']);
83+
$this->assertArrayHasKey($database, array_flip($response['result']));
84+
85+
$responseUser = Database::listUserDatabases($this->connection);
86+
87+
$this->assertArrayHasKey($database, array_flip($responseUser['result']));
8588

8689

8790
$response = Database::delete($this->connection, $database);

0 commit comments

Comments
 (0)