Skip to content

Commit b8b0d7a

Browse files
authored
Merge pull request #1 from hwde/DocumentClass
wrote some tests for custom document class, added documentClass to …
2 parents a2d449a + b0262d6 commit b8b0d7a

File tree

6 files changed

+242
-16
lines changed

6 files changed

+242
-16
lines changed

lib/ArangoDBClient/Batch.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,10 @@ public function append($method, $request)
361361
$result .= '{"error":false,"_id":"0/0","id":"0","_rev":0,"hasMore":1, "result":[{}], "documents":[{}]}' . $eol . $eol;
362362

363363
$response = new HttpResponse($result);
364-
$batchPart = new BatchPart($this, $batchPartId, $type, $request, $response, ['cursorOptions' => $this->_batchPartCursorOptions]);
364+
$batchPart = new BatchPart($this, $batchPartId, $type, $request, $response, [
365+
'cursorOptions' => $this->_batchPartCursorOptions,
366+
'_documentClass' => $this->_documentClass,
367+
]);
365368

366369
$this->_batchParts[$batchPartId] = $batchPart;
367370

@@ -556,6 +559,21 @@ public function getConnection()
556559
{
557560
return $this->_connection;
558561
}
562+
563+
/**
564+
* @var string Document class to use
565+
*/
566+
protected $_documentClass = '\ArangoDBClient\Document';
567+
568+
/**
569+
* Sets the document class to use
570+
*
571+
* @param string $class Document class to use
572+
*/
573+
public function setDocumentClass($class)
574+
{
575+
$this->_documentClass = $class;
576+
}
559577
}
560578

561579
class_alias(Batch::class, '\triagens\ArangoDb\Batch');

lib/ArangoDBClient/BatchPart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public function __construct($batch, $id, $type, $request, $response, $options)
8989
{
9090
$sanitize = false;
9191
$options = array_merge($options, $this->getCursorOptions());
92+
93+
if (isset($options['_documentClass'])) {
94+
$this->setDocumentClass($options['_documentClass']);
95+
}
96+
9297
extract($options, EXTR_IF_EXISTS);
9398
$this->setBatch($batch);
9499
$this->setId($id);

lib/ArangoDBClient/Export.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,27 @@ private function getCursorOptions()
258258
$result = [
259259
ExportCursor::ENTRY_FLAT => (bool) $this->_flat,
260260
ExportCursor::ENTRY_BASEURL => Urls::URL_EXPORT,
261-
ExportCursor::ENTRY_TYPE => $this->_type
261+
ExportCursor::ENTRY_TYPE => $this->_type,
262+
'_documentClass' => $this->_documentClass,
262263
];
263264

264265
return $result;
265266
}
267+
268+
/**
269+
* @var string Document class to use
270+
*/
271+
protected $_documentClass = '\ArangoDBClient\Document';
272+
273+
/**
274+
* Sets the document class to use
275+
*
276+
* @param string $class Document class to use
277+
*/
278+
public function setDocumentClass($class)
279+
{
280+
$this->_documentClass = $class;
281+
}
266282
}
267283

268284
class_alias(Export::class, '\triagens\ArangoDb\Export');

lib/ArangoDBClient/ExportCursor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public function __construct(Connection $connection, array $data, array $options)
117117
if (isset($data[self::ENTRY_ID])) {
118118
$this->_id = $data[self::ENTRY_ID];
119119
}
120+
121+
if (isset($options['_documentClass'])) {
122+
$this->setDocumentClass($options['_documentClass']);
123+
}
120124

121125
// attribute must be there
122126
assert(isset($data[self::ENTRY_HASMORE]));

tests/CustomDocumentClassTest.php

