示例#1
0
 public function testFactories()
 {
     // char
     $this->assertEquals(new CharColumn('foo', 'char', 0), Column::char('foo', 0));
     $this->assertEquals(new CharColumn('foo', 'varchar', 0), Column::varchar('foo', 0));
     // datetime
     $this->assertEquals(new DateTimeColumn('foo'), Column::datetime('foo'));
     // decimal
     $this->assertEquals(new DecimalColumn('foo', 10, 2), Column::decimal('foo', 10, 2));
     // float
     $this->assertEquals(new FloatColumn('foo', 10, 2), Column::float('foo', 10, 2));
     // int
     $this->assertEquals(new IntColumn('foo', 'tinyint'), Column::tinyint('foo'));
     $this->assertEquals(new IntColumn('foo', 'smallint'), Column::smallint('foo'));
     $this->assertEquals(new IntColumn('foo', 'int'), Column::int('foo'));
     $this->assertEquals(new IntColumn('foo', 'mediumint'), Column::mediumint('foo'));
     $this->assertEquals(new IntColumn('foo', 'bigint'), Column::bigint('foo'));
     $this->assertEquals((new IntColumn('foo', 'tinyint'))->setUnsigned()->setAutoIncrement(), Column::tinyserial('foo'));
     $this->assertEquals((new IntColumn('foo', 'smallint'))->setUnsigned()->setAutoIncrement(), Column::smallserial('foo'));
     $this->assertEquals((new IntColumn('foo', 'int'))->setUnsigned()->setAutoIncrement(), Column::serial('foo'));
     $this->assertEquals((new IntColumn('foo', 'mediumint'))->setUnsigned()->setAutoIncrement(), Column::mediumserial('foo'));
     $this->assertEquals((new IntColumn('foo', 'bigint'))->setUnsigned()->setAutoIncrement(), Column::bigserial('foo'));
     // text
     $this->assertEquals(new TextColumn('foo', 'tinytext'), Column::tinytext('foo'));
     $this->assertEquals(new TextColumn('foo', 'text'), Column::text('foo'));
     $this->assertEquals(new TextColumn('foo', 'mediumtext'), Column::mediumtext('foo'));
     $this->assertEquals(new TextColumn('foo', 'longtext'), Column::longtext('foo'));
     // timestamp
     $this->assertEquals(new TimestampColumn('foo'), Column::timestamp('foo'));
 }
 public function testDecimal()
 {
     $column = new DecimalColumn('foo', 10, 2);
     $this->assertSame('foo', $column->getName());
     $this->assertSame('decimal', $column->getType());
     $this->assertSame(10, $column->getPrecision());
     $this->assertSame(2, $column->getScale());
     $this->assertSame(false, $column->isUnsigned());
     $this->assertSame(false, $column->isNullable());
     $this->assertSame(false, $column->hasDefault());
     $this->assertSame(null, $column->getDefault());
     $this->assertSame(false, $column->hasComment());
     $this->assertSame('', $column->getComment());
     $this->assertSame('`foo` decimal(10,2) NOT NULL', $column->buildCreate());
     $this->assertSame('DROP COLUMN `foo`', $column->buildDrop());
     $this->assertSame('ADD COLUMN `foo` decimal(10,2) NOT NULL', $column->buildAdd());
     $this->assertSame('ADD COLUMN `foo` decimal(10,2) NOT NULL AFTER `bar`', $column->buildAdd(Column::int('bar')));
     $this->assertSame('CHANGE COLUMN `bar` `foo` decimal(10,2) NOT NULL', $column->buildChange(Column::int('bar')));
     // unsigned
     $column = new DecimalColumn('foo', 10, 2);
     $column->setUnsigned();
     $this->assertSame(true, $column->isUnsigned());
     $this->assertSame('`foo` decimal(10,2) UNSIGNED NOT NULL', $column->buildCreate());
     // unsigned + nullable
     $column->setNullable();
     $this->assertSame('`foo` decimal(10,2) UNSIGNED', $column->buildCreate());
     // unsigned + nullable + default
     $column->setDefault(1);
     $this->assertSame('`foo` decimal(10,2) UNSIGNED DEFAULT "1.00"', $column->buildCreate());
     // unsigned + nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` decimal(10,2) UNSIGNED DEFAULT "1.00" COMMENT "foobar"', $column->buildCreate());
     // unsigned + default
     $column = new DecimalColumn('foo', 10, 2);
     $column->setUnsigned()->setDefault(1);
     $this->assertSame('`foo` decimal(10,2) UNSIGNED NOT NULL DEFAULT "1.00"', $column->buildCreate());
     // unsigned + default + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` decimal(10,2) UNSIGNED NOT NULL DEFAULT "1.00" COMMENT "foobar"', $column->buildCreate());
     // unsigned + comment
     $column = new DecimalColumn('foo', 10, 2);
     $column->setUnsigned()->setComment('foobar');
     $this->assertSame('`foo` decimal(10,2) UNSIGNED NOT NULL COMMENT "foobar"', $column->buildCreate());
     // nullable
     $column = new DecimalColumn('foo', 10, 2);
     $column->setNullable();
     $this->assertSame(true, $column->isNullable());
     $this->assertSame('`foo` decimal(10,2)', $column->buildCreate());
     // nullable + default
     $column->setDefault(1);
     $this->assertSame('`foo` decimal(10,2) DEFAULT "1.00"', $column->buildCreate());
     // nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` decimal(10,2) DEFAULT "1.00" COMMENT "foobar"', $column->buildCreate());
     // nullable + comment
     $column = new DecimalColumn('foo', 10, 2);
     $column->setNullable()->setComment('foobar');
     $this->assertSame('`foo` decimal(10,2) COMMENT "foobar"', $column->buildCreate());
     // default
     $column = new DecimalColumn('foo', 10, 4);
     $column->setDefault(1.12345);
     $this->assertSame(1.12345, $column->getDefault());
     $this->assertSame('`foo` decimal(10,4) NOT NULL DEFAULT "1.1235"', $column->buildCreate());
     $column = new DecimalColumn('foo', 10, 4);
     $column->setDefault(1);
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame(1.0, $column->getDefault());
     $this->assertSame('`foo` decimal(10,4) NOT NULL DEFAULT "1.0000"', $column->buildCreate());
     // default + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` decimal(10,4) NOT NULL DEFAULT "1.0000" COMMENT "foobar"', $column->buildCreate());
     // comment
     $column = new DecimalColumn('foo', 10, 2);
     $column->setComment('foobar');
     $this->assertSame(true, $column->hasComment());
     $this->assertSame('foobar', $column->getComment());
     $this->assertSame('`foo` decimal(10,2) NOT NULL COMMENT "foobar"', $column->buildCreate());
 }
 public function testDateTimeColumn()
 {
     $column = new DateTimeColumn('foo');
     $this->assertSame('foo', $column->getName());
     $this->assertSame('datetime', $column->getType());
     $this->assertSame(false, $column->isNullable());
     $this->assertSame(false, $column->hasDefault());
     $this->assertSame(false, $column->isDefaultCurrent());
     $this->assertSame(false, $column->isOnUpdateCurrent());
     $this->assertSame(false, $column->hasComment());
     $this->assertSame('', $column->getComment());
     $this->assertSame('`foo` datetime NOT NULL', $column->buildCreate());
     $this->assertSame('DROP COLUMN `foo`', $column->buildDrop());
     $this->assertSame('ADD COLUMN `foo` datetime NOT NULL', $column->buildAdd());
     $this->assertSame('ADD COLUMN `foo` datetime NOT NULL AFTER `bar`', $column->buildAdd(Column::int('bar')));
     $this->assertSame('CHANGE COLUMN `bar` `foo` datetime NOT NULL', $column->buildChange(Column::int('bar')));
     // nullable
     $column->setNullable();
     $this->assertSame(true, $column->isNullable());
     $this->assertSame('`foo` datetime', $column->buildCreate());
     // nullable + default
     $column->setDefault('2016-01-01');
     $this->assertSame('`foo` datetime DEFAULT "2016-01-01 00:00:00"', $column->buildCreate());
     // nullable + default + on update current
     $column->setOnUpdateCurrent();
     $this->assertSame('`foo` datetime DEFAULT "2016-01-01 00:00:00" ON UPDATE CURRENT_TIMESTAMP', $column->buildCreate());
     // nullable + default + on update current + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` datetime DEFAULT "2016-01-01 00:00:00" ON UPDATE CURRENT_TIMESTAMP COMMENT "foobar"', $column->buildCreate());
     // default
     $column = new DateTimeColumn('foo');
     $column->setDefault(new \DateTime('2016-01-01 11:00:00', new \DateTimeZone('Asia/Tokyo')));
     $this->assertSame('`foo` datetime NOT NULL DEFAULT "2016-01-01 02:00:00"', $column->buildCreate());
     $column = new DateTimeColumn('foo');
     $column->setDefault(null);
     $this->assertSame(false, $column->hasDefault());
     $column->setDefault('');
     $this->assertSame(false, $column->hasDefault());
     $this->assertSame('`foo` datetime NOT NULL', $column->buildCreate());
     $column = new DateTimeColumn('foo');
     $column->setDefault('2016-01-01');
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame('`foo` datetime NOT NULL DEFAULT "2016-01-01 00:00:00"', $column->buildCreate());
     // default + on update current
     $column->setOnUpdateCurrent();
     $this->assertSame('`foo` datetime NOT NULL DEFAULT "2016-01-01 00:00:00" ON UPDATE CURRENT_TIMESTAMP', $column->buildCreate());
     // default + on update current + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` datetime NOT NULL DEFAULT "2016-01-01 00:00:00" ON UPDATE CURRENT_TIMESTAMP COMMENT "foobar"', $column->buildCreate());
     // on update current
     $column = new DateTimeColumn('foo');
     $column->setOnUpdateCurrent();
     $this->assertSame(true, $column->isOnUpdateCurrent());
     $this->assertSame('`foo` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP', $column->buildCreate());
     // on update current + comment
     $column->setComment('foobar');
     $this->assertSame('`foo` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT "foobar"', $column->buildCreate());
     // comment
     $column = new DateTimeColumn('foo');
     $column->setComment('foobar');
     $this->assertSame(true, $column->hasComment());
     $this->assertSame('foobar', $column->getComment());
     $this->assertSame('`foo` datetime NOT NULL COMMENT "foobar"', $column->buildCreate());
     // default current
     $column = new DateTimeColumn('foo');
     $column->setDefaultCurrent();
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame(true, $column->isDefaultCurrent());
     $this->assertSame('`foo` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP', $column->buildCreate());
     $column = new DateTimeColumn('foo');
     $column->setDefault('CURRENT_TIMESTAMP');
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame(true, $column->isDefaultCurrent());
     $this->assertSame('`foo` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP', $column->buildCreate());
 }
