First test

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

First test

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Tried myself

Post 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]
Post Reply