Page 1 of 1

First test

Posted: Mon Oct 16, 2006 12:30 am
by kpraman
Hello,

This is the first test i am writing.

//The class to be tested is//

Code: Select all

<?php


// This class does the following:

// AddOrUpdateMember method adds records or updates
// DeleteMember method deletes member
// DisableMember method disables member
// EnableMember method enables member
// GetMemberList method returns records of all members


class Members
{
       function AddOrUpdateMember($firstname,$lastname,$address,$city,$state,$zip,$country,$phone,$email,$fax,$enable,$memberid,$table)
		 {
			if(empty($memberid)){
			      //if memberid is blank a new record is inserted
			      $query=mysql_query("INSERT INTO $table SET fname='$firstname', lname='$lastname', address='$address', city='$city', state='$state', zip='$zip', country='$country', phone='$phone', email='$email', fax='$fax',enable='$enable'");
				}
			
			
			if(!empty($memberid)){
			    //if memberid is not blank, the memberid's record is updated
		        $query=mysql_query("UPDATE $table SET fname='$firstname', lname='$lastname', address='$address', city='$city', state='$state', zip='$zip', country='$country', phone='$phone', email='$email', fax='$fax',enable='$enable' WHERE memberid='$memberid'");
				}
				//on success 1 is returned
            return $query; 
		 }

	 
	     function DeleteMember($memberid, $table)
	      {
		    //memberid's record is deleted
		    $query=mysql_query("DELETE FROM $table WHERE memberid='$memberid'");
				//on success 1 is returned
			return $query;	
		  }

		    //disables a member
	     function DisableMember($memberid, $table)
		  {
		    $query=mysql_query("UPDATE $table SET enable='false' WHERE memberid='$memberid'");
             //on success 1 is returned
			return $query;
		  }
		
	 
	    function EnableMember($memberid, $table)
	      {
		    //enables a member
		    $query=mysql_query("UPDATE $table SET enable='true' WHERE memberid='$memberid'");
           //on success 1 is returned
			return $query;
		  }
		
		
	    function GetMemberList($table)
	      {
		  //returns all records
		  $arr=array();
		   $query=mysql_query("SELECT * FROM $table");
			while($row=mysql_fetch_array($query))
			  {
			     array_push($arr,$row);
			  }
			 return $arr;
		 }
}
?>
Can anyone tell me, how to write testcases for the above class.


Regards,

kpraman

Posted: Mon Oct 16, 2006 7:28 pm
by Ambush Commander
Hmm... you'd have to go through considerable gymnastics to get that to work without factoring out the db access into its own object.

Posted: Mon Oct 16, 2006 8:17 pm
by kpraman
since this is the first time i am writing testcase, i just wanted to know the procedures.
Atleast for the first function. I am also getting error..Warning: require_once(PEAR/Config.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\lib\PHPUnit2\Runner\StandardTestSuiteLoader.php on line 52

Posted: Tue Oct 17, 2006 12:24 am
by Chris Corbyn
Like AC says; Unit Testing that will force some initial refactoring on you before you even begin. You need to separate the Database access component into it's own layer rather than directly using the mysql..() functions in the class. By doing that you can "mock" the database and test without actually needing a connection to MySQL.

Tried myself

Posted: Tue Oct 17, 2006 6:08 am
by kpraman
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
set_include_path("library/");

require_once 'members.php';
require_once 'PHPUnit/Framework/TestCase.php'; 
require_once 'PHPUnit.php';

class MembersTest extends PHPUnit_TestCase {


	 
	 function test_insert()
	    {
            $new=new Members;
            $dbActual=$new->AddOrUpdateMember('a','b','c','d','e','f','g','h','i','j','k','','table1');
			$dbExpected=1;
			$this->assertTrue($dbExpected=="$dbActual", "INSERT FAILED");
		}
	 
 }

 $suite  = new PHPUnit_TestSuite('MembersTest');
 $result = PHPUnit::run($suite);
 print $result->toHTML();
 ?>

is there anything wrong in the code? I also want to know, if i change the function name test_insert(), it does not work. If i want to write a new function, what should i do?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]