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")
<?php
public function testing_insertData()
{
$new=new MemberTypes;
$new->AddOrUpdateMemberType('manager','Head of the','true','','membertype');
echo $memid=mysql_insert_id();
$resExpected=array((string)$memid,'manager','Head of the','true');
$query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$memid");
$resActual=mysql_fetch_row($query);
$this->assertEquals($resExpected,$resActual, "COULD NOT INSERT DATA.");
}
public function testing_insertNotable()
{
$new=new MemberTypes;
$new->AddOrUpdateMemberType('manager','Head of the','true','','');
echo $memid=mysql_insert_id();
$query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$memid");
$resActual=mysql_fetch_row($query);
$this->assertEquals(0,sizeof($this->resActual), "COULD NOT INSERT DATA.");
}
?>
The value of the first method's $memid gets assigned to second method's $memid. I do not want to change $memid name. How to restrict this?
Last edited by kpraman on Tue Oct 31, 2006 3:15 am, edited 1 time in total.
methods should be public, the variables inside it should private. I should be able to give same variable name and they should not be overwritten by their previous values.
My guess is that you're looking at it the wrong way. $memid is being assigned the value of mysqli_insert_id(). Assuming you have not created a new record, mysqli_insert_id() will continue providing the primary key of the last insert over, and over, and over again until you make a new insert.
Your AddOrUpdateMemberType function might be coopted here - have it return mysqli_insert_id() or false depending on whether something is added, or just updated (no new id). Otherwise the Id test is a bit defunct since its not strictly a test of the class and cannot tell which operation (Insert/Update) has taken place.
From there you can test both scenarios, with a false id indicating an update. In an update scenario, verify by selecting based non-id data. Even better, since the id should be a primary key, make $memid a private class property of your test class. Set it on the initial insert, then re-use it for subsequent updates/verification selects...