function FetchTest::testQueryFetchAllAssoc

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

Tests ::fetchAllAssoc().

File

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

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchAllAssoc() : void {
    $expected_result = [
        "Singer" => [
            "id" => "2",
            "name" => "George",
            "age" => "27",
            "job" => "Singer",
        ],
        "Drummer" => [
            "id" => "3",
            "name" => "Ringo",
            "age" => "28",
            "job" => "Drummer",
        ],
    ];
    $statement = $this->connection
        ->query('SELECT * FROM {test} WHERE [age] > :age', [
        ':age' => 26,
    ]);
    $result = $statement->fetchAllAssoc('job', \PDO::FETCH_ASSOC);
    $this->assertSame($expected_result, $result);
    $statement = $this->connection
        ->query('SELECT * FROM {test} WHERE [age] > :age', [
        ':age' => 26,
    ]);
    $result = $statement->fetchAllAssoc('job', \PDO::FETCH_OBJ);
    $this->assertEquals((object) $expected_result['Singer'], $result['Singer']);
    $this->assertEquals((object) $expected_result['Drummer'], $result['Drummer']);
}

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