FIX Ensure consistent behaviour with repeat iterations

This commit is contained in:
Guy Sartorelli 2022-09-09 15:45:12 +12:00
parent 62ee63706f
commit e140c3786c
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
3 changed files with 60 additions and 1 deletions

View File

@ -63,6 +63,11 @@ class MySQLQuery extends Query
}
yield $data;
}
// Check for the method first since $this->handle is a mixed type
if (method_exists($this->handle, 'data_seek')) {
// Reset so the query can be iterated over again
$this->handle->data_seek(0);
}
}
}

View File

@ -42,7 +42,6 @@ class DataListTest extends SapphireTest
);
}
public function testFilterDataObjectByCreatedDate()
{
// create an object to test with

View File

@ -7,6 +7,7 @@ use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\MSSQL\MSSQLDatabase;
use SilverStripe\Dev\SapphireTest;
use Exception;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Tests\DatabaseTest\MyObject;
/**
@ -293,4 +294,58 @@ class DatabaseTest extends SapphireTest
$this->assertEquals($inputData, $query->map());
$this->assertEquals($inputData, $query->map());
}
/**
* Test that repeated abstracted iteration of a query returns all records.
*/
public function testRepeatedIterationUsingAbstraction()
{
$inputData = ['one', 'two', 'three', 'four'];
foreach ($inputData as $i => $text) {
$x = new MyObject();
$x->MyField = $text;
$x->MyInt = $i;
$x->write();
}
$select = SQLSelect::create(['"MyInt"', '"MyField"'], '"DatabaseTest_MyObject"', orderby: ['"MyInt"']);
$this->assertEquals($inputData, $select->execute()->map());
$this->assertEquals($inputData, $select->execute()->map());
}
/**
* Test that stopping iteration part-way through produces predictable results
* on a subsequent iteration.
* This test is here to ensure consistency between implementations (e.g. mysql vs postgres, etc)
*/
public function testRepeatedPartialIteration()
{
$inputData = ['one', 'two', 'three', 'four'];
foreach ($inputData as $i => $text) {
$x = new MyObject();
$x->MyField = $text;
$x->MyInt = $i;
$x->write();
}
$query = DB::query('SELECT "MyInt", "MyField" FROM "DatabaseTest_MyObject" ORDER BY "MyInt"');
$i = 0;
foreach ($query as $record) {
$this->assertEquals($inputData[$i], $record['MyField']);
$i++;
if ($i > 1) {
break;
}
}
// Continue from where we left off, since we're using a Generator
foreach ($query as $record) {
$this->assertEquals($inputData[$i], $record['MyField']);
$i++;
}
}
}