You are here

public function FetchTest::testQueryFetchObjectClass in Drupal 9

Same name and namespace in other branches
  1. 8 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\system\Tests\Database\FakeRecord

\Drupal\Core\Database\StatementPrefetch::fetchObject

File

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

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchObjectClass() {
  $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.');
}