InsertDefaultsTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Query\NoFieldsException;
class InsertDefaultsTest extends DatabaseTestBase {
public function testDefaultInsert() {
$query = $this->connection
->insert('test')
->useDefaults([
'job',
]);
$id = $query
->execute();
$job = $this->connection
->query('SELECT [job] FROM {test} WHERE [id] = :id', [
':id' => $id,
])
->fetchField();
$this
->assertSame('Undefined', $job, 'Default field value is set.');
}
public function testDefaultEmptyInsert() {
$num_records_before = (int) $this->connection
->query('SELECT COUNT(*) FROM {test}')
->fetchField();
try {
$this->connection
->insert('test')
->execute();
$this
->fail('Expected exception NoFieldsException has not been thrown.');
} catch (NoFieldsException $e) {
}
$num_records_after = (int) $this->connection
->query('SELECT COUNT(*) FROM {test}')
->fetchField();
$this
->assertSame($num_records_before, $num_records_after, 'Do nothing as no fields are specified.');
}
public function testDefaultInsertWithFields() {
$query = $this->connection
->insert('test')
->fields([
'name' => 'Bob',
])
->useDefaults([
'job',
]);
$id = $query
->execute();
$job = $this->connection
->query('SELECT [job] FROM {test} WHERE [id] = :id', [
':id' => $id,
])
->fetchField();
$this
->assertSame('Undefined', $job, 'Default field value is set.');
}
}