function FetchTest::testQueryFetchAllAsFetchModeClassInstances

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

Confirms that we can fetch class instances by setting fetch mode.

File

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

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchAllAsFetchModeClassInstances() : void {
  $query = $this->connection
    ->query('SELECT [name] FROM {test} ORDER BY [name]');
  $query->setFetchMode(FetchAs::ClassObject, FakeRecord::class, [
    // Test custom constructor arguments.
'fakeArg' => 10,
  ]);
  $query_result = $query->fetchAll();
  $arrayed = array_map(fn(FakeRecord $record) => \get_object_vars($record), $query_result);
  $this->assertEquals([
    'George',
    'John',
    'Paul',
    'Ringo',
  ], array_column($arrayed, 'name'));
  $this->assertEquals([
    10,
    10,
    10,
    10,
  ], array_column($arrayed, 'fakeArg'));
}

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