Ejemplo n.º 1
0
 public function testModifyColumn()
 {
     $schema = Yii::app()->db->schema;
     $field = new Fields('test');
     $field->modelName = $this->getTestModelName();
     $field->fieldName = $this->getTestColumnName();
     $field->type = 'varchar';
     $field->custom = 0;
     $tableName = X2Model::model($field->modelName)->tableName();
     try {
         $field->createColumn();
     } catch (Exception $e) {
         $this->tearDownTestColumn();
         throw $e;
     }
     Yii::app()->db->schema->refresh();
     $columnsAfterAdd = Yii::app()->db->schema->tables[$tableName]->columnNames;
     $column = Yii::app()->db->schema->tables[$tableName]->columns[$field->fieldName];
     $this->assertEquals('varchar(255)', $column->dbType);
     $this->assertTrue(in_array($field->fieldName, $columnsAfterAdd), "Column {$field->fieldName} was not created.");
     // test column modification
     $field->type = 'float';
     $this->assertTrue($field->modifyColumn());
     Yii::app()->db->schema->refresh();
     $column = Yii::app()->db->schema->tables[$tableName]->columns[$field->fieldName];
     $this->assertEquals('float', $column->dbType);
     // test strict mode. Try to truncate column and ensure that modifyColumn returns false in
     // indicating CDbException
     $contact = Contacts::model()->findByPk(12345);
     if (!$contact) {
         // temporary fix to get test to work in opensource. For some reason the contact
         // fixture isn't being loaded.
         $contact = new Contacts();
         $contact->setAttributes(array('id' => 12345, 'name' => 'Testfirstname Testlastname', 'nameId' => 'Testfirstname Testlastname_12345', 'company' => 'Black Mesa_1', 'firstName' => 'Testfirstname', 'lastName' => 'Testlastname', 'email' => '*****@*****.**', 'assignedTo' => 'Anyone', 'visibility' => 1, 'phone' => '(234) 918-2348', 'phone2' => '398-103-6291', 'trackingKey' => '12345678901234567890'), false);
         $contact->save();
     }
     $fieldName = $field->fieldName;
     $field->type = 'text';
     $this->assertTrue($field->modifyColumn());
     Yii::app()->db->schema->refresh();
     $column = $schema->tables[$tableName]->columns[$field->fieldName];
     $this->assertEquals('text', $column->dbType);
     $contact->refreshMetaData();
     // set field value to a string with length > 255
     $contact->{$fieldName} = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111';
     $this->assertSaves($contact);
     $field->type = 'varchar';
     Yii::app()->db->createCommand('set sql_mode=STRICT_ALL_TABLES;')->execute();
     $this->assertFalse($field->modifyColumn());
     $column = $schema->tables[$tableName]->columns[$field->fieldName];
     $this->assertEquals('text', $column->dbType);
     Yii::app()->db->createCommand('set sql_mode="";')->execute();
     $this->tearDownTestColumn();
 }