You are here

public function UpdateComplexTest::testUpdateExpression in Drupal 8

Same name and namespace in other branches
  1. 9 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.');
}