You are here

public function UpdateComplexTest::testUpdateExpression in Drupal 9

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

Tests UPDATE with expression values.

File

core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php, line 90

Class

UpdateComplexTest
Tests the Update query builder, complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUpdateExpression() {
  $before_age = $this->connection
    ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
    ':name' => 'Ringo',
  ])
    ->fetchField();
  $num_updated = $this->connection
    ->update('test')
    ->condition('name', 'Ringo')
    ->fields([
    'job' => 'Musician',
  ])
    ->expression('age', '[age] + :age', [
    ':age' => 4,
  ])
    ->execute();
  $this
    ->assertSame(1, $num_updated, 'Updated 1 record.');
  $num_matches = $this->connection
    ->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
    ':job' => 'Musician',
  ])
    ->fetchField();
  $this
    ->assertSame('1', $num_matches, 'Updated fields successfully.');
  $person = $this->connection
    ->query('SELECT * FROM {test} WHERE [name] = :name', [
    ':name' => 'Ringo',
  ])
    ->fetch();
  $this
    ->assertEquals('Ringo', $person->name, 'Name set correctly.');
  $this
    ->assertEquals($before_age + 4, $person->age, 'Age set correctly.');
  $this
    ->assertEquals('Musician', $person->job, 'Job set correctly.');
}