You are here

public function QueryTest::testArraySubstitution in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php \Drupal\KernelTests\Core\Database\QueryTest::testArraySubstitution()

Tests that we can pass an array of values directly in the query.

File

core/tests/Drupal/KernelTests/Core/Database/QueryTest.php, line 15

Class

QueryTest
Tests Drupal's extended prepared statement syntax..

Namespace

Drupal\KernelTests\Core\Database

Code

public function testArraySubstitution() {
  $names = $this->connection
    ->query('SELECT [name] FROM {test} WHERE [age] IN ( :ages[] ) ORDER BY [age]', [
    ':ages[]' => [
      25,
      26,
      27,
    ],
  ])
    ->fetchAll();
  $this
    ->assertCount(3, $names, 'Correct number of names returned');
  $names = $this->connection
    ->query('SELECT [name] FROM {test} WHERE [age] IN ( :ages[] ) ORDER BY [age]', [
    ':ages[]' => [
      25,
    ],
  ])
    ->fetchAll();
  $this
    ->assertCount(1, $names, 'Correct number of names returned');
}