mirror of
https://github.com/macintoshplus/mongo-php-driver.git
synced 2026-04-24 00:48:02 +02:00
deead9687e
* Introduce MongoDB\Driver\ServerApi * Accept serverApi driver option * Introduce create_test_manager factory to create manager A centralised entry point is required to inject the API_VERSION env variable later. * Add build variant to test with requireApiVersion=true * Fix wrong configuration for auth variable This changed when migrating from our own scripts to drivers-evergreen-tools and was not updated properly, causing all tests to run with auth disabled. * Declare ZEND_PARSE_PARAMETERS_NONE macro This macro is missing on PHP < 7.3 * Remove duplicated API param storage * Add missing semicolons * Add ZEND_PARSE_PARAMETERS_NON_EX macro * Extract error handling functionality to separate macros * Throw if internal mongoc_server_api_t is already initialised * Use imported namespaces in tools file * Fix type info for reflection * Use American English spelling * Only use typed serialize signature on PHP 8+ * Update PHONGO_PARSE_PARAMETERS_NONE macro for PHP < 7.3 * Remove usage of ZEND_STRL within zend_hash_str_add This causes compile failures on PHP < 7.3 that I have yet to understand. * Fix errors in new PHONGO_PARSE_PARAMETERS macros
103 lines
2.3 KiB
PHP
103 lines
2.3 KiB
PHP
--TEST--
|
|
MongoDB\Driver\Cursor cannot rewind after starting iteration
|
|
--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 "\nRewinding sets the current element:\n";
|
|
$a->rewind();
|
|
$a->dump();
|
|
|
|
echo "\nRewinding again is OK since we haven't advanced:\n";
|
|
$a->rewind();
|
|
$a->dump();
|
|
|
|
echo "\nAdvancing populates the next element:\n";
|
|
$a->next();
|
|
$a->dump();
|
|
|
|
echo "\nRewinding after advancing is not OK:\n";
|
|
try {
|
|
$a->rewind();
|
|
echo "FAILED: rewind should throw if iteration has started\n";
|
|
} catch (MongoDB\Driver\Exception\LogicException $e) {
|
|
printf("LogicException: %s\n", $e->getMessage());
|
|
}
|
|
|
|
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
|
|
|
|
Rewinding sets the current element:
|
|
A: 0 => {_id: 0}
|
|
|
|
Rewinding again is OK since we haven't advanced:
|
|
A: 0 => {_id: 0}
|
|
|
|
Advancing populates the next element:
|
|
A: 1 => {_id: 1}
|
|
|
|
Rewinding after advancing is not OK:
|
|
LogicException: Cursors cannot rewind after starting iteration
|
|
|
|
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===
|