@@ -21,32 +21,207 @@ class CustomDocumentClassTest extends
21
21
\PHPUnit_Framework_TestCase
22
22
{
23
23
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
+
24
33
public function setUp ()
25
34
{
26
- $ this ->connection = getConnection ();
35
+ $ this ->connection = getConnection ();
36
+ $ this ->collectionHandler = new CollectionHandler ($ this ->connection );
27
37
28
- // remove existing databases to make test repeatable
29
- $ database = 'ArangoTestSuiteDatabaseTest03 ' ;
30
38
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
+ }
33
166
}
167
+
168
+ static ::assertTrue ($ found , 'No batch results in Export ' );
34
169
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 );
36
186
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 );
38
213
}
39
214
215
+
40
216
public function tearDown ()
41
217
{
42
- // clean up
43
- $ database = ['ArangoTestSuiteDatabaseTest03 ' ];
44
218
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.
47
222
}
48
223
49
- unset($ this ->connection );
224
+ unset($ this ->documentHandler , $ this -> document , $ this -> collectionHandler , $ this -> collection , $ this -> connection );
50
225
}
51
226
52
227
@@ -58,4 +233,12 @@ public function tearDown()
58
233
*/
59
234
class CustomCollectionHandler extends CollectionHandler {
60
235
61
- }
236
+ }
237
+
238
+ /**
239
+ * Class CustomDocumentClass1 & CustomDocumentClass2
240
+ * @package ArangoDBClient
241
+ */
242
+ class CustomDocumentClass1 extends Document {}
243
+ class CustomDocumentClass2 extends Document {}
244
+
0 commit comments