function UpdateComplexTest::testUpdateExpression

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php \Drupal\KernelTests\Core\Database\UpdateComplexTest::testUpdateExpression()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php \Drupal\KernelTests\Core\Database\UpdateComplexTest::testUpdateExpression()
  3. 11.x 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 92

Class

UpdateComplexTest
Tests the Update query builder, complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUpdateExpression() : void {
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.