|
| 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\pgsql\type; |
| 9 | + |
| 10 | +use yii\db\pgsql\Schema; |
| 11 | +use yiiunit\framework\db\DatabaseTestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * @group db |
| 15 | + * @group pgsql |
| 16 | + */ |
| 17 | +class BooleanTest extends DatabaseTestCase |
| 18 | +{ |
| 19 | + protected $driverName = 'pgsql'; |
| 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' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 36 | + ] |
| 37 | + )->execute(); |
| 38 | + |
| 39 | + // test type `boolean` |
| 40 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 41 | + $this->assertSame('boolean', $column->phpType); |
| 42 | + |
| 43 | + // test value `false` |
| 44 | + $db->createCommand()->insert($tableName, ['bool_col' => false])->execute(); |
| 45 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 46 | + $this->assertEquals(0, $boolValue); |
| 47 | + |
| 48 | + // test php typecast |
| 49 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 50 | + $this->assertFalse($phpTypeCast); |
| 51 | + |
| 52 | + // test value `true` |
| 53 | + $db->createCommand()->insert($tableName, ['bool_col' => true])->execute(); |
| 54 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar(); |
| 55 | + $this->assertEquals(1, $boolValue); |
| 56 | + |
| 57 | + // test php typecast |
| 58 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 59 | + $this->assertTrue($phpTypeCast); |
| 60 | + } |
| 61 | + |
| 62 | + public function testBooleanWithValueInteger() |
| 63 | + { |
| 64 | + $db = $this->getConnection(true); |
| 65 | + $schema = $db->getSchema(); |
| 66 | + $tableName = '{{%boolean}}'; |
| 67 | + |
| 68 | + if ($db->getTableSchema($tableName)) { |
| 69 | + $db->createCommand()->dropTable($tableName)->execute(); |
| 70 | + } |
| 71 | + |
| 72 | + $db->createCommand()->createTable( |
| 73 | + $tableName, |
| 74 | + [ |
| 75 | + 'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), |
| 76 | + 'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 77 | + ] |
| 78 | + )->execute(); |
| 79 | + |
| 80 | + // test type `boolean` |
| 81 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 82 | + $this->assertSame('boolean', $column->phpType); |
| 83 | + |
| 84 | + // test value `0` |
| 85 | + $db->createCommand()->insert($tableName, ['bool_col' => 0])->execute(); |
| 86 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 87 | + $this->assertEquals(0, $boolValue); |
| 88 | + |
| 89 | + // test php typecast |
| 90 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 91 | + $this->assertFalse($phpTypeCast); |
| 92 | + |
| 93 | + // test value `1` |
| 94 | + $db->createCommand()->insert($tableName, ['bool_col' => 1])->execute(); |
| 95 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar(); |
| 96 | + $this->assertEquals(1, $boolValue); |
| 97 | + |
| 98 | + // test php typecast |
| 99 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 100 | + $this->assertTrue($phpTypeCast); |
| 101 | + } |
| 102 | + |
| 103 | + public function testBooleanWithValueNegative() |
| 104 | + { |
| 105 | + $db = $this->getConnection(true); |
| 106 | + $schema = $db->getSchema(); |
| 107 | + $tableName = '{{%boolean}}'; |
| 108 | + |
| 109 | + if ($db->getTableSchema($tableName)) { |
| 110 | + $db->createCommand()->dropTable($tableName)->execute(); |
| 111 | + } |
| 112 | + |
| 113 | + $db->createCommand()->createTable( |
| 114 | + $tableName, |
| 115 | + [ |
| 116 | + 'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), |
| 117 | + 'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 118 | + ] |
| 119 | + )->execute(); |
| 120 | + |
| 121 | + // test type `boolean` |
| 122 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 123 | + $this->assertSame('boolean', $column->phpType); |
| 124 | + |
| 125 | + // test value `-1` |
| 126 | + $db->createCommand()->insert($tableName, ['bool_col' => '-1'])->execute(); |
| 127 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 128 | + $this->assertEquals(1, $boolValue); |
| 129 | + |
| 130 | + // test php typecast |
| 131 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 132 | + $this->assertTrue($phpTypeCast); |
| 133 | + } |
| 134 | + |
| 135 | + public function testBooleanWithValueNull() |
| 136 | + { |
| 137 | + $db = $this->getConnection(true); |
| 138 | + $schema = $db->getSchema(); |
| 139 | + $tableName = '{{%boolean}}'; |
| 140 | + |
| 141 | + if ($db->getTableSchema($tableName)) { |
| 142 | + $db->createCommand()->dropTable($tableName)->execute(); |
| 143 | + } |
| 144 | + |
| 145 | + $db->createCommand()->createTable( |
| 146 | + $tableName, |
| 147 | + [ |
| 148 | + 'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), |
| 149 | + 'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 150 | + ] |
| 151 | + )->execute(); |
| 152 | + |
| 153 | + // test type `boolean` |
| 154 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 155 | + $this->assertSame('boolean', $column->phpType); |
| 156 | + |
| 157 | + // test value `null` |
| 158 | + $db->createCommand()->insert($tableName, ['bool_col' => null])->execute(); |
| 159 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 160 | + $this->assertNull($boolValue); |
| 161 | + |
| 162 | + // test php typecast |
| 163 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 164 | + $this->assertNull($phpTypeCast); |
| 165 | + } |
| 166 | + |
| 167 | + public function testBooleanWithValueOverflow() |
| 168 | + { |
| 169 | + $db = $this->getConnection(true); |
| 170 | + $schema = $db->getSchema(); |
| 171 | + $tableName = '{{%boolean}}'; |
| 172 | + |
| 173 | + if ($db->getTableSchema($tableName)) { |
| 174 | + $db->createCommand()->dropTable($tableName)->execute(); |
| 175 | + } |
| 176 | + |
| 177 | + $db->createCommand()->createTable( |
| 178 | + $tableName, |
| 179 | + [ |
| 180 | + 'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), |
| 181 | + 'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 182 | + ] |
| 183 | + )->execute(); |
| 184 | + |
| 185 | + // test type `boolean` |
| 186 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 187 | + $this->assertSame('boolean', $column->phpType); |
| 188 | + |
| 189 | + // test value `2` |
| 190 | + $db->createCommand()->insert($tableName, ['bool_col' => 2])->execute(); |
| 191 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 192 | + $this->assertEquals(1, $boolValue); |
| 193 | + |
| 194 | + // test php typecast |
| 195 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 196 | + $this->assertTrue($phpTypeCast); |
| 197 | + } |
| 198 | + |
| 199 | + public function testBooleanWithValueString() |
| 200 | + { |
| 201 | + $db = $this->getConnection(true); |
| 202 | + $schema = $db->getSchema(); |
| 203 | + $tableName = '{{%boolean}}'; |
| 204 | + |
| 205 | + if ($db->getTableSchema($tableName)) { |
| 206 | + $db->createCommand()->dropTable($tableName)->execute(); |
| 207 | + } |
| 208 | + |
| 209 | + $db->createCommand()->createTable( |
| 210 | + $tableName, |
| 211 | + [ |
| 212 | + 'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), |
| 213 | + 'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN), |
| 214 | + ] |
| 215 | + )->execute(); |
| 216 | + |
| 217 | + // test type `boolean` |
| 218 | + $column = $db->getTableSchema($tableName)->getColumn('bool_col'); |
| 219 | + $this->assertSame('boolean', $column->phpType); |
| 220 | + |
| 221 | + // test value `0` |
| 222 | + $db->createCommand()->insert($tableName, ['bool_col' => '0'])->execute(); |
| 223 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar(); |
| 224 | + $this->assertEquals(0, $boolValue); |
| 225 | + |
| 226 | + // test php typecast |
| 227 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 228 | + $this->assertFalse($phpTypeCast); |
| 229 | + |
| 230 | + // test value `1` |
| 231 | + $db->createCommand()->insert($tableName, ['bool_col' => '1'])->execute(); |
| 232 | + $boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar(); |
| 233 | + $this->assertEquals(1, $boolValue); |
| 234 | + |
| 235 | + // test php typecast |
| 236 | + $phpTypeCast = $column->phpTypecast($boolValue); |
| 237 | + $this->assertTrue($phpTypeCast); |
| 238 | + } |
| 239 | +} |
0 commit comments