function FetchTest::testQueryFetchAllAsClassInstances

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

Confirms that we can fetch all records as class instances explicitly.

File

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

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchAllAsClassInstances() : void {
  $query = $this->connection
    ->select(table: 'test', options: [
    'fetch' => FakeRecord::class,
  ]);
  $query->addField('test', 'name');
  $query->orderBy('name');
  $query_result = $query->execute()
    ->fetchAll();
  $this->assertContainsOnlyInstancesOf(FakeRecord::class, $query_result);
  $names = array_map(fn(FakeRecord $record) => $record->name, $query_result);
  $expected_names = [
    'George',
    'John',
    'Paul',
    'Ringo',
  ];
  $this->assertEquals($expected_names, $names);
}

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