'bigint', 'dbType' => 'int8', 'phpType' => 'integer', 'allowNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, 'precision' => 64, 'scale' => 0, 'defaultValue' => null, ]; return $columns; } public function testCompositeFk() { $schema = $this->getConnection()->schema; $table = $schema->getTableSchema('composite_fk'); $this->assertCount(1, $table->foreignKeys); $this->assertTrue(isset($table->foreignKeys['fk_composite_fk_order_item'])); $this->assertEquals('order_item', $table->foreignKeys['fk_composite_fk_order_item'][0]); $this->assertEquals('order_id', $table->foreignKeys['fk_composite_fk_order_item']['order_id']); $this->assertEquals('item_id', $table->foreignKeys['fk_composite_fk_order_item']['item_id']); } public function testGetPDOType() { $values = [ [null, \PDO::PARAM_NULL], ['', \PDO::PARAM_STR], ['hello', \PDO::PARAM_STR], [0, \PDO::PARAM_INT], [1, \PDO::PARAM_INT], [1337, \PDO::PARAM_INT], [true, \PDO::PARAM_BOOL], [false, \PDO::PARAM_BOOL], [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB], ]; $schema = $this->getConnection()->schema; foreach ($values as $value) { $this->assertEquals($value[1], $schema->getPdoType($value[0])); } fclose($fp); } public function testBooleanDefaultValues() { $schema = $this->getConnection()->schema; $table = $schema->getTableSchema('bool_values'); $this->assertTrue($table->getColumn('default_true')->defaultValue); $this->assertFalse($table->getColumn('default_false')->defaultValue); } public function testFindSchemaNames() { $schema = $this->getConnection()->schema; $this->assertCount(3, $schema->getSchemaNames()); } public function bigintValueProvider() { return [ [8817806877], [3797444208], [3199585540], [1389831585], [922337203685477580], [9223372036854775807], [-9223372036854775808], ]; } /** * @dataProvider bigintValueProvider * @param int $bigint */ public function testBigintValue($bigint) { $this->mockApplication(); ActiveRecord::$db = $this->getConnection(); Type::deleteAll(); $type = new Type(); $type->setAttributes([ 'bigint_col' => $bigint, // whatever just to satisfy NOT NULL columns 'int_col' => 1, 'char_col' => 'a', 'float_col' => 0.1, 'bool_col' => true, ], false); $type->save(false); $actual = Type::find()->one(); $this->assertEquals($bigint, $actual->bigint_col); } /** * @see https://github.com/yiisoft/yii2/issues/12483 */ public function testParenthesisDefaultValue() { $db = $this->getConnection(false); if ($db->schema->getTableSchema('test_default_parenthesis') !== null) { $db->createCommand()->dropTable('test_default_parenthesis')->execute(); } $db->createCommand()->createTable('test_default_parenthesis', [ 'id' => 'pk', 'user_timezone' => 'numeric(5,2) DEFAULT (0)::numeric NOT NULL', ])->execute(); $db->schema->refreshTableSchema('test_default_parenthesis'); $tableSchema = $db->schema->getTableSchema('test_default_parenthesis'); $this->assertNotNull($tableSchema); $column = $tableSchema->getColumn('user_timezone'); $this->assertNotNull($column); $this->assertFalse($column->allowNull); $this->assertEquals('numeric', $column->dbType); $this->assertEquals(0, $column->defaultValue); } /** * @see https://github.com/yiisoft/yii2/issues/14192 */ public function testTimestampNullDefaultValue() { $db = $this->getConnection(false); if ($db->schema->getTableSchema('test_timestamp_default_null') !== null) { $db->createCommand()->dropTable('test_timestamp_default_null')->execute(); } $db->createCommand()->createTable('test_timestamp_default_null', [ 'id' => 'pk', 'timestamp' => 'timestamp DEFAULT NULL', ])->execute(); $db->schema->refreshTableSchema('test_timestamp_default_null'); $tableSchema = $db->schema->getTableSchema('test_timestamp_default_null'); $this->assertNull($tableSchema->getColumn('timestamp')->defaultValue); } public function constraintsProvider() { $result = parent::constraintsProvider(); $result['1: check'][2][0]->expression = '(("C_check")::text <> \'\'::text)'; $result['3: foreign key'][2][0]->foreignSchemaName = 'public'; $result['3: index'][2] = []; return $result; } }