What Is The Best Way To Test Inline Validator

I’m still new about this Unit Testing in PHP & Yii.

let’s say I have this model




/**

 * The table fields.

 * @param integer $postId

 * @param integer $isActive

 * @param integer $isCommentable

 */

class Comment extends CActiveRecord {


   public function rules(){

     array('postId', 'validatePostIsActiveAndCommentable'),

   }


   public function validatePostIsActiveAndCommentable($attribute, $params){

     $postId = $this->$attribute;

     if (Post::model()->active()->count('id = :id', array(':id' => $postId)){

       $this->addError($attribute, Yii::t('translations', '{attribute} is not active'));

     } else (Post::model()->commentable()->count('id = :id', array(':id' => $postId)){

       $this->addError($attribute, Yii::t('translations', '{attribute} is not commentable'));

     }

     //or here we can select the isActive and isCommentable attribute by primary key and compare with the appropriate value to minimize the query.

   }

}



So if I want to test the method validatePostIsActiveAndCommentable in the Unit Testing, how to differentiate if the error is from the first condition or the second condition?

Right now what I usually do in the test method is using hasErrors('attributeName').




class CommentTest extends CDbTestCase {


   public function testValidatePostIsActiveAndCommentableShouldSucceedWhenPostIsActiveAndCommentable(){

      //bla bla

      $this->assertTrue($comment->validate(array('propertyId')));

   }


   public function testValidatePostIsActiveAndCommentableShouldFailWhenPostIsNotActive(){

      //bla bla

      $this->assertFalse($comment->validate(array('propertyId')));

      $this->assertTrue(in_array(Yii::t('translations', '{attribute} is not active'), $comment->getErrors('propertyId')));

   }


   public function testValidatePostIsActiveAndCommentableShouldFailWhenPostIsActiveButNotCommentable(){

      //bla bla

      $this->assertFalse($comment->validate(array('propertyId')));

      $this->assertTrue(in_array(Yii::t('translations', '{attribute} is not commentable'), $comment->getErrors('propertyId')));

   }

}



I’m just curious if there is a more common way to do this.

I have the same question, anyone to help here?