/** * Test if two Vertices can be saved and an edge can be saved connecting them but with the document & edge-handlers instead of the graphHandler * Then remove all starting with vertex1 first * There is no need for another test with handlers other than as the GraphHandler since there is no automatic edge-removal functionality when removing a vertex */ public function testSaveVerticesFromVertexHandlerAndEdgeFromEdgeHandlerBetweenThemAndRemoveFirstVertexFirst() { $vertex1 = Vertex::createFromArray($this->vertex1Array); $vertex2 = Vertex::createFromArray($this->vertex2Array); $edge1 = Edge::createFromArray($this->edge1Array); $vertexHandler = new VertexHandler($this->connection); // Save vertices using VertexHandler $result1 = $vertexHandler->save($this->vertexCollectionName, $vertex1); $this->assertTrue($result1 == 'vertex1', 'Did not return vertex1!'); $result2 = $vertexHandler->save($this->vertexCollectionName, $vertex2); $this->assertTrue($result2 == 'vertex2', 'Did not return vertex2!'); // Get vertices using VertexHandler $result1 = $vertexHandler->getById($this->vertexCollectionName, $this->vertex1Name); $this->assertTrue($result1->getKey() == 'vertex1', 'Did not return vertex1!'); $result2 = $vertexHandler->getById($this->vertexCollectionName, $this->vertex2Name); $this->assertTrue($result2->getKey() == 'vertex2', 'Did not return vertex2!'); // Save edge using EdgeHandler $edgeHandler = new EdgeHandler($this->connection); $result1 = $edgeHandler->saveEdge($this->edgeCollectionName, $this->vertexCollectionName . '/' . $this->vertex1Name, $this->vertexCollectionName . '/' . $this->vertex2Name, $edge1); $this->assertTrue($result1 == 'edge1', 'Did not return edge1!'); // Get edge using EdgeHandler $result1 = $edgeHandler->getById($this->edgeCollectionName, $this->edge1Name); $this->assertTrue($result1->getKey() == 'edge1', 'Did not return edge1!'); // Remove one vertex using VertexHandler $result1a = $vertexHandler->removeById($this->vertexCollectionName, $this->vertex1Name); $this->assertTrue($result1a, 'Did not return true!'); // Try to get vertex using VertexHandler // This should cause an exception with a code of 404 $e = null; try { $vertexHandler->getById($this->vertexCollectionName, $this->vertex1Name); } catch (\Exception $e) { // don't bother us... just give us the $e } $this->assertInstanceOf('triagens\\ArangoDb\\ServerException', $e); // Try to get the edge using EdgeHandler // This should cause an exception with a code of 404, because connecting edges should be removed when a vertex is removed $e = null; try { $edgeHandler->getById($this->edgeCollectionName, $this->edge1Name); } catch (\Exception $e) { $this->assertInstanceOf('triagens\\ArangoDb\\ServerException', $e); $this->assertTrue($e->getCode() == 404, 'Should be 404, instead got: ' . $e->getCode()); } // Try to remove the edge using EdgeHandler // This should not cause an exception with a code of 404, because the we removed the vertex through the VertexHandler, not the GraphHandler try { $result = $edgeHandler->removeById($this->edgeCollectionName, $this->edge1Name); } catch (\Exception $e) { $result = $e; } $this->assertTrue($result, 'Should be true, instead got: ' . $result); // Try to remove the edge using VertexHandler again // This should not cause an exception with code 404 because we just had removed this edge $e = null; try { $edgeHandler->removeById($this->edgeCollectionName, $this->edge1Name); } catch (\Exception $e) { $this->assertInstanceOf('triagens\\ArangoDb\\ServerException', $e); $this->assertTrue($e->getCode() == 404, 'Should be 404, instead got: ' . $e->getCode()); } // Remove the other vertex using VertexHandler $result2 = $vertexHandler->removeById($this->vertexCollectionName, $this->vertex2Name); $this->assertTrue($result2, 'Did not return true!'); // Try to get vertex using VertexHandler // This should cause an exception with a code of 404 $e = null; try { $vertexHandler->getById($this->vertexCollectionName, $this->vertex2Name); } catch (\Exception $e) { $this->assertInstanceOf('triagens\\ArangoDb\\ServerException', $e); $this->assertTrue($e->getCode() == 404, 'Should be 404, instead got: ' . $e->getCode()); } }
} $nv = get_next_int64($fp); $ne = get_next_int64($fp); $off = get_int64_array($fp, $nv + 1); $ind = get_int64_array($fp, $ne); $wgt = get_int64_array($fp, $ne); fclose($fp); print "Inserting into DB...\n"; //$batch = new Batch($connection); //$batch->activate(); $vertices = array_fill(0, $nv, "-1"); $time_start = microtime(true); for ($v = 0; $v < $nv; $v++) { $s = $vertices[$v]; if ($s == "-1") { $s = new_vertex($documents, $u, $nv); $vertices[$v] = $s; } for ($i = $off[$v]; $i < $off[$v + 1]; $i++) { $u = $ind[$i]; $d = $vertices[$u]; if ($d == "-1") { $d = new_vertex($documents, $u, $nv); $vertices[$u] = $d; } $edg = new Edge(); $edg->set("weight", $wgt[$i]); $edges->saveEdge($edge, $s, $d, $edg, true); } } //$batch->process();
/** * test for replacing a edge using replace() */ public function testReplaceEdge() { $connection = $this->connection; $edgeHandler = new EdgeHandler($connection); $edgeCollection = $this->edgeCollection; $document1 = new Document(); $document2 = new Document(); $documentHandler = new DocumentHandler($connection); $edgeDocument = new Edge(); $document1->someAttribute = 'someValue1'; $document2->someAttribute = 'someValue2'; $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document1); $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document2); $documentHandle1 = $document1->getHandle(); $documentHandle2 = $document2->getHandle(); $edgeDocument->set('label', null); $edgeDocument->set('labelt', "as"); $edgeId = $edgeHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, $edgeDocument); $this->assertTrue(is_numeric($edgeId), 'Did not return an id!'); $edgePutDocument = new Edge(); $edgePutDocument->set('_id', $edgeDocument->getHandle()); $edgePutDocument->set('_rev', $edgeDocument->getRevision()); $edgePutDocument->set('labels', "as"); $result = $edgeHandler->replace($edgePutDocument); $this->assertTrue($result); $resultingEdge = $edgeHandler->get($edgeCollection->getId(), $edgeId); $this->assertObjectHasAttribute('_id', $resultingEdge, '_id field should exist, empty or with an id'); $this->assertTrue($resultingEdge->label == null, 'Should be :null, is: ' . $resultingEdge->label); $this->assertTrue($resultingEdge->labelt == null, 'Should be :null, is: ' . $resultingEdge->labelt); $this->assertTrue($resultingEdge->labels == "as"); $response = $edgeHandler->delete($resultingEdge); $this->assertTrue($response, 'Delete should return true!'); }
/** * Test export as Edge object */ public function testExportEdgeObjects() { if (!$this->hasExportApi) { return; } try { $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestEdge'); } catch (\Exception $e) { } $edgeCollection = new Collection(); $edgeCollection->setName('ArangoDB_PHP_TestSuite_TestEdge'); $edgeCollection->setType(Collection::TYPE_EDGE); $this->collectionHandler->add($edgeCollection); $edgeHandler = new EdgeHandler($this->connection); $vertexCollection = $this->collection->getName(); for ($i = 0; $i < 100; ++$i) { $edgeHandler->saveEdge($edgeCollection, $vertexCollection . "/1", $vertexCollection . "/2", array("value" => $i)); } $export = new Export($this->connection, $edgeCollection, array("_flat" => false)); $cursor = $export->execute(); $this->assertEquals(1, $cursor->getFetches()); $this->assertNull($cursor->getId()); $this->assertEquals(100, $cursor->getCount()); $this->assertEquals(1, $cursor->getFetches()); $all = $cursor->getNextBatch(); $this->assertEquals(100, count($all)); foreach ($all as $doc) { $this->assertTrue($doc instanceof Document); $this->assertTrue($doc instanceof Edge); } $this->assertFalse($cursor->getNextBatch()); $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestEdge'); }
public function testCreateMixedBatchWithPartIds() { $edgeCollection = $this->edgeCollection; $batch = new Batch($this->connection); $this->assertInstanceOf('\\triagens\\ArangoDb\\Batch', $batch); // Create collection $connection = $this->connection; $collection = new Collection(); $collectionHandler = new CollectionHandler($connection); $name = 'ArangoDB_PHP_TestSuite_TestCollection_02'; $collection->setName($name); $batch->nextBatchPartId('testCollection1'); $response = $collectionHandler->add($collection); $this->assertTrue(is_numeric($response), 'Did not return a fake numeric id!'); $batch->process(); $resultingCollectionId = $batch->getProcessedPartResponse('testCollection1'); $testCollection1Part = $batch->getPart('testCollection1'); $this->assertTrue($testCollection1Part->getHttpCode() == 200, 'Did not return an HttpCode 200!'); $resultingCollection = $collectionHandler->get($batch->getProcessedPartResponse('testCollection1')); $resultingAttribute = $resultingCollection->getName(); $this->assertTrue($name === $resultingAttribute, 'The created collection name and resulting collection name do not match!'); $this->assertEquals(Collection::getDefaultType(), $resultingCollection->getType()); $batch = new Batch($this->connection); // Create documents $documentHandler = $this->documentHandler; $batch->nextBatchPartId('doc1'); $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')); $documentId = $documentHandler->add($resultingCollectionId, $document); $this->assertTrue(is_numeric($documentId), 'Did not return a fake numeric id!'); for ($i = 0; $i <= 10; ++$i) { $document = Document::createFromArray(array('someAttribute' => 'someValue' . $i, 'someOtherAttribute' => 'someOtherValue2' . $i)); $documentId = $documentHandler->add($resultingCollectionId, $document); } $this->assertTrue(is_numeric($documentId), 'Did not return a fake numeric id!'); $batch->process(); // try getting processed response through batchpart $testDocument1PartResponse = $batch->getPart('doc1')->getProcessedResponse(); // try getting it from batch $testDocument2PartResponse = $batch->getProcessedPartResponse(1); $batch = new Batch($this->connection); $docId1 = explode('/', $testDocument1PartResponse); $docId2 = explode('/', $testDocument2PartResponse); $documentHandler->getById($resultingCollectionId, $docId1[1]); $documentHandler->getById($resultingCollectionId, $docId2[1]); $batch->process(); $document1 = $batch->getProcessedPartResponse(0); $document2 = $batch->getProcessedPartResponse(1); $batch = new Batch($this->connection); // test edge creation $edgeDocument = new Edge(); $edgeDocumentHandler = new EdgeHandler($connection); $edgeDocument->set('label', 'knows'); $edgeDocumentHandler->saveEdge($edgeCollection->getName(), $document1->getHandle(), $document2->getHandle(), $edgeDocument); $batch->process(); $edge = $batch->getProcessedPartResponse(0); $this->assertFalse(is_a($edge, 'triagens\\ArangoDb\\HttpResponse'), 'Edge batch creation did return an error: ' . print_r($edge, true)); $this->assertTrue($edge == !'', 'Edge batch creation did return empty string: ' . print_r($edge, true)); $batch = new Batch($this->connection); $document = new Document(); $documentHandler = new DocumentHandler($connection); $document->someAttribute = 'someValue'; $documentHandler->add($resultingCollection->getId(), $document); // set the next batchpart id $batch->nextBatchPartId('myBatchPart'); // set cursor options for the next batchpart $batch->nextBatchPartCursorOptions(array("sanitize" => true)); // set batchsize to 10, so we can test if an additional http request is done when we getAll() a bit later $statement = new Statement($connection, array("query" => '', "count" => true, "batchSize" => 10, "sanitize" => true)); $statement->setQuery('FOR a IN `ArangoDB_PHP_TestSuite_TestCollection_02` RETURN a'); $statement->execute(); $documentHandler->removeById($resultingCollectionId, $docId1[1]); $documentHandler->removeById($resultingCollectionId, $docId2[1]); $batch->nextBatchPartId('docsAfterRemoval'); $collectionHandler->getAllIds($resultingCollectionId); $batch->process(); $stmtCursor = $batch->getProcessedPartResponse('myBatchPart'); $this->assertTrue(count($stmtCursor->getAll()) == 13, 'At the time of statement execution there should be 13 documents found! Found: ' . count($stmtCursor->getAll())); // This fails but we'll just make a note because such a query is not needed to be batched. // $docsAfterRemoval=$batch->getProcessedPartResponse('docsAfterRemoval'); // $this->assertTrue(count($docsAfterRemoval) == 1, 'At the time of statement execution there should be 3 documents found! Found: '.count($stmtCursor->getAll())); // Get previously created collection and delete it, from inside a batch $batch = new Batch($this->connection); $collectionHandler->delete($resultingCollectionId); $batch->process(); }
} catch (\Exception $e) { // collection may already exist - ignore this error for now } // create a new department $marketing = Document::createFromArray(array("name" => "Marketing")); $documentHandler->save("departments", $marketing); // create another department $finance = Document::createFromArray(array("name" => "Finance")); $documentHandler->save("departments", $finance); // create a new employee $john = Document::createFromArray(array("name" => "John")); $documentHandler->save("employees", $john); // create another employee $jane = Document::createFromArray(array("name" => "Jane")); $documentHandler->save("employees", $jane); // now insert a link between Marketing and Jane $worksFor = Edge::createFromArray(array("startDate" => "2009-06-23", "endDate" => "2014-11-12")); $edgeHandler->saveEdge("worksFor", $marketing->getHandle(), $jane->getHandle(), $worksFor); // now insert a link between Finance and Jane $worksFor = Edge::createFromArray(array("startDate" => "2014-11-12")); $edgeHandler->saveEdge("worksFor", $finance->getHandle(), $jane->getHandle(), $worksFor); // now insert a link between Finance and John $worksFor = Edge::createFromArray(array("startDate" => "2012-04-01")); $edgeHandler->saveEdge("worksFor", $finance->getHandle(), $john->getHandle(), $worksFor); } catch (ConnectException $e) { print $e . PHP_EOL; } catch (ServerException $e) { print $e . PHP_EOL; } catch (ClientException $e) { print $e . PHP_EOL; }
/** * Test outEdges method */ public function testEdgesOut() { $connection = $this->connection; $edgeCollection = $this->edgeCollection; $document1 = new Document(); $document2 = new Document(); $documentHandler = new DocumentHandler($connection); $edgeDocumentHandler = new EdgeHandler($connection); $document1->someAttribute = 'someValue1'; $document2->someAttribute = 'someValue2'; $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document1); $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document2); $documentHandle1 = $document1->getHandle(); $documentHandle2 = $document2->getHandle(); $edgeDocument1 = $edgeDocumentHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, array('value' => 1)); $edgeDocument2 = $edgeDocumentHandler->saveEdge($edgeCollection->getName(), $documentHandle2, $documentHandle1, array('value' => 2)); $edgeDocument3 = $edgeDocumentHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, array('value' => 3)); $edgesQueryResult = $edgeDocumentHandler->outEdges($edgeCollection->getName(), $documentHandle1); $this->assertEquals(2, count($edgesQueryResult)); foreach ($edgesQueryResult as $edge) { $this->assertInstanceOf('triagens\\ArangoDb\\Edge', $edge); if ($edge->value === 1) { $this->assertEquals($documentHandle1, $edge->getFrom()); $this->assertEquals($documentHandle2, $edge->getTo()); $this->assertEquals($edgeDocument1, $edge->getId()); } else { $this->assertEquals($documentHandle1, $edge->getFrom()); $this->assertEquals($documentHandle2, $edge->getTo()); $this->assertEquals($edgeDocument3, $edge->getId()); } } // test empty result $edgesQueryResult = $edgeDocumentHandler->outEdges($edgeCollection->getName(), "ArangoDBPHPTestSuiteTestCollection01/foobar"); $this->assertEquals(0, count($edgesQueryResult)); }
/** * Try to create, head and delete a edge */ public function testCreateHeadAndDeleteEdgeWithRevision() { $connection = $this->connection; $edgeHandler = new EdgeHandler($connection); $edgeCollection = $this->edgeCollection; $document1 = new Document(); $document2 = new Document(); $documentHandler = new DocumentHandler($connection); $edgeDocument = new Edge(); $document1->someAttribute = 'someValue1'; $document2->someAttribute = 'someValue2'; $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document1); $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document2); $documentHandle1 = $document1->getHandle(); $documentHandle2 = $document2->getHandle(); $edgeDocument->set('label', 'knows'); $edgeId = $edgeHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, $edgeDocument); try { $edgeHandler->getHead($edgeCollection->getId(), $edgeId, "12345", true); } catch (\Exception $e412) { } $this->assertEquals($e412->getCode(), 412); try { $edgeHandler->getHead($edgeCollection->getId(), "notExisting"); } catch (\Exception $e404) { } $this->assertEquals($e404->getCode(), 404); $result304 = $edgeHandler->getHead($edgeCollection->getId(), $edgeId, $edgeDocument->getRevision(), false); $this->assertEquals($result304["etag"], '"' . $edgeDocument->getRevision() . '"'); $this->assertEquals($result304["content-length"], 0); $this->assertEquals($result304["httpCode"], 304); $result200 = $edgeHandler->getHead($edgeCollection->getId(), $edgeId, $edgeDocument->getRevision(), true); $this->assertEquals($result200["etag"], '"' . $edgeDocument->getRevision() . '"'); $this->assertNotEquals($result200["content-length"], 0); $this->assertEquals($result200["httpCode"], 200); $documentHandler->delete($document1); $documentHandler->delete($document2); $edgeHandler->deleteById($edgeCollection->getName(), $edgeId); }