Skip to content

Commit 7986a7e

Browse files
committed
add more tests for document APIs
1 parent 6cb22b1 commit 7986a7e

File tree

7 files changed

+457
-65
lines changed

7 files changed

+457
-65
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ The `Cursor` class will now fetch outstanding cursor result data via HTTP POST r
1818
the same address. The change is necessary because fetching further results is not an
1919
idempotent operation, but the HTTP standard requires PUT operations to be idempotent.
2020

21+
Extended maximum valid length for collection names to 256, up from 64 before. This follows a
22+
change in the ArangoDB server.
23+
2124
## Release notes for the ArangoDB-PHP driver 3.7.x
2225

2326
The corresponding ArangoDB version, ArangoDB 3.7 has dropped support for the MMFiles storage

lib/ArangoDBClient/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public function setInternalId($id)
639639
}
640640

641641

642-
if (!preg_match('/^[a-zA-Z0-9_-]{1,64}\/' . self::KEY_REGEX_PART . '$/', $id)) {
642+
if (!preg_match('/^[a-zA-Z0-9_-]{1,256}\/' . self::KEY_REGEX_PART . '$/', $id)) {
643643
throw new ClientException('Invalid format for document id');
644644
}
645645

lib/ArangoDBClient/DocumentHandler.php

Lines changed: 70 additions & 47 deletions
Large diffs are not rendered by default.

lib/ArangoDBClient/Handler.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,18 @@ protected function includeOptionsInParams($options, array $includeArray = [])
100100
{
101101
$params = [];
102102
foreach ($options as $key => $value) {
103-
if (array_key_exists($key, $includeArray)) {
104-
if ($key === ConnectionOptions::OPTION_UPDATE_POLICY) {
105-
UpdatePolicy::validate($value);
106-
}
103+
if ($key === ConnectionOptions::OPTION_UPDATE_POLICY) {
104+
UpdatePolicy::validate($value);
105+
}
106+
if ($value === null && isset($includeArray[$key])) {
107+
$params[$key] = $includeArray[$key];
108+
} else {
107109
$params[$key] = $value;
108-
if ($value === null) {
109-
$params[$key] = $includeArray[$key];
110-
}
111110
}
112111
}
113112

114113
foreach ($includeArray as $key => $value) {
115-
if (!array_key_exists($key, $options)) {
114+
if (!isset($options[$key])) {
116115
if ($key === ConnectionOptions::OPTION_UPDATE_POLICY) {
117116
UpdatePolicy::validate($value);
118117
}

tests/CollectionBasicTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,64 @@ public function testInitializeCollectionWithEdgeType()
8787

8888
static::assertEquals(Collection::TYPE_EDGE, $collection->getType());
8989
}
90+
91+
/**
92+
* Try to create a collection with a long name
93+
*/
94+
public function testCreateCollectionLongName()
95+
{
96+
$connection = $this->connection;
97+
$collection = new Collection();
98+
$collectionHandler = new CollectionHandler($connection);
99+
100+
$name = 'ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp . '_00000000000000000028477732232578523444444444444444444444444444444444442323232';
101+
static::assertTrue(strlen($name) > 64);
102+
103+
try {
104+
$collectionHandler->drop($name);
105+
} catch (Exception $e) {
106+
//Silence the exception
107+
}
108+
109+
$collection->setName($name);
110+
$response = $collectionHandler->create($collection);
111+
112+
static::assertTrue(is_numeric($response), 'Did not return a numeric id!');
113+
114+
$resultingCollection = $collectionHandler->get($response);
115+
116+
$resultingAttribute = $resultingCollection->getName();
117+
static::assertSame(
118+
$name, $resultingAttribute, 'The created collection name and resulting collection name do not match!'
119+
);
120+
121+
static::assertEquals(Collection::getDefaultType(), $resultingCollection->getType());
122+
123+
$collectionHandler->drop($collection);
124+
}
125+
126+
127+
/**
128+
* Try to create a collection with a too long name
129+
*/
130+
public function testCreateCollectionTooLongName()
131+
{
132+
$connection = $this->connection;
133+
$collection = new Collection();
134+
$collectionHandler = new CollectionHandler($connection);
135+
136+
$name = 'ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp . str_repeat('x', 256);
137+
static::assertTrue(strlen($name) > 256);
138+
139+
$collection->setName($name);
140+
try {
141+
$collectionHandler->create($collection);
142+
} catch (Exception $exception400) {
143+
//Silence the exception
144+
}
145+
146+
static::assertEquals(400, $exception400->getCode());
147+
}
90148

91149

92150
/**

tests/DocumentBasicTest.php

Lines changed: 161 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ public function testInitializeDocument()
6363
}
6464

6565

66+
/**
67+
* Try to create a document silently
68+
*/
69+
public function testInsertSilent()
70+
{
71+
$connection = $this->connection;
72+
$collection = $this->collection;
73+
$document = Document::createFromArray(['_key' => 'me', 'value' => 1]);
74+
$documentHandler = new DocumentHandler($connection);
75+
76+
$document = $documentHandler->insert($collection->getName(), $document, ['silent' => true ]);
77+
static::assertNull($document);
78+
}
79+
80+
81+
/**
82+
* Try to create a document silently - with an error
83+
*/
84+
public function testInsertSilentWithError()
85+
{
86+
$connection = $this->connection;
87+
$collection = $this->collection;
88+
$document = Document::createFromArray(['_key' => 'me', 'value' => 1]);
89+
$documentHandler = new DocumentHandler($connection);
90+
91+
// insert the document once
92+
$result = $documentHandler->insert($collection->getName(), $document, ['silent' => true ]);
93+
static::assertNull($result);
94+
95+
// and try to insert it again
96+
try {
97+
$documentHandler->insert($collection->getName(), $document, ['silent' => true ]);
98+
} catch (\Exception $exception409) {
99+
}
100+
static::assertEquals(409, $exception409->getCode());
101+
}
102+
103+
66104
/**
67105
* Try to create a document and return it
68106
*/
@@ -82,7 +120,7 @@ public function testInsertReturnNew()
82120

83121

84122
/**
85-
* Try to create a document and overwrite it
123+
* Try to create a document and overwrite it, using deprecated overwrite option
86124
*/
87125
public function testInsertOverwrite()
88126
{
@@ -91,21 +129,36 @@ public function testInsertOverwrite()
91129
$document = Document::createFromArray(['_key' => 'me', 'value' => 1]);
92130
$documentHandler = new DocumentHandler($connection);
93131

94-
$document = $documentHandler->insert($collection->getName(), $document, ['returnNew' => true ]);
132+
$document = $documentHandler->insert($collection->getName(), $document, ['returnNew' => true]);
95133

96134
static::assertEquals('me', $document['_key']);
97135
static::assertEquals('me', $document['new']['_key']);
98136
static::assertEquals(1, $document['new']['value']);
99137

138+
try {
139+
$documentHandler->insert($collection->getName(), $document, ['overwrite' => false]);
140+
} catch (\Exception $exception409) {
141+
}
142+
static::assertEquals(409, $exception409->getCode());
143+
144+
$document = Document::createFromArray(['_key' => 'me', 'value' => 2]);
145+
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => true, 'returnOld' => true, 'returnNew' => true]);
146+
static::assertEquals('me', $document['_key']);
147+
static::assertEquals('me', $document['old']['_key']);
148+
static::assertEquals('me', $document['new']['_key']);
149+
static::assertEquals(1, $document['old']['value']);
150+
static::assertEquals(2, $document['new']['value']);
151+
152+
100153
$document = Document::createFromArray(['_key' => 'other', 'value' => 2]);
101-
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => false, 'returnOld' => true, 'returnNew' => true ]);
154+
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => false, 'returnOld' => true, 'returnNew' => true]);
102155

