function FetchLegacyTest::testQueryFetchArray

Confirms that we can fetch a record to an associative array explicitly.

File

core/tests/Drupal/KernelTests/Core/Database/FetchLegacyTest.php, line 40

Class

FetchLegacyTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchArray() : void {
    $this->expectDeprecation("Passing the 'fetch' key as an integer to \$options in query() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\FetchAs enum instead. See https://www.drupal.org/node/3488338");
    $this->expectDeprecation("Passing the 'fetch' key as an integer to \$options in prepareStatement() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\FetchAs enum instead. See https://www.drupal.org/node/3488338");
    $this->expectDeprecation("Passing the 'fetch' key as an integer to \$options in execute() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\FetchAs enum instead. See https://www.drupal.org/node/3488338");
    $records = [];
    $result = $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] = :age', [
        ':age' => 25,
    ], [
        'fetch' => \PDO::FETCH_ASSOC,
    ]);
    foreach ($result as $record) {
        $records[] = $record;
        $this->assertIsArray($record);
        $this->assertArrayHasKey('name', $record);
        $this->assertSame('John', $record['name']);
    }
    $this->assertCount(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.