|
| 1 | +--TEST-- |
| 2 | +MongoDB\Driver\Cursor query result iteration through NoRewindIterator |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> |
| 5 | +<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?> |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 9 | + |
| 10 | +$manager = new MongoDB\Driver\Manager(STANDALONE); |
| 11 | + |
| 12 | +$bulk = new MongoDB\Driver\BulkWrite(); |
| 13 | +$bulk->insert(array('_id' => 1, 'x' => 1)); |
| 14 | +$bulk->insert(array('_id' => 2, 'x' => 1)); |
| 15 | +$manager->executeBulkWrite(NS, $bulk); |
| 16 | + |
| 17 | +$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array("x" => 1))); |
| 18 | + |
| 19 | +/* IteratorIterator requires either rewind() or next() to be called at least |
| 20 | + * once to populate its current.data pointer, which valid() checks. Since next() |
| 21 | + * would skip the first element and NoRewindIterator::rewind() is a NOP, we must |
| 22 | + * explicitly call IteratorIterator::rewind() before composing it. */ |
| 23 | +$iteratorIterator = new IteratorIterator($cursor); |
| 24 | +$iteratorIterator->rewind(); |
| 25 | + |
| 26 | +$noRewindIterator = new NoRewindIterator($iteratorIterator); |
| 27 | + |
| 28 | +foreach ($noRewindIterator as $document) { |
| 29 | + var_dump($document); |
| 30 | +} |
| 31 | + |
| 32 | +/* NoRewindIterator::rewind() is a NOP, so attempting to iterate a second time |
| 33 | + * or calling rewind() directly accomplishes nothing. That said, it does avoid |
| 34 | + * the exception one would otherwise get invoking the rewind handler after |
| 35 | + * iteration has started. */ |
| 36 | +foreach ($noRewindIterator as $document) { |
| 37 | + var_dump($document); |
| 38 | +} |
| 39 | + |
| 40 | +$noRewindIterator->rewind(); |
| 41 | + |
| 42 | +?> |
| 43 | +===DONE=== |
| 44 | +<?php exit(0); ?> |
| 45 | +--EXPECTF-- |
| 46 | +object(stdClass)#%d (2) { |
| 47 | + ["_id"]=> |
| 48 | + int(1) |
| 49 | + ["x"]=> |
| 50 | + int(1) |
| 51 | +} |
| 52 | +object(stdClass)#%d (2) { |
| 53 | + ["_id"]=> |
| 54 | + int(2) |
| 55 | + ["x"]=> |
| 56 | + int(1) |
| 57 | +} |
| 58 | +===DONE=== |
0 commit comments