0

Following code:

function personInfo($person)
{
    $zipCode="12345";

    if($zipCode!=54321)
    {
        $zipCode="12345";
        $postal="World";

        if($zipCode AND $postal < 1)
        {
            return "Wrong";
        }
        //Test stops here and do not continue???
    }
    $person->firstname="Mike";
    $person->lastname="Wayne";
    $person->address="Universe";
    $person->zipCode="12345";
    $person->mobile="123456789784512"; 
    $person->password="GogoGO";
    $person->socialSecurityNumber="1234567891234567";
    return "OK";
}

I am not sure why it stops there since I want to test the code when it fails and when it's OK. I'm still learning so I would be much appreciated for all help!

Rest of the code (database & test) that connects to "function personInfo($person)":

function personInfo($person)
{
    $this->db->autocommit(false);

    $sql = "Select * from Postal Where ZipCode = '$person->zipCode'";
    $result = $this->db->query($sql);
    if($this->db->affected_rows!=1)
    { 
        $sql = "Insert Into Postal (ZipCode, Postal) Values ('$person->zipCode','$person->postal')";
        $result = $this->db->query($sql);
        if($this->db->affected_rows < 1)
        {
            $this->db->rollback();
            return "Wrong";
        }
    }

    $sql .=  "Update Person Set Firstname = '$person->firstname', Lastname = '$person->lastname',";
    $sql .= " Address = '$person->address', ZipCode = '$person->zipCode',";
    $sql .= " Mobile = '$person->mobile', Password ='$person->password'";
    $sql .= " Where SocialSecurityNumber = '$person->socialSecurityNumber'";
    $result = $this->db->query($sql);
    $this->db->commit();
    return "OK";
}

class personInfoTest extends PHPUnit_Framework_TestCase {

    public function testPersonInfoWrong() 
    {
        //arrange
        $zipCode="1234";
        $person=$zipCode;
        $record=new Record(new DBStub());
        // act
        $result= $record->personInfo($person);
        //assert
        $this->assertEquals("Wrong",$result);
    }

    public function testPersonInfoOK() 
    {
        //arrange
        $zipCode="1234";
        $person=$zipCode;
        $record=new Record(new DBStub());
        // act
        $result= $record->personInfo($person);
        // assert
        $this->assertEquals("OK",$result);
        $this->assertEquals("1234567891234567",$result->socialSecurityNumber); 
        $this->assertEquals("Mike",$result->firstname);
        $this->assertEquals("Wayne",$result->lastname);
        $this->assertEquals("Universe",$result->address); 
        $this->assertEquals("1234567812345",$result->telefonnr);
        $this->assertEquals("GogoGO",$result->password); 
        $this->assertEquals("12345",$result->zipCode);
        $this->assertEquals("World",$result->poststed);
    }
}
Niknak
  • 583
  • 1
  • 8
  • 22
  • 2
    Your method is too large to test seriously. You select, insert, update, rollback or commit in 1 method. – schellingerht Jan 15 '16 at 18:36
  • You are comparing a string `World` to an integer `1`. `World` is cast to an integer and turns into `0` which is less than `1` - see https://stackoverflow.com/questions/9468148/php-string-int-comparison – Jo. Jan 15 '16 at 18:37
  • After you will make your code work, i advice to post it at http://codereview.stackexchange.com because there are some things to improve (refactor). People there will help it look nice and more professional. – Nikita U. Jan 15 '16 at 21:40

0 Answers0