View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
class UpsertTest extends DatabaseTestBase {
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',
]);
$upsert
->values([
'job' => 'Presenter',
'age' => 31,
'name' => 'Tiffany',
]);
$upsert
->values([
'job' => 'Speaker',
'age' => 32,
'name' => 'Meredith',
]);
$result = $upsert
->execute();
$this
->assertIsInt($result);
$this
->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.');
$num_records_after = $connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
$person = $connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Presenter',
])
->fetch();
$this
->assertEquals('Presenter', $person->job, 'Job set correctly.');
$this
->assertEquals(31, $person->age, 'Age set correctly.');
$this
->assertEquals('Tiffany', $person->name, 'Name set correctly.');
$person = $connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Speaker', $person->job, 'Job was not changed.');
$this
->assertEquals(32, $person->age, 'Age updated correctly.');
$this
->assertEquals('Meredith', $person->name, 'Name was not changed.');
}
public function testUpsertWithKeywords() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {select}')
->fetchField();
$upsert = $this->connection
->upsert('select')
->key('id')
->fields([
'id',
'update',
]);
$upsert
->values([
'id' => 3,
'update' => 'Update value 2',
]);
$upsert
->values([
'id' => 1,
'update' => 'Update value 1 updated',
]);
$result = $upsert
->execute();
$this
->assertIsInt($result);
$this
->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.');
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {select}')
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
$record = $this->connection
->query('SELECT * FROM {select} WHERE [id] = :id', [
':id' => 1,
])
->fetch();
$this
->assertEquals('Update value 1 updated', $record->update);
$record = $this->connection
->query('SELECT * FROM {select} WHERE [id] = :id', [
':id' => 3,
])
->fetch();
$this
->assertEquals('Update value 2', $record->update);
$upsert
->values([
'id' => 4,
'update' => 'Another value',
]);
$return_value = $upsert
->execute();
$this
->assertSame(1, $return_value);
$record = $this->connection
->query('SELECT * FROM {select} WHERE [id] = :id', [
':id' => 4,
])
->fetch();
$this
->assertEquals('Another value', $record->update);
}
public function testUpsertNonExistingTable() : void {
$this
->expectException(DatabaseExceptionWrapper::class);
$upsert = $this->connection
->upsert('a-table-that-does-not-exist')
->key('id')
->fields([
'id',
'update',
]);
$upsert
->values([
'id' => 1,
'update' => 'Update value 1 updated',
]);
$upsert
->execute();
}
public function testUpsertNullBlob() {
$id = $this->connection
->insert('test_one_blob')
->fields([
'blob1' => 'test',
])
->execute();
$r = $this->connection
->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [
':id' => $id,
])
->fetchAssoc();
$this
->assertSame('test', $r['blob1']);
$this->connection
->upsert('test_one_blob')
->key('id')
->fields([
'id',
'blob1',
])
->values([
'id' => $id,
'blob1' => NULL,
])
->values([
'id' => $id + 1,
'blob1' => NULL,
])
->execute();
$r = $this->connection
->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [
':id' => $id,
])
->fetchAssoc();
$this
->assertNull($r['blob1']);
$r = $this->connection
->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [
':id' => $id + 1,
])
->fetchAssoc();
$this
->assertNull($r['blob1']);
}
}