Skip to content

Commit 65c1fc1

Browse files
committed
Unit tests: Initial commit
Signed-off-by: Frank Mayer <frank@frankmayer.net>
1 parent 27f8cd2 commit 65c1fc1

9 files changed

+507
-0
lines changed

UNITTESTS.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# PHPUnit Tests for ArangoDB-PHP
2+
3+
To run the unit tests, cd into the tests folder and run:
4+
5+
<code>
6+
phpunit --testsuite ArangoDB-PHP
7+
</code>
8+
9+
10+
###Changelog:
11+
12+
2012-10-06 : Some tests are failing because of issue https://github.com/triAGENS/ArangoDB/issues/220
13+
14+
2012-10-06 : Initial Commit. This first batch covers most important parts of:
15+
- Connection
16+
- Collection
17+
- CollectionHandler
18+
- Document
19+
- Documenthandler
20+
- Statements
21+

tests/CollectionBasicTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client testsuite
5+
* File: collectionbasictest.php
6+
*
7+
* @package ArangoDbPhpClient
8+
* @author Frank Mayer
9+
*/
10+
11+
namespace triagens\ArangoDB;
12+
13+
class CollectionBasicTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function setUp()
16+
{
17+
$this->connection = getConnection();
18+
}
19+
20+
/**
21+
* Test if Collection and CollectionHandler instances can be initialized
22+
*/
23+
public function testInitializeCollection()
24+
{
25+
$connection = $this->connection;
26+
$collection = new \triagens\ArangoDb\Collection();
27+
$this->assertInstanceOf('triagens\ArangoDB\Collection', $collection);
28+
$collectionHandler = new \triagens\ArangoDb\CollectionHandler($connection);
29+
$this->assertInstanceOf('triagens\ArangoDB\Collection', $collection);
30+
}
31+
32+
/**
33+
* Try to create and delete a collection
34+
*/
35+
public function testCreateAndDeleteCollection()
36+
{
37+
$connection = $this->connection;
38+
$collection = new \triagens\ArangoDb\Collection();
39+
$collectionHandler = new \triagens\ArangoDb\CollectionHandler($connection);
40+
41+
$name = 'ArangoDB-PHP-TestSuite-TestCollection-01';
42+
$collection->setName($name);
43+
44+
$response = $collectionHandler->add($collection);
45+
46+
$this->assertTrue(is_numeric($response), 'Did not return a numeric id!');
47+
48+
$resultingCollection = $collectionHandler->get($response);
49+
50+
$resultingAttribute = $resultingCollection->getName();
51+
$this->assertTrue($name === $resultingAttribute, 'The created collection name and resulting collection name do not match!');
52+
53+
$response = $collectionHandler->delete($collection);
54+
}
55+
56+
public function tearDown()
57+
{
58+
unset($this->connection);
59+
}
60+
}