103156
static::assertEquals('other', $document['_key']);
104157
static::assertEquals('other', $document['new']['_key']);
105158
static::assertEquals(2, $document['new']['value']);
106159

107160
$document = Document::createFromArray(['_key' => 'other', 'value' => 3]);
108-
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => true, 'returnOld' => true, 'returnNew' => true ]);
161+
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => true, 'returnOld' => true, 'returnNew' => true]);
109162

110163
static::assertEquals('other', $document['_key']);
111164
static::assertEquals('other', $document['old']['_key']);
@@ -114,12 +167,90 @@ public function testInsertOverwrite()
114167
static::assertEquals(3, $document['new']['value']);
115168

116169
$document = Document::createFromArray(['_key' => 'foo', 'value' => 4]);
117-
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => true, 'returnOld' => true, 'returnNew' => true ]);
170+
$document = $documentHandler->insert($collection->getName(), $document, ['overwrite' => true, 'returnOld' => true, 'returnNew' => true]);
118171

119172
static::assertEquals('foo', $document['_key']);
120173
static::assertEquals('foo', $document['new']['_key']);
121174
static::assertEquals(4, $document['new']['value']);
122175
}
176+
177+
/**
178+
* Try to create a document and overwrite it, using overwriteMode option
179+
*/
180+
public function testInsertOverwriteMode()
181+
{
182+
$connection = $this->connection;
183+
$collection = $this->collection;
184+
$document = Document::createFromArray(['_key' => 'me', 'value' => 1]);
185+
$documentHandler = new DocumentHandler($connection);
186+
187+
$document = $documentHandler->insert($collection->getName(), $document, ['returnNew' => true]);
188+
189+
static::assertEquals('me', $document['_key']);
190+
static::assertEquals('me', $document['new']['_key']);
191+
static::assertEquals(1, $document['new']['value']);
192+
193+
// conflict mode
194+
try {
195+
$documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'conflict']);
196+
} catch (\Exception $exception409) {
197+
}
198+
static::assertEquals(409, $exception409->getCode());
199+
200+
$document = Document::createFromArray(['_key' => 'other-no-conflict', 'value' => 1]);
201+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'conflict']);
202+
203+
static::assertEquals($collection->getName() . '/other-no-conflict', $document);
204+
205+
206+
// ignore mode
207+
$document = Document::createFromArray(['_key' => 'me', 'value' => 2]);
208+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'ignore', 'returnOld' => true, 'returnNew' => true]);
209+
210+
static::assertEquals('me', $document['_key']);
211+
static::assertFalse(isset($document['_new']));
212+
static::assertFalse(isset($document['_old']));
213+
214+
215+
$document = Document::createFromArray(['_key' => 'yet-another', 'value' => 3]);
216+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'ignore', 'returnOld' => true, 'returnNew' => true]);
217+
218+
static::assertEquals('yet-another', $document['_key']);
219+
static::assertEquals('yet-another', $document['new']['_key']);
220+
static::assertEquals(3, $document['new']['value']);
221+
static::assertFalse(isset($document['_old']));
222+
223+
224+
$document = Document::createFromArray(['_key' => 'yet-another', 'value' => 4]);
225+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'ignore']);
226+
227+
static::assertEquals($collection->getName() . '/yet-another', $document);
228+
229+
230+
// update mode
231+
$document = Document::createFromArray(['_key' => 'me', 'foo' => 'bar']);
232+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'update', 'returnOld' => true, 'returnNew' => true ]);
233+
234+
static::assertEquals('me', $document['_key']);
235+
static::assertEquals('me', $document['old']['_key']);
236+
static::assertEquals(1, $document['old']['value']);
237+
static::assertEquals('me', $document['new']['_key']);
238+
static::assertEquals(1, $document['new']['value']);
239+
static::assertEquals('bar', $document['new']['foo']);
240+
241+
242+
// replace mode
243+
$document = Document::createFromArray(['_key' => 'me', 'qux' => 'qaz']);
244+
$document = $documentHandler->insert($collection->getName(), $document, ['overwriteMode' => 'replace', 'returnOld' => true, 'returnNew' => true ]);
245+
246+
static::assertEquals('me', $document['_key']);
247+
static::assertEquals('me', $document['new']['_key']);
248+
static::assertEquals(1, $document['old']['value']);
249+
static::assertEquals('bar', $document['old']['foo']);
250+
static::assertFalse(isset($document['new']['foo']));
251+
static::assertFalse(isset($document['new']['value']));
252+
static::assertEquals('qaz', $document['new']['qux']);
253+
}
123254

