From 27f8cd2443ac34fc7ccb4cb79107cef23e50c311 Mon Sep 17 00:00:00 2001 From: Frank Mayer Date: Fri, 5 Oct 2012 01:37:09 +0300 Subject: [PATCH 1/2] Added version variable and a static getVersion() method to COnnection.php. Signed-off-by: Frank Mayer --- lib/triagens/ArangoDb/Connection.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/triagens/ArangoDb/Connection.php b/lib/triagens/ArangoDb/Connection.php index 1391479d..c9317378 100644 --- a/lib/triagens/ArangoDb/Connection.php +++ b/lib/triagens/ArangoDb/Connection.php @@ -20,6 +20,12 @@ * @package ArangoDbPhpClient */ class Connection { + /** + * Api Version + * @var string + */ + public static $_apiVersion = '0.3.1'; + /** * Connection options * @var array @@ -257,4 +263,12 @@ private function executeRequest($method, $url, $data) { throw new ClientException('Whoops, this should never happen'); } + /** + * Get the client api version + * + * @return string + */ + public static function getVersion() { + return self::$_apiVersion; + } } From 65c1fc122bcb130a9be901a4c95c78b472ded1fd Mon Sep 17 00:00:00 2001 From: Frank Mayer Date: Sat, 6 Oct 2012 22:21:58 +0300 Subject: [PATCH 2/2] Unit tests: Initial commit Signed-off-by: Frank Mayer --- UNITTESTS.md | 21 ++++++ tests/CollectionBasicTest.php | 60 ++++++++++++++++ tests/CollectionExtendedTest.php | 117 +++++++++++++++++++++++++++++++ tests/ConnectionTest.php | 32 +++++++++ tests/DocumentBasicTest.php | 77 ++++++++++++++++++++ tests/DocumentExtendedTest.php | 75 ++++++++++++++++++++ tests/StatementTest.php | 66 +++++++++++++++++ tests/bootstrap.php | 46 ++++++++++++ tests/phpunit.xml | 13 ++++ 9 files changed, 507 insertions(+) create mode 100644 UNITTESTS.md create mode 100644 tests/CollectionBasicTest.php create mode 100644 tests/CollectionExtendedTest.php create mode 100644 tests/ConnectionTest.php create mode 100644 tests/DocumentBasicTest.php create mode 100644 tests/DocumentExtendedTest.php create mode 100644 tests/StatementTest.php create mode 100644 tests/bootstrap.php create mode 100644 tests/phpunit.xml diff --git a/UNITTESTS.md b/UNITTESTS.md new file mode 100644 index 00000000..85085657 --- /dev/null +++ b/UNITTESTS.md @@ -0,0 +1,21 @@ +# PHPUnit Tests for ArangoDB-PHP + +To run the unit tests, cd into the tests folder and run: + + +phpunit --testsuite ArangoDB-PHP + + + +###Changelog: + +2012-10-06 : Some tests are failing because of issue https://github.com/triAGENS/ArangoDB/issues/220 + +2012-10-06 : Initial Commit. This first batch covers most important parts of: +- Connection +- Collection +- CollectionHandler +- Document +- Documenthandler +- Statements + diff --git a/tests/CollectionBasicTest.php b/tests/CollectionBasicTest.php new file mode 100644 index 00000000..7a05414a --- /dev/null +++ b/tests/CollectionBasicTest.php @@ -0,0 +1,60 @@ +connection = getConnection(); + } + + /** + * Test if Collection and CollectionHandler instances can be initialized + */ + public function testInitializeCollection() + { + $connection = $this->connection; + $collection = new \triagens\ArangoDb\Collection(); + $this->assertInstanceOf('triagens\ArangoDB\Collection', $collection); + $collectionHandler = new \triagens\ArangoDb\CollectionHandler($connection); + $this->assertInstanceOf('triagens\ArangoDB\Collection', $collection); + } + + /** + * Try to create and delete a collection + */ + public function testCreateAndDeleteCollection() + { + $connection = $this->connection; + $collection = new \triagens\ArangoDb\Collection(); + $collectionHandler = new \triagens\ArangoDb\CollectionHandler($connection); + + $name = 'ArangoDB-PHP-TestSuite-TestCollection-01'; + $collection->setName($name); + + $response = $collectionHandler->add($collection); + + $this->assertTrue(is_numeric($response), 'Did not return a numeric id!'); + + $resultingCollection = $collectionHandler->get($response); + + $resultingAttribute = $resultingCollection->getName(); + $this->assertTrue($name === $resultingAttribute, 'The created collection name and resulting collection name do not match!'); + + $response = $collectionHandler->delete($collection); + } + + public function tearDown() + { + unset($this->connection); + } +} diff --git a/tests/CollectionExtendedTest.php b/tests/CollectionExtendedTest.php new file mode 100644 index 00000000..9fc3f256 --- /dev/null +++ b/tests/CollectionExtendedTest.php @@ -0,0 +1,117 @@ +connection = getConnection(); + $this->collection = new \triagens\ArangoDb\Collection(); + $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection); + } + + /** + * test for creation, get, and delete of a collection with waitForSync default value (no setting) + */ + public function testCreateAndDeleteCollectionWithWaitForSyncDefault() + { + $collection = $this->collection; + $collectionHandler = $this->collectionHandler; + + $resultingAttribute = $collection->getWaitForSync(); + print_r($resultingAttribute); + $this->assertTrue(null === $resultingAttribute, 'Default waitForSync in API should be NULL!'); + + $collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01'); + + $response = $collectionHandler->add($collection); + + $this->assertTrue(is_numeric($response), 'Adding collection did not return an id!'); + + $resultingCollection = $collectionHandler->get($response); + + $resultingAttribute = $resultingCollection->getWaitForSync(); + $this->assertTrue(false === $resultingAttribute, 'Default Server waitForSync should return false!'); + + $response = $collectionHandler->delete($collection); + $this->assertTrue(true === $response, 'Delete should return true!'); + } + + /** + * test for creation, get, and delete of a collection with waitForSync set to true + */ + public function testCreateGetAndDeleteCollectionWithWaitForSyncTrue() + { + $collection = $this->collection; + $collectionHandler = $this->collectionHandler; + + $collection->setWaitForSync(true); + $resultingAttribute = $collection->getWaitForSync(); + $this->assertTrue(true === $resultingAttribute, 'WaitForSync should be true!'); + $collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01'); + + $response = $collectionHandler->add($collection); + + $resultingCollection = $collectionHandler->get($response); + + $resultingAttribute = $resultingCollection->getWaitForSync(); + $this->assertTrue(true === $resultingAttribute, 'Server waitForSync should return true!'); + + $response = $collectionHandler->delete($collection); + $this->assertTrue(true === $response, 'Delete should return true!'); + } + + /** + * test for creation, get, and delete of a collection given its settings through createFromArray() + */ + public function testCreateGetAndDeleteCollectionThroughCreateFromArrayWithWaitForSyncTrue() + { + $collectionHandler = $this->collectionHandler; + + $collection = Collection::createFromArray(array('name' => 'ArangoDB-PHP-TestSuite-TestCollection-01', 'waitForSync' => true)); + $response = $collectionHandler->add($collection); + + $resultingCollection = $collectionHandler->get($response); + + $resultingAttribute = $resultingCollection->getWaitForSync(); + $this->assertTrue(true === $resultingAttribute, 'Server waitForSync should return true!'); + + $response = $collectionHandler->delete($collection); + $this->assertTrue(true === $response, 'Delete should return true!'); + } + + /** + * test to set some attributes and get all attributes of the collection through getAll() + */ + public function testGetAll() + { + $collection = Collection::createFromArray(array('name' => 'ArangoDB-PHP-TestSuite-TestCollection-01', 'waitForSync' => true)); + $result = $collection->getAll(); + + $this->assertArrayHasKey('id', $result, 'Id field should exist, empty or with an id'); + $this->assertTrue(true === ($result['name'] == 'ArangoDB-PHP-TestSuite-TestCollection-01'), 'name should return ArangoDB-PHP-TestSuite-TestCollection-01!'); + $this->assertTrue(true === ($result['waitForSync'] == true), 'waitForSync should return true!'); + + } + + public function tearDown() + { + try { + $response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + + unset($this->collectionHandler); + unset($this->collection); + unset($this->connection); + } +} diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php new file mode 100644 index 00000000..63ca4c8d --- /dev/null +++ b/tests/ConnectionTest.php @@ -0,0 +1,32 @@ +assertInstanceOf('triagens\ArangoDB\Connection', $connection); + } + + /** + * This is just a test to really test connectivity with the server before moving on to further tests. + */ + public function testGetStatus() + { + $connection = getConnection(); + $response = $connection->get('/_admin/status'); + $this->assertTrue($response->getHttpCode() == 200, 'Did not return http code 200'); + } +} diff --git a/tests/DocumentBasicTest.php b/tests/DocumentBasicTest.php new file mode 100644 index 00000000..824daf6a --- /dev/null +++ b/tests/DocumentBasicTest.php @@ -0,0 +1,77 @@ +connection = getConnection(); + $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection); + $this->collection = new \triagens\ArangoDb\Collection(); + $this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01'); + $this->collectionHandler->add($this->collection); + } + + /** + * Test if Document and DocumentHandler instances can be initialized + */ + public function testInitializeDocument() + { + $connection = $this->connection; + $this->collection = new \triagens\ArangoDb\Collection(); + $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection); + $document = new \triagens\ArangoDb\Document(); + $this->assertInstanceOf('triagens\ArangoDB\Document', $document); + $this->assertInstanceOf('triagens\ArangoDB\Document', $document); + unset ($document); + } + + /** + * Try to create and delete a document + */ + public function testCreateAndDeleteDocument() + { + $connection = $this->connection; + $collection = $this->collection; + $collectionHandler = $this->collectionHandler; + $document = new \triagens\ArangoDb\Document(); + $documentHandler = new \triagens\ArangoDb\DocumentHandler($connection); + + $document->someAttribute = 'someValue'; + + $documentId = $documentHandler->add($collection->getId(), $document); + + print_r($documentId); + + $resultingDocument = $documentHandler->get($collection->getId(), $documentId); + + $resultingAttribute = $resultingDocument->someAttribute; + $this->assertTrue($resultingAttribute === 'someValue', 'Created document id is not numeric!'); + + $response = $documentHandler->delete($document); + } + + public function tearDown() + { + try { + $response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + + + unset($this->documentHandler); + unset($this->document); + unset($this->collectionHandler); + unset($this->collection); + unset($this->connection); + } +} diff --git a/tests/DocumentExtendedTest.php b/tests/DocumentExtendedTest.php new file mode 100644 index 00000000..e8b3b980 --- /dev/null +++ b/tests/DocumentExtendedTest.php @@ -0,0 +1,75 @@ +connection = getConnection(); + $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection); + $this->collection = new \triagens\ArangoDb\Collection(); + $this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01'); + $this->collectionHandler->add($this->collection); + $this->documentHandler = new DocumentHandler($this->connection); + } + + /** + * test for creation, get, and delete of a document given its settings through createFromArray() + */ + public function testCreateGetAndDeleteDocumentThroughCreateFromArray() + { + $documentHandler = $this->documentHandler; + + $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')); + $documentId = $documentHandler->add($this->collection->getId(), $document); + + $this->assertTrue(is_numeric($documentId), 'Did not return an id!'); + + $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId); + + $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id'); + $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue')); + $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue')); + + $response = $documentHandler->delete($document); + $this->assertTrue(true === $response, 'Delete should return true!'); + } + + + /** + * test to set some attributes and get all attributes of the document through getAll() + */ + public function testGetAll() + { + $documentHandler = $this->documentHandler; + + $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')); + $documentHandler->add($this->collection->getId(), $document); + + $result = $document->getAll(); + + $this->assertTrue(true === ($result['someAttribute'] == 'someValue')); + $this->assertTrue(true === ($result['someOtherAttribute'] == 'someOtherValue')); + } + + public function tearDown() + { + try { + $response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + + unset($this->collectionHandler); + unset($this->collection); + unset($this->connection); + } +} diff --git a/tests/StatementTest.php b/tests/StatementTest.php new file mode 100644 index 00000000..2e353988 --- /dev/null +++ b/tests/StatementTest.php @@ -0,0 +1,66 @@ +connection = getConnection(); + $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection); + $this->collection = new \triagens\ArangoDb\Collection(); + $this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01'); + $this->collectionHandler->add($this->collection); + } + + /** + * This is just a test to really test connectivity with the server before moving on to further tests. + */ + public function testCreateAndDeleteDocument() + { + $connection = $this->connection; + $collection = $this->collection; + $collectionHandler = $this->collectionHandler; + $document = new \triagens\ArangoDb\Document(); + $documentHandler = new \triagens\ArangoDb\DocumentHandler($connection); + + $document->someAttribute = 'someValue'; + + $documentId = $documentHandler->add($collection->getId(), $document); + + $statement = new \triagens\ArangoDb\Statement($connection, array( + "query" => '', + "count" => true, + "batchSize" => 1000, + "sanitize" => true, + )); + $statement->setQuery('FOR a IN `ArangoDB-PHP-TestSuite-TestCollection-01` RETURN a'); + $cursor = $statement->execute(); + + $result = $cursor->current(); + + $this->assertTrue($result->someAttribute === 'someValue', 'Created document id is not numeric!'); + } + + public function tearDown() + { + try { + $response = $this->collectionHandler->delete('ArangoDB-PHP-TestSuite-TestCollection-01'); + } catch (\Exception $e) { + // don't bother us, if it's already deleted. + } + + unset($this->documentHandler); + unset($this->document); + unset($this->collectionHandler); + unset($this->collection); + unset($this->connection); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..d171e709 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,46 @@ + 'tcp://localhost:8529/', // endpoint to connect to + ConnectionOptions::OPTION_CONNECTION => 'Close', // can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections) + ConnectionOptions::OPTION_AUTH_TYPE => 'Basic', // use basic authorization + /* + ConnectionOptions::OPTION_AUTH_USER => '', // user for basic authorization + ConnectionOptions::OPTION_AUTH_PASSWD => '', // password for basic authorization + ConnectionOptions::OPTION_PORT => 8529, // port to connect to (deprecated, should use endpoint instead) + ConnectionOptions::OPTION_HOST => "localhost", // host to connect to (deprecated, should use endpoint instead) + */ + ConnectionOptions::OPTION_TIMEOUT => 5, // timeout in seconds + //ConnectionOptions::OPTION_TRACE => $traceFunc, // tracer function, can be used for debugging + ConnectionOptions::OPTION_CREATE => false, // do not create unknown collections automatically + ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST, // last update wins + ); +} + + +function getConnection(){ + return new Connection(getConnectionOptions()); +} + diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 00000000..c023a622 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,13 @@ + + + + + ConnectionTest.php + CollectionBasicTest.php + CollectionExtendedTest.php + DocumentBasicTest.php + DocumentExtendedTest.php + StatementTest.php + + +