tests/CollectionExtendedTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client testsuite
4+
* File: collectionextendedtest.php
5+
*
6+
* @package ArangoDbPhpClient
7+
* @author Frank Mayer
8+
*/
9+
10+
namespace triagens\ArangoDB;
11+
12+
class CollectionExtendedTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function setUp()
15+
{
16+
$this->connection = getConnection();
17+
$this->collection = new \triagens\ArangoDb\Collection();
18+
$this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
19+
}
20+
21+
/**
22+
* test for creation, get, and delete of a collection with waitForSync default value (no setting)
23+
*/
24+
public function testCreateAndDeleteCollectionWithWaitForSyncDefault()
25+
{
26+
$collection = $this->collection;
27+
$collectionHandler = $this->collectionHandler;
28+
29+
$resultingAttribute = $collection->getWaitForSync();
30+
print_r($resultingAttribute);
31+
$this->assertTrue(null === $resultingAttribute, 'Default waitForSync in API should be NULL!');
32+
33+
$collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
34+
35+
$response = $collectionHandler->add($collection);
36+
37+
$this->assertTrue(is_numeric($response), 'Adding collection did not return an id!');
38+
39+
$resultingCollection = $collectionHandler->get($response);
40+
41+
$resultingAttribute = $resultingCollection->getWaitForSync();
42+
$this->assertTrue(false === $resultingAttribute, 'Default Server waitForSync should return false!');
43+
44+
$response = $collectionHandler->delete($collection);
45+
$this->assertTrue(true === $response, 'Delete should return true!');
46+
}
47+
48+
/**
49+
* test for creation, get, and delete of a collection with waitForSync set to true
50+
*/
51+
public function testCreateGetAndDeleteCollectionWithWaitForSyncTrue()
52+
{
53+
$collection = $this->collection;
54+
$collectionHandler = $this->collectionHandler;
55+
56+
$collection->setWaitForSync(true);
57+
$resultingAttribute = $collection->getWaitForSync();
58+
$this->assertTrue(true === $resultingAttribute, 'WaitForSync should be true!');
59+
$collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
60+
61+
$response = $collectionHandler->add($collection);
62+
63+
$resultingCollection = $collectionHandler->get($response);
64+
65+
$resultingAttribute = $resultingCollection->getWaitForSync();
66+
$this->assertTrue(true === $resultingAttribute, 'Server waitForSync should return true!');
67+
68+
$response = $collectionHandler->delete($collection);
69+
$this->assertTrue(true === $response, 'Delete should return true!');
70+
}
71+
72+
/**
73+
* test for creation, get, and delete of a collection given its settings through createFromArray()
74+
*/
75+
public function testCreateGetAndDeleteCollectionThroughCreateFromArrayWithWaitForSyncTrue()
76+
{
77+
$collectionHandler = $this->collectionHandler;
78+
79+
$collection = Collection::createFromArray(array('name' => 'ArangoDB-PHP-TestSuite-TestCollection-01', 'waitForSync' => true));
80+
$response = $collectionHandler->add($collection);
81+
82+
$resultingCollection = $collectionHandler->get($response);
83+
84+
$resultingAttribute = $resultingCollection->getWaitForSync();
85+
$this->assertTrue(true === $resultingAttribute, 'Server waitForSync should return true!');
86+
87+
$response = $collectionHandler->delete($collection);
88+
$this->assertTrue(true === $response, 'Delete should return true!');
89+
}
90+
91+
/**
92+
* test to set some attributes and get all attributes of the collection through getAll()
93+
*/
94+
public function testGetAll()
95+
{
96+
$collection = Collection::createFromArray(array('name' => 'ArangoDB-PHP-TestSuite-TestCollection-01', 'waitForSync' => true));
97+
$result = $collection->getAll();
98+
99+
$this->assertArrayHasKey('id', $result, 'Id field should exist, empty or with an id');
100+
$this->assertTrue(true === ($result['name'] == 'ArangoDB-PHP-TestSuite-TestCollection-01'), 'name should return ArangoDB-PHP-TestSuite-TestCollection-01!');
101+
$this->assertTrue(true === ($result['waitForSync'] == true), 'waitForSync should return true!');
102+
103+
}
104+
105+
public function tearDown()
106+
{
107+
try {
108+
$response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01');
109+
} catch (\Exception $e) {
110+
// don't bother us, if it's already deleted.
111+
}
112+
113+
unset($this->collectionHandler);
114+
unset($this->collection);
115+
unset($this->connection);
116+
}
117+
}

tests/ConnectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client testsuite
4+
* File: connectiontest.php
5+
*
6+
* @package ArangoDbPhpClient
7+
* @author Frank Mayer
8+
*/
9+
10+
namespace triagens\ArangoDB;
11+
12+
class ConnectionTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* Test if Connection instance can be initialized
16+
*/
17+
public function testInitializeConnection()
18+
{
19+
$connection = getConnection();
20+
$this->assertInstanceOf('triagens\ArangoDB\Connection', $connection);
21+
}
22+
23+
/**
24+
* This is just a test to really test connectivity with the server before moving on to further tests.
25+
*/
26+
public function testGetStatus()
27+
{
28+
$connection = getConnection();
29+
$response = $connection->get('/_admin/status');
30+
$this->assertTrue($response->getHttpCode() == 200, 'Did not return http code 200');
31+
}
32+
}

