Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type boolean MYSQL. #20045

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Yii Framework 2 Change Log

2.0.50 under development
------------------------
- Bug #20045: Fix type `boolean` in `MySQL` (terabytesoftw)
samdark marked this conversation as resolved.
Show resolved Hide resolved
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
- Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir)
Expand Down
2 changes: 1 addition & 1 deletion framework/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected function loadColumnSchema($info)
if (isset($values[1])) {
$column->scale = (int) $values[1];
}
if ($column->size === 1 && $type === 'bit') {
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
$column->type = 'boolean';
} elseif ($type === 'bit') {
if ($column->size > 32) {
Expand Down
6 changes: 5 additions & 1 deletion tests/framework/db/mysql/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ public function getExpectedColumns()
]
);

$columns['bool_col']['type'] = 'boolean';
$columns['bool_col']['phpType'] = 'boolean';
$columns['bool_col2']['type'] = 'boolean';
$columns['bool_col2']['phpType'] = 'boolean';

if (version_compare($version, '5.7', '<')) {
$columns['int_col3']['phpType'] = 'string';

$columns['json_col']['type'] = 'text';
$columns['json_col']['dbType'] = 'longtext';
$columns['json_col']['phpType'] = 'string';
Expand Down
249 changes: 249 additions & 0 deletions tests/framework/db/mysql/type/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yiiunit\framework\db\mysql\type;

use yii\db\mysql\Schema;
use yiiunit\framework\db\DatabaseTestCase;

/**
* @group db
* @group mysql
*/
class BooleanTest extends DatabaseTestCase
{
protected $driverName = 'mysql';

public function testBoolean()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableName = '{{%boolean}}';

if ($db->getTableSchema($tableName)) {
$db->createCommand()->dropTable($tableName)->execute();
}

$db->createCommand()->createTable(
$tableName,
[
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
]
)->execute();

// test type `boolean`
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
$this->assertSame('boolean', $columnBoolColTinyint->phpType);

$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
$this->assertSame('boolean', $columnBoolColBit->phpType);

// test value `false`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => false, 'bool_col_bit' => false])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
$this->assertEquals(0, $boolValues['bool_col_tinyint']);
$this->assertEquals(0, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertFalse($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertFalse($phpTypeCastBoolColBit);

// test value `true`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => true, 'bool_col_bit' => true])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 2")->queryOne();
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
$this->assertEquals(1, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertTrue($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertTrue($phpTypeCastBoolColBit);
}

public function testBooleanWithValueInteger()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableName = '{{%boolean}}';

if ($db->getTableSchema($tableName)) {
$db->createCommand()->dropTable($tableName)->execute();
}

$db->createCommand()->createTable(
$tableName,
[
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
]
)->execute();

// test type `boolean`
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
$this->assertSame('boolean', $columnBoolColTinyint->phpType);

$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
$this->assertSame('boolean', $columnBoolColBit->phpType);

// test value `0`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 0, 'bool_col_bit' => 0])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
$this->assertEquals(0, $boolValues['bool_col_tinyint']);
$this->assertEquals(0, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertFalse($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertFalse($phpTypeCastBoolColBit);

// test value `1`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 1, 'bool_col_bit' => 1])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 2")->queryOne();
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
$this->assertEquals(1, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertTrue($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertTrue($phpTypeCastBoolColBit);
}

public function testBooleanWithValueNegative()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableName = '{{%boolean}}';

if ($db->getTableSchema($tableName)) {
$db->createCommand()->dropTable($tableName)->execute();
}

$db->createCommand()->createTable(
$tableName,
[
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
]
)->execute();

// test type `boolean`
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
$this->assertSame('boolean', $columnBoolColTinyint->phpType);

$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
$this->assertSame('boolean', $columnBoolColBit->phpType);

// test value `-1`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => -1, 'bool_col_bit' => -1])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();

$this->assertEquals(1, $boolValues['bool_col_tinyint']);
$this->assertEquals(1, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertTrue($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertTrue($phpTypeCastBoolColBit);
}

public function testBooleanWithValueNull()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableName = '{{%boolean}}';

if ($db->getTableSchema($tableName)) {
$db->createCommand()->dropTable($tableName)->execute();
}

$db->createCommand()->createTable(
$tableName,
[
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
]
)->execute();

// test type `boolean`
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
$this->assertSame('boolean', $columnBoolColTinyint->phpType);

$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
$this->assertSame('boolean', $columnBoolColBit->phpType);

// test value `null`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => null, 'bool_col_bit' => null])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();

$this->assertNull($boolValues['bool_col_tinyint']);
$this->assertNull($boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertNull($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertNull($phpTypeCastBoolColBit);
}

public function testBooleanWithValueOverflow()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableName = '{{%boolean}}';

if ($db->getTableSchema($tableName)) {
$db->createCommand()->dropTable($tableName)->execute();
}

$db->createCommand()->createTable(
$tableName,
[
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
]
)->execute();

// test type `boolean`
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
$this->assertSame('boolean', $columnBoolColTinyint->phpType);

$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
$this->assertSame('boolean', $columnBoolColBit->phpType);

// test value `2`
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 2, 'bool_col_bit' => 2])->execute();
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();

$this->assertEquals(1, $boolValues['bool_col_tinyint']);
$this->assertEquals(1, $boolValues['bool_col_bit']);

// test php typecast
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
$this->assertTrue($phpTypeCastBoolColTinyint);

$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
$this->assertTrue($phpTypeCastBoolColBit);
}
}
Loading