function FetchTest::testQueryFetchObjectClassNoConstructorArgs

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Database/FetchTest.php \Drupal\KernelTests\Core\Database\FetchTest::testQueryFetchObjectClassNoConstructorArgs()

Confirms that we can fetch a record into a class without constructor args.

See also

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

\Drupal\Core\Database\StatementPrefetch::fetchObject

File

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

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchObjectClassNoConstructorArgs() : void {
    $records = 0;
    $query = $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] = :age', [
        ':age' => 25,
    ]);
    while ($result = $query->fetchObject(FakeRecord::class)) {
        $records += 1;
        $this->assertInstanceOf(FakeRecord::class, $result);
        $this->assertSame('John', $result->name);
        $this->assertSame(0, $result->fakeArg);
    }
    $this->assertSame(1, $records);
}

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