function StatementTest::testStatementRewind

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/StatementTest.php \Drupal\KernelTests\Core\Database\StatementTest::testStatementRewind()

Tests statement rewinding.

File

core/tests/Drupal/KernelTests/Core/Database/StatementTest.php, line 165

Class

StatementTest
Tests the Statement classes.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testStatementRewind() : void {
    $statement = $this->connection
        ->query('SELECT * FROM {test}');
    foreach ($statement as $row) {
        $this->assertNotNull($row);
    }
    // Trying to iterate through the same statement again should fail.
    $this->expectError();
    $this->expectErrorMessage('Attempted rewinding a StatementInterface object when fetching has already started. Refactor your code to avoid rewinding statement objects.');
    foreach ($statement as $row) {
        $this->assertNotNull($row);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.