function DatabaseUpdateComplexTestCase::testUpdateExpression in Drupal 7
Test update with expression values.
File
- modules/
simpletest/ tests/ database_test.test, line 986
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, '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;
}