Lines changed: 196 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,207 @@ class CustomDocumentClassTest extends
2121
\PHPUnit_Framework_TestCase
2222
{
2323

24+
protected static $testsTimestamp;
25+
26+
public function __construct($name = null, array $data = [], $dataName = '')
27+
{
28+
parent::__construct($name, $data, $dataName);
29+
static::$testsTimestamp = str_replace('.', '_', (string) microtime(true));
30+
}
31+
32+
2433
public function setUp()
2534
{
26-
$this->connection = getConnection();
35+
$this->connection = getConnection();
36+
$this->collectionHandler = new CollectionHandler($this->connection);
2737

28-
// remove existing databases to make test repeatable
29-
$database = 'ArangoTestSuiteDatabaseTest03';
3038
try {
31-
Database::delete($this->connection, $database);
32-
} catch (Exception $e) {
39+
$this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestCollection_01');
40+
} catch (\Exception $e) {
41+
// don't bother us, if it's already deleted.
42+
}
43+
44+
$this->collection = new Collection();
45+
$this->collection->setName('ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp);
46+
$this->collectionHandler->create($this->collection);
47+
}
48+
49+
50+
/**
51+
* Try to retrieve a document with custom document class
52+
*/
53+
public function testGetCustomDocumentWithHandler()
54+
{
55+
$connection = $this->connection;
56+
$collection = $this->collection;
57+
$document = new Document();
58+
$documentHandler = new DocumentHandler($connection);
59+
60+
$document->someAttribute = 'someValue';
61+
62+
$documentId = $documentHandler->save($collection->getId(), $document);
63+
64+
$documentHandler->setDocumentClass(CustomDocumentClass1::class);
65+
$resultingDocument1 = $documentHandler->get($collection->getId(), $documentId);
66+
static::assertInstanceOf(CustomDocumentClass1::class, $resultingDocument1, 'Retrieved document isn\'t made with provided CustomDocumentClass1!');
67+
68+
$documentHandler->setDocumentClass(CustomDocumentClass2::class);
69+
$resultingDocument2 = $documentHandler->get($collection->getId(), $documentId);
70+
static::assertInstanceOf(CustomDocumentClass2::class, $resultingDocument2, 'Retrieved document isn\'t made with provided CustomDocumentClass2!');
71+
72+
$documentHandler->setDocumentClass(Document::class);
73+
$resultingDocument = $documentHandler->get($collection->getId(), $documentId);
74+
static::assertInstanceOf(Document::class, $resultingDocument, 'Retrieved document isn\'t made with provided Document!');
75+
static::assertNotInstanceOf(CustomDocumentClass1::class, $resultingDocument, 'Retrieved document is made with CustomDocumentClass1!');
76+
static::assertNotInstanceOf(CustomDocumentClass2::class, $resultingDocument, 'Retrieved document is made with CustomDocumentClass2!');
77+
78+
$resultingAttribute = $resultingDocument->someAttribute;
79+
static::assertSame(
80+
'someValue', $resultingAttribute, 'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute
81+
);
82+
83+
$resultingAttribute1 = $resultingDocument1->someAttribute;
84+
static::assertSame(
85+
'someValue', $resultingAttribute1, 'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute
86+
);
87+
88+
$resultingAttribute2 = $resultingDocument2->someAttribute;
89+
static::assertSame(
90+
'someValue', $resultingAttribute2, 'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute
91+
);
92+
93+
$documentHandler->remove($document);
94+
}
95+
96+
/**
97+
* Try to retrieve a custom document class via Statement.
98+
*/
99+
public function testGetCustomDocumentWithStatement()
100+
{
101+
$connection = $this->connection;
102+
$collection = $this->collection;
103+
$document = new Document();
104+
$documentHandler = new DocumentHandler($connection);
105+
106+
$document->someAttribute = 'anotherValue';
107+
108+
$documentHandler->save($collection->getId(), $document);
109+
110+
$statement = new Statement(
111+
$connection, [
112+
'query' => '',
113+
'count' => true,
114+
'batchSize' => 1000,
115+
'_sanitize' => true,
116+
]
117+
);
118+
$statement->setDocumentClass(CustomDocumentClass1::class);
119+
$statement->setQuery(sprintf('FOR a IN `%s` RETURN a', $collection->getName()));
120+
$cursor = $statement->execute();
121+
122+
$result = $cursor->current();
123+
124+
static::assertInstanceOf(CustomDocumentClass1::class, $result, 'Retrieved document isn\'t made with provided CustomDocumentClass1!');
125+
static::assertSame(
126+
'anotherValue', $result->someAttribute, 'Expected value anotherValue, found :' . $result->someAttribute
127+
);
128+
129+
$documentHandler->remove($document);
130+
}
131+
132+
/**
133+
* Try to retrieve a custom document class via Export.
134+
*/
135+
public function testGetCustomDocumentWithExport()
136+
{
137+
$connection = $this->connection;
138+
$collection = $this->collection;
139+
$document = new Document();
140+
$documentHandler = new DocumentHandler($connection);
141+
142+
$document->someAttribute = 'exportValue';
143+
144+
$documentHandler->save($collection->getId(), $document);
145+
146+
$export = new Export($connection, $collection->getName(), [
147+
'batchSize' => 5000,
148+
'_flat' => false,
149+
'flush' => true,
150+
]);
151+
152+
// execute the export. this will return a special, forward-only cursor
153+
$export->setDocumentClass(CustomDocumentClass1::class);
154+
$cursor = $export->execute();
155+
156+
$found = false;
157+
while ($docs = $cursor->getNextBatch()) {
158+
$found = true;
159+
static::assertTrue(count($docs) > 0, 'No documents retrieved!');
160+
foreach($docs as $doc) {
161+
static::assertInstanceOf(CustomDocumentClass1::class, $doc, 'Retrieved document isn\'t made with provided CustomDocumentClass1!');
162+
static::assertSame(
163+
'exportValue', $doc->someAttribute, 'Expected value exportValue, found :' . $doc->someAttribute
164+
);
165+
}
33166
}
167+
168+
static::assertTrue($found, 'No batch results in Export');
34169

35-
Database::create($this->connection, $database);
170+
$documentHandler->remove($document);
171+
}
172+
173+
public function testGetCustomDocumentWithBatch()
174+
{
175+
$connection = $this->connection;
176+
$collection = $this->collection;
177+
$documentHandler = new DocumentHandler($connection);
178+
$document1 = Document::createFromArray(
179+
['someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue']
180+
);
181+
$docId1 = $documentHandler->save($this->collection->getId(), $document1);
182+
$document2 = Document::createFromArray(
183+
['someAttribute' => 'someValue2', 'someOtherAttribute' => 'someOtherValue2']
184+
);
185+
$docId2 = $documentHandler->save($this->collection->getId(), $document2);
36186

37-
$this->collectionHandler = new CustomCollectionHandler($this->connection);
187+
$batch = new Batch($connection);
188+
$batch->setDocumentClass(CustomDocumentClass1::class);
189+
$batch->startCapture();
190+
191+
$documentHandler->getById($this->collection->getId(), $docId1);
192+
$documentHandler->getById($this->collection->getId(), $docId2);
193+
194+
$batch->process();
195+
$result = $batch->getPart(0)->getProcessedResponse();
196+
197+
static::assertInstanceOf(CustomDocumentClass1::class, $result, 'Retrieved document isn\'t made with provided CustomDocumentClass1!');
198+
static::assertSame(
199+
'someValue', $result->someAttribute, 'Expected value someValue, found :' . $result->someAttribute
200+
);
201+
202+
$batchPart = $batch->getPart(1);
203+
$batchPart->setDocumentClass(CustomDocumentClass2::class);
204+
$result = $batchPart->getProcessedResponse();
205+
206+
static::assertInstanceOf(CustomDocumentClass2::class, $result, 'Retrieved document isn\'t made with provided CustomDocumentClass2!');
207+
static::assertSame(
208+
'someValue2', $result->someAttribute, 'Expected value someValue2, found :' . $result->someAttribute
209+
);
210+
211+
$documentHandler->remove($document1);
212+
$documentHandler->remove($document2);
38213
}
39214

215+
40216
public function tearDown()
41217
{
42-
// clean up
43-
$database = ['ArangoTestSuiteDatabaseTest03'];
44218
try {
45-
Database::delete($this->connection, $database);
46-
} catch (Exception $e) {
219+
$this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp);
220+
} catch (\Exception $e) {
221+
// don't bother us, if it's already deleted.
47222
}
48223

49-
unset($this->connection);
224+
unset($this->documentHandler, $this->document, $this->collectionHandler, $this->collection, $this->connection);
50225
}
51226

52227

@@ -58,4 +233,12 @@ public function tearDown()
58233
*/
59234
class CustomCollectionHandler extends CollectionHandler {
60235

61-
}
236+
}
237+
238+
/**
239+
* Class CustomDocumentClass1 & CustomDocumentClass2
240+
* @package ArangoDBClient
241+
*/
242+
class CustomDocumentClass1 extends Document {}
243+
class CustomDocumentClass2 extends Document {}
244+

tests/bootstrap-connection-close.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function getConnectionOptions()
5050
// use basic authorization
5151
ConnectionOptions::OPTION_AUTH_USER => 'root',
5252
// user for basic authorization
53-
ConnectionOptions::OPTION_AUTH_PASSWD => 'test',
53+
ConnectionOptions::OPTION_AUTH_PASSWD => '',
5454
// password for basic authorization
5555
ConnectionOptions::OPTION_TIMEOUT => 12,
5656
// timeout in seconds

0 commit comments

Comments
 (0)