function DriverSpecificSchemaTestBase::testUnsignedColumns

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::testUnsignedColumns()

Tests creating unsigned columns and data integrity thereof.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 323

Class

DriverSpecificSchemaTestBase
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnsignedColumns() : void {
  // First create the table with just a serial column.
  $table_name = 'unsigned_table';
  $table_spec = [
    'fields' => [
      'serial_column' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'serial_column',
    ],
  ];
  $this->schema
    ->createTable($table_name, $table_spec);
  // Now set up columns for the other types.
  $types = [
    'int',
    'float',
    'numeric',
  ];
  foreach ($types as $type) {
    $column_spec = [
      'type' => $type,
      'unsigned' => TRUE,
    ];
    if ($type == 'numeric') {
      $column_spec += [
        'precision' => 10,
        'scale' => 0,
      ];
    }
    $column_name = $type . '_column';
    $table_spec['fields'][$column_name] = $column_spec;
    $this->schema
      ->addField($table_name, $column_name, $column_spec);
  }
  // Finally, check each column and try to insert invalid values into them.
  foreach ($table_spec['fields'] as $column_name => $column_spec) {
    $this->assertTrue($this->schema
      ->fieldExists($table_name, $column_name), "Unsigned {$column_spec['type']} column was created.");
    $this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), "Unsigned {$column_spec['type']} column rejected a negative value.");
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.