Skip to content

Commit 9476b84

Browse files
authored
Merge pull request #20045 from terabytesoftw/fix-type-boolean-mysql
Fix type boolean `MYSQL`.
2 parents 7005d27 + b40de6a commit 9476b84

File tree

4 files changed

+256
-2
lines changed

4 files changed

+256
-2
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Yii Framework 2 Change Log
33

44
2.0.50 under development
55
------------------------
6+
- Bug #20045: Fix type `boolean` in `MySQL` (terabytesoftw)
67
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
78
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
89
- Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir)

framework/db/mysql/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ protected function loadColumnSchema($info)
279279
if (isset($values[1])) {
280280
$column->scale = (int) $values[1];
281281
}
282-
if ($column->size === 1 && $type === 'bit') {
282+
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
283283
$column->type = 'boolean';
284284
} elseif ($type === 'bit') {
285285
if ($column->size > 32) {

tests/framework/db/mysql/SchemaTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ public function getExpectedColumns()
228228
]
229229
);
230230

231+
$columns['bool_col']['type'] = 'boolean';
232+
$columns['bool_col']['phpType'] = 'boolean';
233+
$columns['bool_col2']['type'] = 'boolean';
234+
$columns['bool_col2']['phpType'] = 'boolean';
235+
231236
if (version_compare($version, '5.7', '<')) {
232237
$columns['int_col3']['phpType'] = 'string';
233-
234238
$columns['json_col']['type'] = 'text';
235239
$columns['json_col']['dbType'] = 'longtext';
236240
$columns['json_col']['phpType'] = 'string';
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
<?php
2+
/**
3+
* @link https://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license https://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yiiunit\framework\db\mysql\type;
9+
10+
use yii\db\mysql\Schema;
11+
use yiiunit\framework\db\DatabaseTestCase;
12+
13+
/**
14+
* @group db
15+
* @group mysql
16+
*/
17+
class BooleanTest extends DatabaseTestCase
18+
{
19+
protected $driverName = 'mysql';
20+
21+
public function testBoolean()
22+
{
23+
$db = $this->getConnection(true);
24+
$schema = $db->getSchema();
25+
$tableName = '{{%boolean}}';
26+
27+
if ($db->getTableSchema($tableName)) {
28+
$db->createCommand()->dropTable($tableName)->execute();
29+
}
30+
31+
$db->createCommand()->createTable(
32+
$tableName,
33+
[
34+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
35+
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
36+
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
37+
]
38+
)->execute();
39+
40+
// test type `boolean`
41+
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
42+
$this->assertSame('boolean', $columnBoolColTinyint->phpType);
43+
44+
$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
45+
$this->assertSame('boolean', $columnBoolColBit->phpType);
46+
47+
// test value `false`
48+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => false, 'bool_col_bit' => false])->execute();
49+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
50+
$this->assertEquals(0, $boolValues['bool_col_tinyint']);
51+
$this->assertEquals(0, $boolValues['bool_col_bit']);
52+
53+
// test php typecast
54+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
55+
$this->assertFalse($phpTypeCastBoolColTinyint);
56+
57+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
58+
$this->assertFalse($phpTypeCastBoolColBit);
59+
60+
// test value `true`
61+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => true, 'bool_col_bit' => true])->execute();
62+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 2")->queryOne();
63+
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
64+
$this->assertEquals(1, $boolValues['bool_col_bit']);
65+
66+
// test php typecast
67+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
68+
$this->assertTrue($phpTypeCastBoolColTinyint);
69+
70+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
71+
$this->assertTrue($phpTypeCastBoolColBit);
72+
}
73+
74+
public function testBooleanWithValueInteger()
75+
{
76+
$db = $this->getConnection(true);
77+
$schema = $db->getSchema();
78+
$tableName = '{{%boolean}}';
79+
80+
if ($db->getTableSchema($tableName)) {
81+
$db->createCommand()->dropTable($tableName)->execute();
82+
}
83+
84+
$db->createCommand()->createTable(
85+
$tableName,
86+
[
87+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
88+
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
89+
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
90+
]
91+
)->execute();
92+
93+
// test type `boolean`
94+
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
95+
$this->assertSame('boolean', $columnBoolColTinyint->phpType);
96+
97+
$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
98+
$this->assertSame('boolean', $columnBoolColBit->phpType);
99+
100+
// test value `0`
101+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 0, 'bool_col_bit' => 0])->execute();
102+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
103+
$this->assertEquals(0, $boolValues['bool_col_tinyint']);
104+
$this->assertEquals(0, $boolValues['bool_col_bit']);
105+
106+
// test php typecast
107+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
108+
$this->assertFalse($phpTypeCastBoolColTinyint);
109+
110+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
111+
$this->assertFalse($phpTypeCastBoolColBit);
112+
113+
// test value `1`
114+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 1, 'bool_col_bit' => 1])->execute();
115+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 2")->queryOne();
116+
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
117+
$this->assertEquals(1, $boolValues['bool_col_bit']);
118+
119+
// test php typecast
120+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
121+
$this->assertTrue($phpTypeCastBoolColTinyint);
122+
123+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
124+
$this->assertTrue($phpTypeCastBoolColBit);
125+
}
126+
127+
public function testBooleanWithValueNegative()
128+
{
129+
$db = $this->getConnection(true);
130+
$schema = $db->getSchema();
131+
$tableName = '{{%boolean}}';
132+
133+
if ($db->getTableSchema($tableName)) {
134+
$db->createCommand()->dropTable($tableName)->execute();
135+
}
136+
137+
$db->createCommand()->createTable(
138+
$tableName,
139+
[
140+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
141+
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
142+
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
143+
]
144+
)->execute();
145+
146+
// test type `boolean`
147+
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
148+
$this->assertSame('boolean', $columnBoolColTinyint->phpType);
149+
150+
$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
151+
$this->assertSame('boolean', $columnBoolColBit->phpType);
152+
153+
// test value `-1`
154+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => -1, 'bool_col_bit' => -1])->execute();
155+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
156+
157+
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
158+
$this->assertEquals(1, $boolValues['bool_col_bit']);
159+
160+
// test php typecast
161+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
162+
$this->assertTrue($phpTypeCastBoolColTinyint);
163+
164+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
165+
$this->assertTrue($phpTypeCastBoolColBit);
166+
}
167+
168+
public function testBooleanWithValueNull()
169+
{
170+
$db = $this->getConnection(true);
171+
$schema = $db->getSchema();
172+
$tableName = '{{%boolean}}';
173+
174+
if ($db->getTableSchema($tableName)) {
175+
$db->createCommand()->dropTable($tableName)->execute();
176+
}
177+
178+
$db->createCommand()->createTable(
179+
$tableName,
180+
[
181+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
182+
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
183+
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
184+
]
185+
)->execute();
186+
187+
// test type `boolean`
188+
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
189+
$this->assertSame('boolean', $columnBoolColTinyint->phpType);
190+
191+
$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
192+
$this->assertSame('boolean', $columnBoolColBit->phpType);
193+
194+
// test value `null`
195+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => null, 'bool_col_bit' => null])->execute();
196+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
197+
198+
$this->assertNull($boolValues['bool_col_tinyint']);
199+
$this->assertNull($boolValues['bool_col_bit']);
200+
201+
// test php typecast
202+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
203+
$this->assertNull($phpTypeCastBoolColTinyint);
204+
205+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
206+
$this->assertNull($phpTypeCastBoolColBit);
207+
}
208+
209+
public function testBooleanWithValueOverflow()
210+
{
211+
$db = $this->getConnection(true);
212+
$schema = $db->getSchema();
213+
$tableName = '{{%boolean}}';
214+
215+
if ($db->getTableSchema($tableName)) {
216+
$db->createCommand()->dropTable($tableName)->execute();
217+
}
218+
219+
$db->createCommand()->createTable(
220+
$tableName,
221+
[
222+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
223+
'bool_col_tinyint' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
224+
'bool_col_bit' => $schema->createColumnSchemaBuilder('bit', 1),
225+
]
226+
)->execute();
227+
228+
// test type `boolean`
229+
$columnBoolColTinyint = $db->getTableSchema($tableName)->getColumn('bool_col_tinyint');
230+
$this->assertSame('boolean', $columnBoolColTinyint->phpType);
231+
232+
$columnBoolColBit = $db->getTableSchema($tableName)->getColumn('bool_col_bit');
233+
$this->assertSame('boolean', $columnBoolColBit->phpType);
234+
235+
// test value `2`
236+
$db->createCommand()->insert($tableName, ['bool_col_tinyint' => 2, 'bool_col_bit' => 2])->execute();
237+
$boolValues = $db->createCommand("SELECT * FROM $tableName WHERE id = 1")->queryOne();
238+
239+
$this->assertEquals(1, $boolValues['bool_col_tinyint']);
240+
$this->assertEquals(1, $boolValues['bool_col_bit']);
241+
242+
// test php typecast
243+
$phpTypeCastBoolColTinyint = $columnBoolColTinyint->phpTypecast($boolValues['bool_col_tinyint']);
244+
$this->assertTrue($phpTypeCastBoolColTinyint);
245+
246+
$phpTypeCastBoolColBit = $columnBoolColBit->phpTypecast($boolValues['bool_col_bit']);
247+
$this->assertTrue($phpTypeCastBoolColBit);
248+
}
249+
}

0 commit comments

Comments
 (0)