Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest::testAlterExpression()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest::testAlterExpression()

Tests that we can alter expressions in the query.

File

core/tests/Drupal/KernelTests/Core/Database/AlterTest.php, line 96

Class

AlterTest
Tests the hook_query_alter capabilities of the Select builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testAlterExpression() {
  $query = $this->connection
    ->select('test');
  $name_field = $query
    ->addField('test', 'name');
  $age_field = $query
    ->addExpression("[age]*2", 'double_age');
  $query
    ->condition('age', 27);
  $query
    ->addTag('database_test_alter_change_expressions');
  $result = $query
    ->execute();

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