You are here

function UpdateComplexTest::testUpdateExpression in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/UpdateComplexTest.php \Drupal\system\Tests\Database\UpdateComplexTest::testUpdateExpression()

Tests UPDATE with expression values.

File

core/modules/system/src/Tests/Database/UpdateComplexTest.php, line 95
Contains \Drupal\system\Tests\Database\UpdateComplexTest.

Class

UpdateComplexTest
Tests the Update query builder, complex queries.

Namespace

Drupal\system\Tests\Database

Code

function testUpdateExpression() {
  $before_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Ringo',
  ))
    ->fetchField();
  $GLOBALS['larry_test'] = 1;
  $num_updated = db_update('test')
    ->condition('name', 'Ringo')
    ->fields(array(
    'job' => 'Musician',
  ))
    ->expression('age', 'age + :age', array(
    ':age' => 4,
  ))
    ->execute();
  $this
    ->assertIdentical($num_updated, 1, 'Updated 1 record.');
  $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(
    ':job' => 'Musician',
  ))
    ->fetchField();
  $this
    ->assertIdentical($num_matches, '1', 'Updated fields successfully.');
  $person = db_query('SELECT * FROM {test} WHERE name = :name', array(
    ':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.');
  $GLOBALS['larry_test'] = 0;
}