Reputation: 11
In below code a feedback form takes input for 3 fields like name, email id & comments. So validation needs to be tested using phpunit test. please help on this. I am new to php syntax altogether. how to assert & check valdiation to give report in phunit testing.
if($_POST){
//echo $sRc;
//print "<pre>";print_r($_POST);exit;
require_once(CLASSPATH."/utility.class.php");
$oUtility = new utility();
$sRc = base64_decode($_COOKIE['Rc_cf']);
$sEmail = trim($_POST['emailid']);
$scomment = trim($_POST['comment']);
$syour_name = trim($_POST['your_name']);
$sOthereSubject = trim($_POST['subject']);
$sName = trim(strip_tags($oUtility->nonxmlcharreplace($_REQUEST['your_name'])));
$sEmail = trim(strip_tags($oUtility->nonxmlcharreplace($_REQUEST['emailid'])));
$sComments = trim(strip_tags($oUtility->nonxmlcharreplace($_REQUEST['comment'])));
$iError=0;
$sErrorMsg="";
if(strlen($sName)==0){
$sErrorMsg="Your name cannot be blank.";
$iError=1;
} else if(strlen($sEmail)==0){
$sErrorMsg="Email address cannot be blank.";
$iError=1;
}else if(strlen($sEmail)>0){
$iValid = $oUtility->isValidEmail($sEmail);
if($iValid!=1){
$sErrorMsg="Please enter valid email address.";
$iError=1;
}
}
if(empty($sComments) && strlen($sErrorMsg)==0){
$sErrorMsg="Comment cannot be blank.";
$iError=1;
}
if(strlen($sErrorMsg)==0){
require_once(CLASSPATH."feedback.class.php");
$oFeedback = new feedback();
if($iError==0){
$aParameters=Array('name'=>$sName,'email'=>$sEmail,'comments'=>$sComments,'cdate'=>'now()', 'is_active'=>1,'cdate'=>'now()','udate'=>'now()');
$iRes = $oFeedback->addUpdFeedback($aParameters);
$sInserMsg = 1;
$sEmail = $sName = $scomment='';
}
}
Upvotes: 1
Views: 7118
Reputation: 6746
Your method should be hard to test because it's too long. To build your test you have to follow three steps :
The goal of unit testing is to test small things, unit things. So in your case it's very hard to write your test because it's hard to define a different input $_POST
in a test context. It's also hard because your function do a lot of different things.
So you have to cut your big function into smaller ones and just test this functions one by one.
For example, something like that :
$errorsArray = $this->validation($_POST);
if (count($errorsArray) == 0) {
require_once(CLASSPATH."feedback.class.php");
$oFeedback = new feedback();
$aParameters=Array('name'=>$sName,'email'=>$sEmail,'comments'=>$sComments,'cdate'=>'now()', 'is_active'=>1,'cdate'=>'now()','udate'=>'now()');
$iRes = $oFeedback->addUpdFeedback($aParameters);
$sInserMsg = 1;
$sEmail = $sName = $
}
//
public static function validation($input)
{
//Your validation here, return an array of error messages
}
Then, you will just have to write somme test cases like this ones :
public function testValidationOk()
{
$input = array('name' => 'John', 'email' => '[email protected]');
$errorsArray = MyClass::validation($input);
assertCount(0, $errorsArray);
}
public function testValidationFailWithoutName()
{
$input = array('name' => '', 'email' => '[email protected]');
$errorsArray = MyClass::validation($input);
assertCount(1, $errorsArray);
}
public function testValidationFailWithInvalidEmail()
{
$input = array('name' => 'John', 'email' => 'john.com');
$errorsArray = MyClass::validation($input);
assertCount(1, $errorsArray);
}
The rest of your function will be tested in other tests cases. Small methods is the key of testability, readability and maintainability.
Upvotes: 1