示例#4
0
 /**
  * @dataProvider dataProviderCharTypes
  */
 public function testCharColumn(string $type, int $length)
 {
     $column = new CharColumn('foo', $type, 255);
     $this->assertSame('foo', $column->getName());
     $this->assertSame($type, $column->getType());
     $this->assertSame(255, $column->getLength());
     $this->assertSame(false, $column->isBinary());
     $this->assertSame('', $column->getCharset());
     $this->assertSame('', $column->getCollation());
     $this->assertSame(false, $column->isNullable());
     $this->assertSame(false, $column->hasDefault());
     $this->assertSame(null, $column->getDefault());
     $this->assertSame(false, $column->hasComment());
     $this->assertSame('', $column->getComment());
     $this->assertSame(sprintf('`foo` %s(255) NOT NULL', $type), $column->buildCreate());
     $this->assertSame('DROP COLUMN `foo`', $column->buildDrop());
     $this->assertSame(sprintf('ADD COLUMN `foo` %s(255) NOT NULL', $type), $column->buildAdd());
     $this->assertSame(sprintf('ADD COLUMN `foo` %s(255) NOT NULL AFTER `bar`', $type), $column->buildAdd(Column::int('bar')));
     $this->assertSame(sprintf('CHANGE COLUMN `bar` `foo` %s(255) NOT NULL', $type), $column->buildChange(Column::int('bar')));
     // binary
     $column->setBinary();
     $this->assertSame(true, $column->isBinary());
     $this->assertSame(sprintf('`foo` %s(255) BINARY NOT NULL', $type), $column->buildCreate());
     // binary + charset
     $column->setCharset('utf8');
     $this->assertSame(sprintf('`foo` %s(255) BINARY CHARACTER SET utf8 NOT NULL', $type), $column->buildCreate());
     // binary + charset + collation
     $column->setCollation('utf8_bin');
     $this->assertSame(sprintf('`foo` %s(255) BINARY CHARACTER SET utf8 COLLATE utf8_bin NOT NULL', $type), $column->buildCreate());
     // binary + charset + collation + nullable
     $column->setNullable();
     $this->assertSame(sprintf('`foo` %s(255) BINARY CHARACTER SET utf8 COLLATE utf8_bin', $type), $column->buildCreate());
     // binary + charset + collation + nullable + default
     $column->setDefault('bar');
     $this->assertSame(sprintf('`foo` %s(255) BINARY CHARACTER SET utf8 COLLATE utf8_bin DEFAULT "bar"', $type), $column->buildCreate());
     // binary + charset + collation + nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s(255) BINARY CHARACTER SET utf8 COLLATE utf8_bin DEFAULT "bar" COMMENT "foobar"', $type), $column->buildCreate());
     // charset
     $column = new CharColumn('foo', $type, 255);
     $column->setCharset('utf8');
     $this->assertSame('utf8', $column->getCharset());
     $this->assertSame(sprintf('`foo` %s(255) CHARACTER SET utf8 NOT NULL', $type), $column->buildCreate());
     // charset + collation
     $column->setCollation('utf8_bin');
     $this->assertSame(sprintf('`foo` %s(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL', $type), $column->buildCreate());
     // charset + collation + nullable
     $column->setNullable();
     $this->assertSame(sprintf('`foo` %s(255) CHARACTER SET utf8 COLLATE utf8_bin', $type), $column->buildCreate());
     // charset + collation + nullable + default
     $column->setDefault('bar');
     $this->assertSame(sprintf('`foo` %s(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT "bar"', $type), $column->buildCreate());
     // charset + collation + nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT "bar" COMMENT "foobar"', $type), $column->buildCreate());
     // collation
     $column = new CharColumn('foo', $type, 255);
     $column->setCollation('utf8_bin');
     $this->assertSame('utf8_bin', $column->getCollation());
     $this->assertSame(sprintf('`foo` %s(255) COLLATE utf8_bin NOT NULL', $type), $column->buildCreate());
     // collation + nullable
     $column->setNullable();
     $this->assertSame(sprintf('`foo` %s(255) COLLATE utf8_bin', $type), $column->buildCreate());
     // collation + nullable + default
     $column->setDefault('bar');
     $this->assertSame(sprintf('`foo` %s(255) COLLATE utf8_bin DEFAULT "bar"', $type), $column->buildCreate());
     // collation + nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s(255) COLLATE utf8_bin DEFAULT "bar" COMMENT "foobar"', $type), $column->buildCreate());
     // nullable
     $column = new CharColumn('foo', $type, 255);
     $column->setNullable();
     $this->assertSame(true, $column->isNullable());
     $this->assertSame(sprintf('`foo` %s(255)', $type), $column->buildCreate());
     // nullable + default
     $column->setDefault('bar');
     $this->assertSame(sprintf('`foo` %s(255) DEFAULT "bar"', $type), $column->buildCreate());
     // nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s(255) DEFAULT "bar" COMMENT "foobar"', $type), $column->buildCreate());
     // default
     $column = new CharColumn('foo', $type, 255);
     $column->setDefault('bar');
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame('bar', $column->getDefault());
     $this->assertSame(sprintf('`foo` %s(255) NOT NULL DEFAULT "bar"', $type), $column->buildCreate());
     // default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s(255) NOT NULL DEFAULT "bar" COMMENT "foobar"', $type), $column->buildCreate());
     // comment
     $column = new CharColumn('foo', $type, 255);
     $column->setComment('foobar');
     $this->assertSame(true, $column->hasComment());
     $this->assertSame('foobar', $column->getComment());
     $this->assertSame(sprintf('`foo` %s(255) NOT NULL COMMENT "foobar"', $type), $column->buildCreate());
 }
