function FetchTest::testQueryFetchObjectClass

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchObjectClass()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchObjectClass()
  3. 10 core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchObjectClass()

Confirms that we can fetch a record into a class using fetchObject.

See also

\Drupal\Tests\system\Functional\Database\FakeRecord

\Drupal\Core\Database\StatementPrefetch::fetchObject

File

core/tests/Drupal/KernelTests/Core/Database/FetchTest.php, line 90

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchObjectClass() : void {
    $records = 0;
    $query = $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] = :age', [
        ':age' => 25,
    ]);
    while ($result = $query->fetchObject(FakeRecord::class, [
        1,
    ])) {
        $records += 1;
        $this->assertInstanceOf(FakeRecord::class, $result);
        $this->assertSame('John', $result->name, '25 year old is John.');
        $this->assertSame(1, $result->fakeArg, 'The record has received an argument through its constructor.');
    }
    $this->assertSame(1, $records, 'There is only one record.');
}

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