forked from mongodb/mongo-php-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor-iterator_handlers-001.phpt
90 lines (71 loc) · 2 KB
/
cursor-iterator_handlers-001.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--TEST--
MongoDB\Driver\Cursor iterator handlers
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
class MyIteratorIterator extends IteratorIterator
{
private $name;
public function __construct(Traversable $iterator, $name)
{
parent::__construct($iterator);
$this->name = (string) $name;
}
public function dump()
{
$key = parent::key();
$current = parent::current();
$position = is_int($key) ? (string) $key : 'null';
$document = is_object($current) ? sprintf("{_id: %d}", $current->_id) : 'null';
printf("%s: %s => %s\n", $this->name, $position, $document);
}
}
$manager = create_test_manager();
$bulkWrite = new MongoDB\Driver\BulkWrite;
for ($i = 0; $i < 5; $i++) {
$bulkWrite->insert(array('_id' => $i));
}
$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);
printf("Inserted: %d\n", $writeResult->getInsertedCount());
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
$a = new MyIteratorIterator($cursor, 'A');
echo "\nBefore rewinding, position and current element are not populated:\n";
$a->dump();
echo "\nAfter rewinding, current element is populated:\n";
$a->rewind();
$a->dump();
echo "\nAfter advancing, next element is populated:\n";
$a->next();
$a->dump();
echo "\nAdvancing through remaining elements:\n";
$a->next();
$a->dump();
$a->next();
$a->dump();
$a->next();
$a->dump();
echo "\nAdvancing beyond the last element:\n";
$a->next();
$a->dump();
?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Inserted: 5
Before rewinding, position and current element are not populated:
A: null => null
After rewinding, current element is populated:
A: 0 => {_id: 0}
After advancing, next element is populated:
A: 1 => {_id: 1}
Advancing through remaining elements:
A: 2 => {_id: 2}
A: 3 => {_id: 3}
A: 4 => {_id: 4}
Advancing beyond the last element:
A: null => null
===DONE===