Skip to content

Commit 2fc4c1f

Browse files
committed
added symbolic constants for collection types, added tests
1 parent df0609f commit 2fc4c1f

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

lib/triagens/ArangoDb/Collection.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ class Collection {
6363
/**
6464
* properties option
6565
*/
66-
const OPTION_PROPERTIES = 'properties';
66+
const OPTION_PROPERTIES = 'properties';
67+
68+
/**
69+
* document collection type
70+
*/
71+
const TYPE_DOCUMENT = 2;
72+
73+
/**
74+
* edge collection type
75+
*/
76+
const TYPE_EDGE = 3;
6777

6878
/**
6979
* Constructs an empty collection
@@ -90,6 +100,15 @@ public static function createFromArray(array $values) {
90100
return $collection;
91101
}
92102

103+
/**
104+
* Get the default collection type
105+
*
106+
* @return string - name
107+
*/
108+
public static function getDefaultType() {
109+
return self::TYPE_DOCUMENT;
110+
}
111+
93112
/**
94113
* Clone a collection
95114
* Returns the clone
@@ -140,7 +159,7 @@ public function getAll() {
140159
self::ENTRY_ID => $this->_id,
141160
self::ENTRY_NAME => $this->_name,
142161
self::ENTRY_WAIT_SYNC => $this->_waitForSync,
143-
self::ENTRY_TYPE => $this->_type,
162+
self::ENTRY_TYPE => $this->_type,
144163
);
145164
}
146165

@@ -175,6 +194,7 @@ public function set($key, $value) {
175194
$this->setWaitForSync($value);
176195
return;
177196
}
197+
178198
if ($key === self::ENTRY_TYPE) {
179199
$this->setType($value);
180200
return;
@@ -247,10 +267,14 @@ public function getName() {
247267
public function setType($type) {
248268
assert(is_int($type));
249269

250-
if ($this->_type !== NULL && $this->$type != $type) {
270+
if ($this->_type !== NULL && $this->_type != $type) {
251271
throw new ClientException('Should not update the type of an existing collection');
252272
}
253273

274+
if ($type != self::TYPE_DOCUMENT && $type != self::TYPE_EDGE) {
275+
throw new ClientException('Invalid type used for collection');
276+
}
277+
254278
$this->_type = $type;
255279
}
256280

@@ -282,8 +306,5 @@ public function setWaitForSync($value) {
282306
public function getWaitForSync() {
283307
return $this->_waitForSync;
284308
}
285-
286-
287-
288309

289310
}

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public function create(Collection $collection) {
165165
if ($collection->getWaitForSync() === NULL) {
166166
$collection->setWaitForSync($this->getConnection()->getOption(ConnectionOptions::OPTION_WAIT_SYNC));
167167
}
168-
$type = $collection->getType()? $collection->getType(): 2;
168+
169+
$type = $collection->getType() ? $collection->getType() : Collection::getDefaultType();
169170
$params = array(Collection::ENTRY_NAME => $collection->getName(), Collection::ENTRY_TYPE => $type, Collection::ENTRY_WAIT_SYNC => $collection->getWaitForSync());
170171
$response = $this->getConnection()->post(Urls::URL_COLLECTION, json_encode($params));
171172

@@ -331,5 +332,4 @@ private function getCursorOptions($sanitize) {
331332
);
332333
}
333334

334-
335335
}

tests/CollectionBasicTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public function setUp()
1616
{
1717
$this->connection = getConnection();
1818
}
19+
20+
/**
21+
* Test default collection type
22+
*/
23+
public function testDefaultCollectionType()
24+
{
25+
$this->assertEquals(Collection::TYPE_DOCUMENT, Collection::getDefaultType());
26+
}
1927

2028
/**
2129
* Test if Collection and CollectionHandler instances can be initialized
@@ -28,6 +36,28 @@ public function testInitializeCollection()
2836
$collectionHandler = new \triagens\ArangoDb\CollectionHandler($connection);
2937
$this->assertInstanceOf('triagens\ArangoDb\Collection', $collection);
3038
}
39+
40+
/**
41+
* Test setting and getting collection types
42+
*/
43+
public function testInitializeCollectionWithDocumentType()
44+
{
45+
$collection = new \triagens\ArangoDb\Collection();
46+
$collection->setType(Collection::TYPE_DOCUMENT);
47+
48+
$this->assertEquals(Collection::TYPE_DOCUMENT, $collection->getType());
49+
}
50+
51+
/**
52+
* Test setting and getting collection types
53+
*/
54+
public function testInitializeCollectionWithEdgeType()
55+
{
56+
$collection = new \triagens\ArangoDb\Collection();
57+
$collection->setType(Collection::TYPE_EDGE);
58+
59+
$this->assertEquals(Collection::TYPE_EDGE, $collection->getType());
60+
}
3161

3262
/**
3363
* Try to create and delete a collection
@@ -51,6 +81,8 @@ public function testCreateAndDeleteCollection()
5181
$resultingAttribute = $resultingCollection->getName();
5282
$this->assertTrue($name === $resultingAttribute, 'The created collection name and resulting collection name do not match!');
5383

84+
$this->assertEquals(Collection::getDefaultType(), $resultingCollection->getType());
85+
5486
$response = $collectionHandler->delete($collection);
5587
}
5688

0 commit comments

Comments
 (0)