You are here

public function SelectTest::testSimpleSelectExpressionMultiple in Drupal 9

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

Tests SELECT statements with multiple expressions.

File

core/tests/Drupal/KernelTests/Core/Database/SelectTest.php, line 147

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleSelectExpressionMultiple() {
  $query = $this->connection
    ->select('test');
  $name_field = $query
    ->addField('test', 'name');
  $age_double_field = $query
    ->addExpression("[age]*2");
  $age_triple_field = $query
    ->addExpression("[age]*3");
  $query
    ->condition('age', 27);
  $result = $query
    ->execute();

  // Check that the aliases are being created the way we want.
  $this
    ->assertEquals('expression', $age_double_field, 'Double age field alias is correct.');
  $this
    ->assertEquals('expression_2', $age_triple_field, 'Triple age field alias is correct.');

  // Ensure that we got the right record.
  $record = $result
    ->fetch();
  $this
    ->assertEquals('George', $record->{$name_field}, 'Fetched name is correct.');
  $this
    ->assertEquals(27 * 2, $record->{$age_double_field}, 'Fetched double age expression is correct.');
  $this
    ->assertEquals(27 * 3, $record->{$age_triple_field}, 'Fetched triple age expression is correct.');
}