public function UpsertTest::testUpsert in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Database/UpsertTest.php \Drupal\system\Tests\Database\UpsertTest::testUpsert()
Confirms that we can upsert (update-or-insert) records successfully.
File
- core/
modules/ system/ src/ Tests/ Database/ UpsertTest.php, line 22 - Contains \Drupal\system\Tests\Database\UpsertTest.
Class
- UpsertTest
- Tests the Upsert query builder.
Namespace
Drupal\system\Tests\DatabaseCode
public function testUpsert() {
$connection = Database::getConnection();
$num_records_before = $connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$upsert = $connection
->upsert('test_people')
->key('job')
->fields([
'job',
'age',
'name',
]);
// Add a new row.
$upsert
->values([
'job' => 'Presenter',
'age' => 31,
'name' => 'Tiffany',
]);
// Update an existing row.
$upsert
->values([
'job' => 'Speaker',
// The initial age was 30.
'age' => 32,
'name' => 'Meredith',
]);
$upsert
->execute();
$num_records_after = $connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEqual($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
$person = $connection
->query('SELECT * FROM {test_people} WHERE job = :job', array(
':job' => 'Presenter',
))
->fetch();
$this
->assertEqual($person->job, 'Presenter', 'Job set correctly.');
$this
->assertEqual($person->age, 31, 'Age set correctly.');
$this
->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
$person = $connection
->query('SELECT * FROM {test_people} WHERE job = :job', array(
':job' => 'Speaker',
))
->fetch();
$this
->assertEqual($person->job, 'Speaker', 'Job was not changed.');
$this
->assertEqual($person->age, 32, 'Age updated correctly.');
$this
->assertEqual($person->name, 'Meredith', 'Name was not changed.');
}