View source
<?php
namespace Drupal\Tests\sqlsrv\Kernel;
use Drupal\Core\Database\SchemaObjectDoesNotExistException;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\KernelTests\Core\Database\DatabaseTestBase;
class SchemaTest extends DatabaseTestBase {
protected $table = [];
protected $schema;
protected function setUp() {
parent::setUp();
$schema = $this->connection
->schema();
$this->schema = $schema;
$this->table = [
'description' => 'New Comment',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => "A person's name",
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'binary' => TRUE,
],
'age' => [
'description' => "The person's age",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'job' => [
'description' => "The person's job",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'Undefined',
],
],
'primary key' => [
'id',
],
'unique keys' => [
'name' => [
'name',
],
],
'indexes' => [
'ages' => [
'age',
],
],
];
}
public function testDropTableComment() {
$this->schema
->dropTable('test');
$this
->assertFalse($this->schema
->getComment('test'));
$this->schema
->createTable('test', $this->table);
$comment = $this->schema
->getComment('test');
$this
->assertEquals('New Comment', $comment);
}
public function testDropFieldComment() {
$this->schema
->dropField('test', 'name');
$this
->assertFalse($this->schema
->getComment('test', 'name'));
$spec = $this->table['fields']['name'];
$spec['description'] = 'New name comment';
$this->schema
->addField('test', 'name', $spec);
$comment = $this->schema
->getComment('test', 'name');
$this
->assertEquals('New name comment', $comment);
}
public function testChangeFieldComment() {
$spec = $this->table['fields']['name'];
$spec['description'] = 'New name comment';
$this->schema
->changeField('test', 'name', 'name', $spec);
$comment = $this->schema
->getComment('test', 'name');
$this
->assertEquals('New name comment', $comment);
}
public function testAddDefaultException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->fieldSetDefault('test', 'noname', 'Elvis');
}
public function testAddNotDefaultException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->fieldSetNoDefault('test', 'noname');
}
public function testCreateTableExists() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->createTable('test', $this->table);
}
public function testGetDefaultSchemaNoDefault() {
$schema = new \ReflectionClass('\\Drupal\\sqlsrv\\Driver\\Database\\sqlsrv\\Schema');
$property = $schema
->getProperty("defaultSchema");
$property
->setAccessible(TRUE);
$property
->setValue($this->schema, NULL);
$schema_name = $this->schema
->getDefaultSchema();
$this
->assertEquals($schema_name, 'dbo');
}
public function testRenameTableAlreadyExistsException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->renameTable('tabledoesnotexist', 'test_new');
}
public function testRenameTableDoesNotExistException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->renameTable('test_people', 'test');
}
public function testNewFieldExistsException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->addField('test', 'name', $this->table['fields']['name']);
}
public function testPrimaryKeyTableDoesNotExistException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->addPrimaryKey('test_new', 'name');
}
public function testPrimaryKeyExistsException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->addPrimaryKey('test', 'name');
}
public function testUniqueKeyTableDoesNotExistException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->addUniqueKey('test_new', 'name', $this->table['fields']);
}
public function testUniqueKeyExistsException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->addUniqueKey('test', 'name', $this->table['fields']);
}
public function testIndexTableDoesNotExistException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->addIndex('test_new', 'name', $this->table['fields'], $this->table);
}
public function testIndexExistsException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->addIndex('test', 'ages', $this->table['fields'], $this->table);
}
public function testIntroscpectSchemaException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->introspectIndexSchema('test_new');
}
public function testFieldDoesNotExistException() {
$this
->expectException(SchemaObjectDoesNotExistException::class);
$this->schema
->changeField('test', 'age1', 'age2', $this->table['fields']['age']);
}
public function testFieldExistsException() {
$this
->expectException(SchemaObjectExistsException::class);
$this->schema
->changeField('test', 'age', 'name', $this->table['fields']['age']);
}
}