function FetchLegacyTest::testQueryFetchAllAssoc

Tests ::fetchAllAssoc().

Attributes

#[IgnoreDeprecations]

File

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

Class

FetchLegacyTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchAllAssoc() : void {
  $this->expectDeprecation("Passing the \$fetch argument as an integer to fetchAllAssoc() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\Statement\\FetchAs enum instead. See https://www.drupal.org/node/3488338");
  $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.