tests/DocumentBasicTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client testsuite
4+
* File: documentbasictest.php
5+
*
6+
* @package ArangoDbPhpClient
7+
* @author Frank Mayer
8+
*/
9+
10+
namespace triagens\ArangoDB;
11+
12+
class DocumentBasicTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function setUp()
15+
{
16+
$this->connection = getConnection();
17+
$this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
18+
$this->collection = new \triagens\ArangoDb\Collection();
19+
$this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
20+
$this->collectionHandler->add($this->collection);
21+
}
22+
23+
/**
24+
* Test if Document and DocumentHandler instances can be initialized
25+
*/
26+
public function testInitializeDocument()
27+
{
28+
$connection = $this->connection;
29+
$this->collection = new \triagens\ArangoDb\Collection();
30+
$this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
31+
$document = new \triagens\ArangoDb\Document();
32+
$this->assertInstanceOf('triagens\ArangoDB\Document', $document);
33+
$this->assertInstanceOf('triagens\ArangoDB\Document', $document);
34+
unset ($document);
35+
}
36+
37+
/**
38+
* Try to create and delete a document
39+
*/
40+
public function testCreateAndDeleteDocument()
41+
{
42+
$connection = $this->connection;
43+
$collection = $this->collection;
44+
$collectionHandler = $this->collectionHandler;
45+
$document = new \triagens\ArangoDb\Document();
46+
$documentHandler = new \triagens\ArangoDb\DocumentHandler($connection);
47+
48+
$document->someAttribute = 'someValue';
49+
50+
$documentId = $documentHandler->add($collection->getId(), $document);
51+
52+
print_r($documentId);
53+
54+
$resultingDocument = $documentHandler->get($collection->getId(), $documentId);
55+
56+
$resultingAttribute = $resultingDocument->someAttribute;
57+
$this->assertTrue($resultingAttribute === 'someValue', 'Created document id is not numeric!');
58+
59+
$response = $documentHandler->delete($document);
60+
}
61+
62+
public function tearDown()
63+
{
64+
try {
65+
$response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01');
66+
} catch (\Exception $e) {
67+
// don't bother us, if it's already deleted.
68+
}
69+
70+
71+
unset($this->documentHandler);
72+
unset($this->document);
73+
unset($this->collectionHandler);
74+
unset($this->collection);
75+
unset($this->connection);
76+
}
77+
}

tests/DocumentExtendedTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client testsuite
4+
* File: documentextendedtest.php
5+
*
6+
* @package ArangoDbPhpClient
7+
* @author Frank Mayer
8+
*/
9+
10+
namespace triagens\ArangoDB;
11+
12+
class DocumentExtendedTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function setUp()
15+
{
16+
$this->connection = getConnection();
17+
$this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
18+
$this->collection = new \triagens\ArangoDb\Collection();
19+
$this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
20+
$this->collectionHandler->add($this->collection);
21+
$this->documentHandler = new DocumentHandler($this->connection);
22+
}
23+
24+
/**
25+
* test for creation, get, and delete of a document given its settings through createFromArray()
26+
*/
27+
public function testCreateGetAndDeleteDocumentThroughCreateFromArray()
28+
{
29+
$documentHandler = $this->documentHandler;
30+
31+
$document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
32+
$documentId = $documentHandler->add($this->collection->getId(), $document);
33+
34+
$this->assertTrue(is_numeric($documentId), 'Did not return an id!');
35+
36+
$resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
37+
38+
$this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
39+
$this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue'));
40+
$this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue'));
41+
42+
$response = $documentHandler->delete($document);
43+
$this->assertTrue(true === $response, 'Delete should return true!');
44+
}
45+
46+
47+
/**
48+
* test to set some attributes and get all attributes of the document through getAll()
49+
*/
50+
public function testGetAll()
51+
{
52+
$documentHandler = $this->documentHandler;
53+
54+
$document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
55+
$documentHandler->add($this->collection->getId(), $document);
56+
57+
$result = $document->getAll();
58+
59+
$this->assertTrue(true === ($result['someAttribute'] == 'someValue'));
60+
$this->assertTrue(true === ($result['someOtherAttribute'] == 'someOtherValue'));
61+
}
62+
63+
public function tearDown()
64+
{
65+
try {
66+
$response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01');
67+
} catch (\Exception $e) {
68+
// don't bother us, if it's already deleted.
69+
}
70+
71+
unset($this->collectionHandler);
72+
unset($this->collection);
73+
unset($this->connection);
74+
}
75+
}

0 commit comments

Comments
 (0)