View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Query\Merge;
use Drupal\Core\Database\Query\InvalidMergeQueryException;
class MergeTest extends DatabaseTestBase {
public function testMergeInsert() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$result = $this->connection
->merge('test_people')
->key('job', 'Presenter')
->fields([
'age' => 31,
'name' => 'Tiffany',
])
->execute();
$this
->assertEquals(Merge::STATUS_INSERT, $result, 'Insert status returned.');
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Presenter',
])
->fetch();
$this
->assertEquals('Tiffany', $person->name, 'Name set correctly.');
$this
->assertEquals(31, $person->age, 'Age set correctly.');
$this
->assertEquals('Presenter', $person->job, 'Job set correctly.');
}
public function testMergeUpdate() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$result = $this->connection
->merge('test_people')
->key('job', 'Speaker')
->fields([
'age' => 31,
'name' => 'Tiffany',
])
->execute();
$this
->assertEquals(Merge::STATUS_UPDATE, $result, 'Update status returned.');
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge updated properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Tiffany', $person->name, 'Name set correctly.');
$this
->assertEquals(31, $person->age, 'Age set correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job set correctly.');
}
public function testMergeUpdateExcept() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this->connection
->merge('test_people')
->key('job', 'Speaker')
->insertFields([
'age' => 31,
])
->updateFields([
'name' => 'Tiffany',
])
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge updated properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Tiffany', $person->name, 'Name set correctly.');
$this
->assertEquals(30, $person->age, 'Age skipped correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job set correctly.');
}
public function testMergeUpdateExplicit() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this->connection
->merge('test_people')
->key('job', 'Speaker')
->insertFields([
'age' => 31,
'name' => 'Tiffany',
])
->updateFields([
'name' => 'Joe',
])
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge updated properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Joe', $person->name, 'Name set correctly.');
$this
->assertEquals(30, $person->age, 'Age skipped correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job set correctly.');
}
public function testMergeUpdateExpression() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$age_before = $this->connection
->query('SELECT [age] FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetchField();
$this->connection
->merge('test_people')
->key('job', 'Speaker')
->fields([
'name' => 'Tiffany',
])
->insertFields([
'age' => 31,
])
->expression('age', '[age] + :age', [
':age' => 4,
])
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge updated properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Tiffany', $person->name, 'Name set correctly.');
$this
->assertEquals($age_before + 4, $person->age, 'Age updated correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job set correctly.');
}
public function testMergeInsertWithoutUpdate() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this->connection
->merge('test_people')
->key('job', 'Presenter')
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Presenter',
])
->fetch();
$this
->assertEquals('', $person->name, 'Name set correctly.');
$this
->assertEquals(0, $person->age, 'Age set correctly.');
$this
->assertEquals('Presenter', $person->job, 'Job set correctly.');
}
public function testMergeUpdateWithoutUpdate() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this->connection
->merge('test_people')
->key('job', 'Speaker')
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge skipped properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Meredith', $person->name, 'Name skipped correctly.');
$this
->assertEquals(30, $person->age, 'Age skipped correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job skipped correctly.');
$this->connection
->merge('test_people')
->key('job', 'Speaker')
->insertFields([
'age' => 31,
])
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {test_people}')
->fetchField();
$this
->assertEquals($num_records_before, $num_records_after, 'Merge skipped properly.');
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Speaker',
])
->fetch();
$this
->assertEquals('Meredith', $person->name, 'Name skipped correctly.');
$this
->assertEquals(30, $person->age, 'Age skipped correctly.');
$this
->assertEquals('Speaker', $person->job, 'Job skipped correctly.');
}
public function testInvalidMerge() {
$this
->expectException(InvalidMergeQueryException::class);
$this->connection
->merge('test_people')
->fields([
'age' => 31,
'name' => 'Tiffany',
])
->execute();
}
public function testMergeWithReservedWords() {
$num_records_before = $this->connection
->query('SELECT COUNT(*) FROM {select}')
->fetchField();
$this->connection
->merge('select')
->key('id', 2)
->execute();
$num_records_after = $this->connection
->query('SELECT COUNT(*) FROM {select}')
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
$person = $this->connection
->query('SELECT * FROM {select} WHERE [id] = :id', [
':id' => 2,
])
->fetch();
$this
->assertEquals('', $person->update);
$this
->assertEquals('2', $person->id);
}
}