mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX Ensure consistent behaviour with repeat iterations
This commit is contained in:
parent
62ee63706f
commit
e140c3786c
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,6 @@ class DataListTest extends SapphireTest
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testFilterDataObjectByCreatedDate()
|
||||
{
|
||||
// create an object to test with
|
||||
|
@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user