アプリケーションでグループの作成をテストしたいと考えています。そこで、チュートリアルの AR-Test をコピーして、必要に応じて変更しました。フィクスチャを追加し、 ...::model()->findAll() を呼び出すと、フィクスチャのグループが取得されます。
私のテストでは、新しいグループを作成して、グループが挿入されたことを確認したいと考えています。$this->assertTrue($group->save()) はパスしますが、var_dump($group->id) は NULL です。ここで私のテスト:
public function testCreateGroup(){
// Insert new group
$group= RightGroup::model();
$group->title = 'Create Test';
$this->assertTrue($group->save());
// Verify created and updated date
$group = RightGroup::model()->findByPk($group->id);
$this->assertTrue($group instanceof RightGroup);
}
そして、これが私のモデルです。自分で自動生成・編集しています。
class RightGroup extends CActiveRecord {
// Constants
const ADMIN_GROUP = 1;
const USER_GROUP = 2;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return RightGroup the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return 'right_groups';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title', 'required'),
array('title', 'length', 'max' => 50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, created, updated', 'safe', 'on' => 'search'),
// Set the updated value for each update
array('updated', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'update'),
// Set the created and updated values at create action
array('created,updated', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'insert')
);
}
/**
* @return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::MANY_MANY, 'User', 'right_group_users(group_id, user_id)'),
'rights' => array(self::MANY_MANY, 'Right', 'right_groups_rights(group_id, right_id)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => Yii::t('right','ID'),
'title' => Yii::t('right','Title'),
'created' => Yii::t('right','Created'),
'updated' => Yii::t('right','Updated'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('created', $this->created, true);
$criteria->compare('updated', $this->updated, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Proofs if the given right belongs to this right group.
*
* @param Right $right
* @return bool
*/
public function hasRight(Right $right) {
return in_array($right, $this->rights);
}
public function behaviors() {
return array('CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'));
}
}
私のバグはどこですか?