124255

125256
/**
@@ -142,7 +273,7 @@ public function testCreateAndDeleteDocumentWithId()
142273
$id = $resultingDocument->getHandle();
143274
static::assertSame($collection->getName() . '/' . $key, $id);
144275

145-
$documentHandler->remove($document);
276+
static::assertTrue($documentHandler->remove($document));
146277
}
147278

148279

@@ -165,7 +296,30 @@ public function testCreateAndDeleteDocument()
165296
$resultingAttribute = $resultingDocument->someAttribute;
166297
static::assertSame('someValue', $resultingAttribute, 'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute);
167298

168-
$documentHandler->remove($document);
299+
static::assertTrue($documentHandler->remove($document));
300+
}
301+
302+
303+
/**
304+
* Try to create and silently delete a document
305+
*/
306+
public function testCreateAndDeleteDocumentSilent()
307+
{
308+
$connection = $this->connection;
309+
$collection = $this->collection;
310+
$document = new Document();
311+
$documentHandler = new DocumentHandler($connection);
312+
313+
$document->someAttribute = 'someValue';
314+
315+
$documentId = $documentHandler->save($collection->getName(), $document);
316+
317+
$resultingDocument = $documentHandler->get($collection->getName(), $documentId);
318+
319+
$resultingAttribute = $resultingDocument->someAttribute;
320+
static::assertSame('someValue', $resultingAttribute, 'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute);
321+
322+
static::assertTrue($documentHandler->remove($document, ['silent' => true]));
169323
}
170324

171325

@@ -231,7 +385,6 @@ public function testCreateAndDeleteDocumentWithoutCreatedCollectionAndOptionCrea
231385
}
232386

233387

234-
235388
/**
236389
* Try to create and delete a document using a defined key
237390
*/

0 commit comments

Comments
 (0)