Shaolin
Shaolin

Reputation: 2551

Test cases to test a function phpunit

I'm new to php unit testing, What are the valid test cases for below function.

 protected function validateParams($graph, $start, $destination)
{
    if (!is_object($graph)) {

        throw new \InvalidArgumentException('Graph param should be an object !');
    }

    if (empty($start)) {

        throw new \InvalidArgumentException('Start param is empty !');
    }

    if (empty($destination)) {

        throw new \InvalidArgumentException('Graph param is empty !');
    }

    return true;
}

Upvotes: 0

Views: 74

Answers (1)

gontrollez
gontrollez

Reputation: 6538

First, test that when passing the correct arguments to the method, it returns true.

public function testParamsValidation();

Then check that an InvalidArgumentException is thrown when any of the arguments is empty. Note that you should have 3 tests, one for each of the arguments. In each test you pass only one empty argument. You probably want to have each of these tests executed several times with distinct argument values (like null, false, scalars, etc). Use dataProviders for that.

public function testInvalidArgumentExceptionIsThrownWhenGraphIsNotAnObject(;

public function testInvalidArgumentExceptionIsThrownWhenStartIsEmpty();

public function testInvalidArgumentExceptionIsThrownWhenDestinationIsEmpty();

Side note: you probably want to explicit the required object class in the method definition. The $graph object should be of a certain class or implement a certain interface?

Upvotes: 1

Related Questions