user1261800
user1261800

Reputation:

PHPUnit testing- Unit test a basic function

I am learning how to perform proper unit testing/test cases of some of my php code. I have a simple function atMaxLivesAllowed() that I am testing atMaxLivesAllowedTest() below. But I am not sure how to pass a value to getLivesLeftCount() to compare to max_lives_limit. How can I do this correctly? any ideas what other test can I perform for that function atMaxLivesAllowed()?

public function getLivesCount(){

    $lives = new Life();
    $lives->player = $this->id;
    $lives->lives_left = '1';
    $lives->lives_used = '0';
    return $player->count();

}

public function atMaxLivesAllowed(){

    return $this->getLivesLeftCount() >= $this->max_lives_limit;
}

/*
 * Tests the expected behavior of atMaxLivesAllowed
 *
 */
public function atMaxLivesAllowedTest(){

    $player->getLivesLeftCount(1);
    $player->max_lives_limit=5;

    $this->asertTrue($player->atMaxLivesAllowed());
}

Upvotes: 0

Views: 100

Answers (1)

Daniel Figueroa
Daniel Figueroa

Reputation: 10696

Assuming that your implementation is correct and you're getting the expected behaviour I don't see that much wrong with this test except for on little misspelling of assert:

It should be:

$this->assertTrue($player->atMaxLivesAllowed());

On another note, when doing TDD, you write the tests first, It is tricky in the beginning but it is well worth the struggle because it forces you to think about how the different parts of your program will interact, that is, it forces you to think about the interfaces (public methods) which is very good indeed.

Another thing that I notice in your code is that you store the lives_left and lives_used as strings shouldn't they be integers?

Upvotes: 1

Related Questions