示例#5
0
 /**
  * @dataProvider dataProviderIntTypes
  */
 public function testIntColumn(string $type, int $signed_min, int $signed_max, int $unsigned_max)
 {
     $column = new IntColumn('foo', $type);
     $this->assertSame('foo', $column->getName());
     $this->assertSame($type, $column->getType());
     $this->assertSame(false, $column->isUnsigned());
     $this->assertSame(false, $column->isNullable());
     $this->assertSame(false, $column->isAutoIncrement());
     $this->assertSame(false, $column->hasDefault());
     $this->assertSame(null, $column->getDefault());
     $this->assertSame(false, $column->hasComment());
     $this->assertSame('', $column->getComment());
     $this->assertSame($signed_min, $column->getMin());
     $this->assertSame($signed_max, $column->getMax());
     $this->assertSame(sprintf('`foo` %s NOT NULL', $type), $column->buildCreate());
     $this->assertSame('DROP COLUMN `foo`', $column->buildDrop());
     $this->assertSame(sprintf('ADD COLUMN `foo` %s NOT NULL', $type), $column->buildAdd());
     $this->assertSame(sprintf('ADD COLUMN `foo` %s NOT NULL AFTER `bar`', $type), $column->buildAdd(Column::int('bar')));
     $this->assertSame(sprintf('CHANGE COLUMN `bar` `foo` %s NOT NULL', $type), $column->buildChange(Column::int('bar')));
     // unsigned
     $column = new IntColumn('foo', $type);
     $column->setUnsigned();
     $this->assertSame(true, $column->isUnsigned());
     $this->assertSame(0, $column->getMin());
     $this->assertSame($unsigned_max, $column->getMax());
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL', $type), $column->buildCreate());
     // unsigned + auto_increment
     $column->setAutoIncrement();
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL AUTO_INCREMENT', $type), $column->buildCreate());
     // unsigned + auto_increment + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL AUTO_INCREMENT COMMENT "foobar"', $type), $column->buildCreate());
     // unsigned + nullable
     $column = new IntColumn('foo', $type);
     $column->setUnsigned()->setNullable();
     $this->assertSame(sprintf('`foo` %s UNSIGNED', $type), $column->buildCreate());
     // unsigned + nullable + default
     $column->setDefault(1);
     $this->assertSame(sprintf('`foo` %s UNSIGNED DEFAULT "1"', $type), $column->buildCreate());
     // unsigned + nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s UNSIGNED DEFAULT "1" COMMENT "foobar"', $type), $column->buildCreate());
     // unsigned + default
     $column = new IntColumn('foo', $type);
     $column->setUnsigned()->setDefault(1);
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL DEFAULT "1"', $type), $column->buildCreate());
     // unsigned + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL DEFAULT "1" COMMENT "foobar"', $type), $column->buildCreate());
     // unsigned + comment
     $column = new IntColumn('foo', $type);
     $column->setUnsigned()->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s UNSIGNED NOT NULL COMMENT "foobar"', $type), $column->buildCreate());
     // nullable
     $column = new IntColumn('foo', $type);
     $column->setNullable();
     $this->assertSame(true, $column->isNullable());
     $this->assertSame(sprintf('`foo` %s', $type), $column->buildCreate());
     // nullable + default
     $column->setDefault(1);
     $this->assertSame(sprintf('`foo` %s DEFAULT "1"', $type), $column->buildCreate());
     // nullable + default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s DEFAULT "1" COMMENT "foobar"', $type), $column->buildCreate());
     // nullable + comment
     $column = new IntColumn('foo', $type);
     $column->setNullable()->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s COMMENT "foobar"', $type), $column->buildCreate());
     // auto_increment
     $column = new IntColumn('foo', $type);
     $column->setAutoIncrement();
     $this->assertSame(true, $column->isAutoIncrement());
     $this->assertSame(sprintf('`foo` %s NOT NULL AUTO_INCREMENT', $type), $column->buildCreate());
     // auto_increment + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s NOT NULL AUTO_INCREMENT COMMENT "foobar"', $type), $column->buildCreate());
     // default
     $column = new IntColumn('foo', $type);
     $column->setDefault(1);
     $this->assertSame(true, $column->hasDefault());
     $this->assertSame(1, $column->getDefault());
     $this->assertSame(sprintf('`foo` %s NOT NULL DEFAULT "1"', $type), $column->buildCreate());
     // default + comment
     $column->setComment('foobar');
     $this->assertSame(sprintf('`foo` %s NOT NULL DEFAULT "1" COMMENT "foobar"', $type), $column->buildCreate());
     // comment
     $column = new IntColumn('foo', $type);
     $column->setComment('foobar');
     $this->assertSame(true, $column->hasComment());
     $this->assertSame('foobar', $column->getComment());
     $this->assertSame(sprintf('`foo` %s NOT NULL COMMENT "foobar"', $type), $column->buildCreate());
 }