66
77use PHPUnit \Framework \TestCase ;
88use ArangoDb \Connection ;
9- use ArangoDb \Vpack ;
10- use ArangoDb \Cursor ;
119
1210/**
1311 * @group connection
1412 */
1513class ConnectionTest extends TestCase
1614{
17-
1815 /**
1916 * @var Connection
2017 */
@@ -133,11 +130,11 @@ public function it_sends_large_request_via_post()
133130
134131 $ this ->connection = TestUtil::getConnection ();
135132
136- $ response = $ this ->connection ->post (
133+ $ this ->connection ->post (
137134 '/_api/collection ' ,
138135 TestUtil::getVpackCreateCollection ($ collection )
139136 );
140- $ response = $ this ->connection ->post (
137+ $ this ->connection ->post (
141138 '/_api/collection ' ,
142139 TestUtil::getVpackCreateCollection ('c6f955fd5efbc2cedbb5f97cfd8890bb98b364c1d ' )
143140 );
@@ -158,235 +155,14 @@ public function it_sends_large_request_via_post()
158155 $ this ->assertTrue (TestUtil::wasSuccessful ($ response ), var_export ($ body , true ));
159156 }
160157
161- /**
162- * @test
163- */
164- public function it_sends_query ()
165- {
166- $ this ->connection = TestUtil::getConnection ();
167-
168- $ cursor = $ this ->connection ->query (Vpack::fromArray ([
169- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
170- 'batchSize ' => 10
171- ]));
172-
173- $ cursor ->rewind ();
174- $ iterations = 0 ;
175-
176- while ($ cursor ->valid ()) {
177- $ data = $ cursor ->current ();
178- $ this ->assertArrayHasKey (0 , json_decode ($ data ));
179- $ iterations ++;
180- $ cursor ->next ();
181- }
182-
183- $ this ->assertSame (100 , $ iterations );
184- }
185-
186- /**
187- * @test
188- */
189- public function it_sends_query_with_bind_params ()
190- {
191- $ collection = 'event_streams ' ;
192- $ this ->connection = TestUtil::getConnection ();
193-
194- $ response = $ this ->connection ->post (
195- '/_api/collection ' ,
196- TestUtil::getVpackCreateCollection ($ collection )
197- );
198-
199- $ body = json_decode ($ response ->getBody (), true );
200-
201- $ this ->assertNotNull ($ body );
202- $ this ->assertTrue (TestUtil::wasSuccessful ($ response ), var_export ($ body , true ));
203-
204- $ response = $ this ->connection ->post (
205- '/_api/document/ ' . $ collection ,
206- Vpack::fromJson ('[{"Hello":"Earth"}, {"Hello":"Venus"}, {"Hello":"Mars"}] ' )
207- );
208-
209- $ body = json_decode ($ response ->getBody (), true );
210-
211- $ this ->assertNotNull ($ body );
212- $ this ->assertTrue (TestUtil::wasSuccessful ($ response ), var_export ($ body , true ));
213-
214- $ cursor = $ this ->connection ->query (Vpack::fromArray ([
215- 'query ' => 'FOR c IN @@collection RETURN c ' ,
216- 'bindVars ' => ['@collection ' => $ collection ]
217- ]));
218-
219- $ cursor ->rewind ();
220- $ iterations = 0 ;
221-
222- $ this ->assertSame (3 , $ cursor ->count ());
223- $ this ->assertSame (3 , count ($ cursor ));
224- $ this ->assertTrue ($ cursor ->valid ());
225-
226- for ($ i = 0 ; $ i < $ cursor ->count (); $ i ++) {
227- $ data = $ cursor ->current ();
228- $ this ->assertArrayHasKey ('_id ' , json_decode ($ data , true ));
229- $ iterations ++;
230- $ cursor ->next ();
231- }
232-
233- $ this ->assertSame (3 , $ iterations );
234- }
235-
236- /**
237- * @test
238- */
239- public function it_sends_query_and_returns_result_as_array ()
240- {
241- $ this ->connection = TestUtil::getConnection ();
242-
243- $ cursor = $ this ->connection ->query (
244- Vpack::fromArray ([
245- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
246- 'batchSize ' => 10
247- ]), [
248- Cursor::ENTRY_TYPE => Cursor::ENTRY_TYPE_ARRAY
249- ]
250- );
251-
252- $ cursor ->rewind ();
253- $ iterations = 0 ;
254-
255- while ($ cursor ->valid ()) {
256- $ data = $ cursor ->current ();
257- $ this ->assertArrayHasKey (0 , $ data );
258- $ iterations ++;
259- $ cursor ->next ();
260- }
261-
262- $ this ->assertSame (100 , $ iterations );
263- }
264-
265- /**
266- * @test
267- */
268- public function it_creates_array_from_cursor ()
269- {
270- $ this ->connection = TestUtil::getConnection ();
271-
272- $ cursor = $ this ->connection ->query (
273- Vpack::fromArray ([
274- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
275- 'batchSize ' => 10
276- ]), [
277- Cursor::ENTRY_TYPE => Cursor::ENTRY_TYPE_ARRAY
278- ]
279- );
280-
281- $ data = $ cursor ->toArray ();
282-
283- $ this ->assertCount (100 , $ data );
284- }
285-
286-
287- /**
288- * @test
289- */
290- public function it_rewinds_cursor_multiple_times ()
291- {
292- $ this ->connection = TestUtil::getConnection ();
293-
294- $ cursor = $ this ->connection ->query (Vpack::fromArray ([
295- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
296- 'batchSize ' => 10
297- ]));
298-
299- $ cursor ->rewind ();
300- $ iterations = 0 ;
301- $ dataSet1 = [];
302- $ dataSet2 = [];
303-
304- while ($ cursor ->valid ()) {
305- $ dataSet1 [] = $ cursor ->current ();
306- $ cursor ->next ();
307- $ iterations ++;
308- }
309-
310- $ cursor ->rewind ();
311-
312- while ($ cursor ->valid ()) {
313- $ dataSet2 [] = $ cursor ->current ();
314- $ cursor ->next ();
315- $ iterations ++;
316- }
317-
318- $ this ->assertSame (200 , $ iterations );
319- $ this ->assertTrue ($ dataSet1 === $ dataSet2 );
320- }
321-
322-
323- /**
324- * @test
325- */
326- public function it_gets_raw_response_from_cursor ()
327- {
328- $ this ->connection = TestUtil::getConnection ();
329-
330- $ cursor = $ this ->connection ->query (Vpack::fromArray ([
331- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
332- 'batchSize ' => 10
333- ]));
334-
335- $ cursor ->rewind ();
336- $ response = $ cursor ->getResponse ();
337-
338- $ this ->assertInstanceOf (\ArangoDb \Response::class, $ response );
339- $ this ->assertTrue (TestUtil::wasSuccessful ($ response ));
340- }
341-
342-
343- /**
344- * @test
345- */
346- public function it_rewinds_cursor_half_way_through ()
347- {
348- $ this ->connection = TestUtil::getConnection ();
349-
350- $ cursor = $ this ->connection ->query (Vpack::fromArray ([
351- 'query ' => 'FOR i IN 1..100 RETURN [i, i+1] ' ,
352- 'batchSize ' => 10
353- ]));
354-
355- $ cursor ->rewind ();
356- $ iterations = 0 ;
357- $ dataSet1 = [];
358- $ dataSet2 = [];
359-
360- for ($ i = 0 ; $ i < 50 ; $ i ++) {
361- $ cursor ->valid ();
362- $ dataSet1 [] = $ cursor ->current ();
363- $ cursor ->next ();
364- $ iterations ++;
365- }
366-
367- $ cursor ->rewind ();
368-
369- while ($ cursor ->valid ()) {
370- $ dataSet2 [] = $ cursor ->current ();
371- $ cursor ->next ();
372- $ iterations ++;
373- }
374-
375- $ this ->assertSame (150 , $ iterations );
376- $ this ->assertCount (50 , $ dataSet1 );
377- $ this ->assertCount (100 , $ dataSet2 );
378- $ this ->assertTrue ($ dataSet1 === array_slice ($ dataSet2 , 0 , 50 ));
379- }
380-
381-
382158 /**
383159 * @test
384160 */
385161 public function it_throws_request_failed_exception_with_response_data ()
386162 {
387163 $ this ->connection = TestUtil::getConnection ();
388164
389- $ res = $ this ->connection ->post (
165+ $ this ->connection ->post (
390166 '/_api/collection/ ' ,
391167 TestUtil::getVpackCreateCollection ('request_failed_exception_test ' )
392168 );
0 commit comments