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. 10 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() {
    $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->assertIdentical($num_updated, 1, 'Updated 1 record.');
    $num_matches = $this->connection
        ->query('SELECT COUNT(*) FROM {test} WHERE job = :job', [
        ':job' => 'Musician',
    ])
        ->fetchField();
    $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
    $person = $this->connection
        ->query('SELECT * FROM {test} WHERE name = :name', [
        ':name' => 'Ringo',
    ])
        ->fetch();
    $this->assertEqual($person->name, 'Ringo', 'Name set correctly.');
    $this->assertEqual($person->age, $before_age + 4, 'Age set correctly.');
    $this->assertEqual($person->job, 'Musician', 'Job set correctly.');
}

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