Skip to content

Commit

Permalink
Added schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msarca committed May 31, 2018
1 parent a316903 commit 42e2762
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Expand Up @@ -3,5 +3,8 @@
<testsuite name="Opis Database SQL Test Suite">
<directory suffix="Test.php">./tests/SQL</directory>
</testsuite>
<testsuite name="Opis Database Schema Test Suite">
<directory suffix="Test.php">./tests/Schema</directory>
</testsuite>
</testsuites>
</phpunit>
37 changes: 37 additions & 0 deletions tests/Schema.php
@@ -0,0 +1,37 @@
<?php
/* ===========================================================================
* Copyright 2018 The Opis Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============================================================================ */

namespace Opis\Database\Test;

use Opis\Database\Schema\CreateTable;
use Opis\Database\Schema as BaseSchema;

class Schema extends BaseSchema
{
public function create(string $table, callable $callback)
{
$compiler = $this->connection->schemaCompiler();

$schema = new CreateTable($table);

$callback($schema);

return implode("\n", array_map(function($value){
return $value['sql'];
}, $compiler->create($schema)));
}
}
182 changes: 182 additions & 0 deletions tests/Schema/BaseClass.php
@@ -0,0 +1,182 @@
<?php
/* ===========================================================================
* Copyright 2018 The Opis Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============================================================================ */

namespace Opis\Database\Test\Schema;

use Opis\Database\Schema\CreateTable;
use Opis\Database\Test\Schema;
use PHPUnit\Framework\TestCase;

class BaseClass extends TestCase
{
protected static $data = [];

/** @var Schema */
protected $schema;

public function testCreateTable()
{
$result = $this->schema->create('foo', function (CreateTable $table){

});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testAddSingleColumn()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testAddMultipleColumns()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a');
$table->integer('b');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testTypes()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a');
$table->float('b');
$table->double('c');
$table->decimal('d');
$table->decimal("d1", 4);
$table->decimal("d2", 4, 8);
$table->boolean('e');
$table->string("f");
$table->string("f1", 32);
$table->fixed('g');
$table->fixed('g1', 2);
$table->date('h');
$table->dateTime('i');
$table->timestamp('j');
$table->time('k');
$table->binary('l');
$table->text('m');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testIntSizes()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->size('tiny');
$table->integer('b')->size('small');
$table->integer('c')->size('normal');
$table->integer('d')->size('medium');
$table->integer('e')->size('big');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testTextSizes()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->text('a')->size('tiny');
$table->text('b')->size('small');
$table->text('c')->size('normal');
$table->text('d')->size('medium');
$table->text('e')->size('big');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testBinarySizes()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->binary('a')->size('tiny');
$table->binary('b')->size('small');
$table->binary('c')->size('normal');
$table->binary('d')->size('medium');
$table->binary('e')->size('big');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testColumnProperties()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->unsigned();
$table->float('b')->defaultValue(0.1);
$table->string('c')->notNull();
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testColumnConstraints()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->primary();
$table->integer('b')->unique();
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testColumnNamedConstraints()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->primary('pk_a');
$table->integer('b')->unique('uk_b');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}

public function testAutoincrement()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->autoincrement();
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}
public function testNamedAutoincrement()
{
$result = $this->schema->create('foo', function (CreateTable $table){
$table->integer('a')->autoincrement('x');
});

$expected = static::$data[__FUNCTION__];
$this->assertEquals($expected, $result);
}
}
137 changes: 137 additions & 0 deletions tests/Schema/MySqlTest.php
@@ -0,0 +1,137 @@
<?php
/* ===========================================================================
* Copyright 2018 The Opis Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============================================================================ */

namespace Opis\Database\Test\Schema;

use Opis\Database\Test\Connection;
use Opis\Database\Test\Schema;

class MySqlTest extends BaseClass
{
/** @var Schema */
protected $schema;

protected static $data = [];

public static function setUpBeforeClass()
{
static::$data = [
'testCreateTable' => implode("\n", [
"CREATE TABLE `foo`(", "", ")"
]),
'testAddSingleColumn' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT",
]), ")"
]),
'testAddMultipleColumns' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT",
"`b` INT"
]), ")"
]),
'testTypes' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT",
"`b` FLOAT",
"`c` DOUBLE",
"`d` DECIMAL",
"`d1` DECIMAL (4)",
"`d2` DECIMAL (4, 8)",
"`e` TINYINT(1)",
"`f` VARCHAR(255)",
"`f1` VARCHAR(32)",
"`g` CHAR(255)",
"`g1` CHAR(2)",
"`h` DATE",
"`i` DATETIME",
"`j` TIMESTAMP",
"`k` TIME",
"`l` BLOB",
"`m` TEXT",
]), ")"
]),
'testIntSizes' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` TINYINT",
"`b` SMALLINT",
"`c` INT",
"`d` MEDIUMINT",
"`e` BIGINT",
]), ")"
]),
'testTextSizes' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` TINYTEXT",
"`b` TINYTEXT",
"`c` TEXT",
"`d` MEDIUMTEXT",
"`e` LONGTEXT",
]), ")"
]),
'testBinarySizes' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` TINYBLOB",
"`b` TINYBLOB",
"`c` BLOB",
"`d` MEDIUMBLOB",
"`e` LONGBLOB",
]), ")"
]),
'testColumnProperties' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT UNSIGNED",
"`b` FLOAT DEFAULT 0.1",
"`c` VARCHAR(255) NOT NULL"
]), ")"
]),
'testColumnConstraints' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT",
"`b` INT",
"CONSTRAINT `foo_pk_a` PRIMARY KEY (`a`)",
"CONSTRAINT `foo_uk_b` UNIQUE (`b`)",
]), ")"
]),
'testColumnNamedConstraints' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT",
"`b` INT",
"CONSTRAINT `pk_a` PRIMARY KEY (`a`)",
"CONSTRAINT `uk_b` UNIQUE (`b`)",
]), ")"
]),
'testAutoincrement' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT AUTO_INCREMENT",
"CONSTRAINT `foo_pk_a` PRIMARY KEY (`a`)",
]), ")"
]),
'testNamedAutoincrement' => implode("\n", [
"CREATE TABLE `foo`(", implode(",\n", [
"`a` INT AUTO_INCREMENT",
"CONSTRAINT `x` PRIMARY KEY (`a`)",
]), ")"
])
];
}

public function setUp()
{
$this->schema = new Schema(new Connection("mysql"));
}
}

0 comments on commit 42e2762

Please sign in to comment.