You are here

function DatabaseUpdateComplexTestCase::testUpdateExpression in SimpleTest 7

Test update with expression values.

File

tests/database_test.test, line 835

Class

DatabaseUpdateComplexTestCase
Tests for more complex update statements.

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