From 3497c146947d113c160d700d96e4a10e9a76c8ae Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 24 Oct 2023 18:29:50 -0300 Subject: [PATCH] Add more tests. --- tests/framework/db/mssql/type/BooleanTest.php | 132 ++++++++++++++++-- 1 file changed, 121 insertions(+), 11 deletions(-) diff --git a/tests/framework/db/mssql/type/BooleanTest.php b/tests/framework/db/mssql/type/BooleanTest.php index bb5f33893bc..63a8a529bf8 100644 --- a/tests/framework/db/mssql/type/BooleanTest.php +++ b/tests/framework/db/mssql/type/BooleanTest.php @@ -40,8 +40,8 @@ public function testBoolean(): void $column = $db->getTableSchema($tableName)->getColumn('bool_col'); $this->assertSame('boolean', $column->phpType); - // test value 0 - $db->createCommand()->insert($tableName, ['bool_col' => 0])->execute(); + // test value `false` + $db->createCommand()->insert($tableName, ['bool_col' => false])->execute(); $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); $this->assertEquals(0, $boolValue); @@ -49,40 +49,150 @@ public function testBoolean(): void $phpTypeCast = $column->phpTypecast($boolValue); $this->assertFalse($phpTypeCast); - // test value 1 - $db->createCommand()->insert($tableName, ['bool_col' => 1])->execute(); + // test value `true` + $db->createCommand()->insert($tableName, ['bool_col' => true])->execute(); $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar(); $this->assertEquals(1, $boolValue); // test php typecast $phpTypeCast = $column->phpTypecast($boolValue); $this->assertTrue($phpTypeCast); + } - // test value `false` - $db->createCommand()->insert($tableName, ['bool_col' => false])->execute(); - $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 3")->queryScalar(); + public function testBooleanWithValueInteger(): void + { + $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' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), + ] + )->execute(); + + // test type + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); + $this->assertSame('boolean', $column->phpType); + + // test value 0 + $db->createCommand()->insert($tableName, ['bool_col' => 0])->execute(); + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); $this->assertEquals(0, $boolValue); // test php typecast $phpTypeCast = $column->phpTypecast($boolValue); $this->assertFalse($phpTypeCast); - // test value `true` - $db->createCommand()->insert($tableName, ['bool_col' => true])->execute(); - $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 4")->queryScalar(); + // test value 1 + $db->createCommand()->insert($tableName, ['bool_col' => 1])->execute(); + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar(); $this->assertEquals(1, $boolValue); // test php typecast $phpTypeCast = $column->phpTypecast($boolValue); $this->assertTrue($phpTypeCast); + } + + public function testBooleanValueNegative(): void + { + $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' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), + ] + )->execute(); + + // test type + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); + $this->assertSame('boolean', $column->phpType); + + // test value 2 + $db->createCommand()->insert($tableName, ['bool_col' => -1])->execute(); + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); + $this->assertEquals(1, $boolValue); + + // test php typecast + $phpTypeCast = $column->phpTypecast($boolValue); + $this->assertTrue($phpTypeCast); + } + + public function testBooleanWithValueNull(): void + { + $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' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), + ] + )->execute(); + + // test type + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); + $this->assertSame('boolean', $column->phpType); // test value `null` $db->createCommand()->insert($tableName, ['bool_col' => null])->execute(); - $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 5")->queryScalar(); + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); $this->assertNull($boolValue); // test php typecast $phpTypeCast = $column->phpTypecast($boolValue); $this->assertNull($phpTypeCast); } + + public function testBooleanWithValueOverflow(): void + { + $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' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), + ] + )->execute(); + + // test type + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); + $this->assertSame('boolean', $column->phpType); + + // test value 2 + $db->createCommand()->insert($tableName, ['bool_col' => 2])->execute(); + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); + $this->assertEquals(1, $boolValue); + + // test php typecast + $phpTypeCast = $column->phpTypecast($boolValue); + $this->assertTrue($phpTypeCast); + } }