Skip to content

Commit

Permalink
Option "signed" for table, default "true". Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimazzzZ committed Feb 1, 2016
1 parent 8ae06da commit 4abe806
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function createTable(Table $table)
$column = new Column();
$column->setName('id')
->setType('integer')
->setSigned(isset($options['signed']) ? $options['signed'] : false)
->setSigned(isset($options['signed']) ? $options['signed'] : true)
->setIdentity(true);

array_unshift($columns, $column);
Expand Down
85 changes: 85 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ public function testCreateTableWithLatin1Collate()
$this->assertEquals('latin1_general_ci', $row['Collation']);
}

public function testCreateTableWithUnsignedPK()
{
$table = new \Phinx\Db\Table('ntable', array('signed' => false), $this->adapter);
$table->addColumn('realname', 'string')
->addColumn('email', 'integer')
->save();
$this->assertTrue($this->adapter->hasTable('ntable'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'realname'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'email'));
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));
}

public function testRenameTable()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
Expand Down Expand Up @@ -779,6 +792,23 @@ public function testAddForeignKey()
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), array('ref_table_id')));
}

public function testAddForeignKeyForTableWithUnsignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', array('signed' => false), $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', array(), $this->adapter);
$table->addColumn('ref_table_id', 'integer', array('signed' => false))->save();

$fk = new \Phinx\Db\Table\ForeignKey();
$fk->setReferencedTable($refTable)
->setColumns(array('ref_table_id'))
->setReferencedColumns(array('id'));

$this->adapter->addForeignKey($table, $fk);
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), array('ref_table_id')));
}

public function testDropForeignKey()
{
$refTable = new \Phinx\Db\Table('ref_table', array(), $this->adapter);
Expand All @@ -797,6 +827,24 @@ public function testDropForeignKey()
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id')));
}

public function testDropForeignKeyForTableWithUnsignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', array('signed' => false), $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', array(), $this->adapter);
$table->addColumn('ref_table_id', 'integer', array('signed' => false))->save();

$fk = new \Phinx\Db\Table\ForeignKey();
$fk->setReferencedTable($refTable)
->setColumns(array('ref_table_id'))
->setReferencedColumns(array('id'));

$this->adapter->addForeignKey($table, $fk);
$this->adapter->dropForeignKey($table->getName(), array('ref_table_id'));
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id')));
}

public function testDropForeignKeyAsString()
{
$refTable = new \Phinx\Db\Table('ref_table', array(), $this->adapter);
Expand Down Expand Up @@ -833,6 +881,24 @@ public function testHasForeignKey()
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id2')));
}

public function testHasForeignKeyForTableWithUnsignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', array('signed' => false), $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', array(), $this->adapter);
$table->addColumn('ref_table_id', 'integer', array('signed' => false))->save();

$fk = new \Phinx\Db\Table\ForeignKey();
$fk->setReferencedTable($refTable)
->setColumns(array('ref_table_id'))
->setReferencedColumns(array('id'));

$this->adapter->addForeignKey($table, $fk);
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), array('ref_table_id')));
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id2')));
}

public function testHasForeignKeyAsString()
{
$refTable = new \Phinx\Db\Table('ref_table', array(), $this->adapter);
Expand Down Expand Up @@ -870,6 +936,25 @@ public function testHasForeignKeyWithConstraint()
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id'), 'my_constraint2'));
}

public function testHasForeignKeyWithConstraintForTableWithUnsignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', array('signed' => false), $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', array(), $this->adapter);
$table->addColumn('ref_table_id', 'integer', array('signed' => false))->save();

$fk = new \Phinx\Db\Table\ForeignKey();
$fk->setReferencedTable($refTable)
->setColumns(array('ref_table_id'))
->setConstraint("my_constraint")
->setReferencedColumns(array('id'));

$this->adapter->addForeignKey($table, $fk);
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), array('ref_table_id'), 'my_constraint'));
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), array('ref_table_id'), 'my_constraint2'));
}

public function testHasDatabase()
{
$this->assertFalse($this->adapter->hasDatabase('fake_database_name'));
Expand Down
38 changes: 38 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,44 @@ public function testCreateTablePrimaryKey()
$this->adapter->createTable($table);
}

public function testCreateTableUnsignedPK()
{
$column1 = $this->getMockBuilder('Phinx\Db\Table\Column')
->disableOriginalConstructor()
->setMethods(array( 'getName', 'getAfter', 'getType', 'getLimit', 'setLimit'))
->getMock();

$column1->expects($this->any())->method('getName')->will($this->returnValue('column_name'));
$column1->expects($this->any())->method('getType')->will($this->returnValue('string'));
$column1->expects($this->any())->method('getAfter')->will($this->returnValue(null));
$column1->expects($this->at(0))->method('getLimit')->will($this->returnValue('64'));

$column2 = $this->getMockBuilder('Phinx\Db\Table\Column')
->disableOriginalConstructor()
->setMethods(array( 'getName', 'getAfter', 'getType', 'getLimit', 'setLimit'))
->getMock();

$column2->expects($this->any())->method('getName')->will($this->returnValue('column_name2'));
$column2->expects($this->any())->method('getType')->will($this->returnValue('integer'));
$column2->expects($this->any())->method('getAfter')->will($this->returnValue(null));
$column2->expects($this->at(0))->method('getLimit')->will($this->returnValue('4'));

$table = $this->getMockBuilder('Phinx\Db\Table')
->disableOriginalConstructor()
->setMethods(array('getName', 'getOptions', 'getPendingColumns', 'getIndexes', 'getForeignKeys'))
->getMock();

$tableOptions = array('signed' => false);
$table->expects($this->any())->method('getPendingColumns')->will($this->returnValue(array($column1,$column2)));
$table->expects($this->any())->method('getName')->will($this->returnValue('table_name'));
$table->expects($this->any())->method('getOptions')->will($this->returnValue($tableOptions));
$table->expects($this->any())->method('getIndexes')->will($this->returnValue(array()));
$table->expects($this->any())->method('getForeignKeys')->will($this->returnValue(array()));

$expectedSql = 'CREATE TABLE `table_name` (`id` INT(11) unsigned NOT NULL AUTO_INCREMENT, `column_name` VARCHAR(255) NOT NULL, `column_name2` INT(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;';
$this->assertExecuteSql($expectedSql);
$this->adapter->createTable($table);
}

public function testCreateTableAdvanced()
{
Expand Down

0 comments on commit 4abe806

Please sign in to comment.