Question

I'm a beginner when it comes to writing tests for my code. And i was hoping someone can give me some good advice.

Here's my class i want to test.

class Http_Client {

/**
 * @var string
 */
private $uri;

/**
 * @var string
 */
private $username;

/**
 * @var string
 */
private $password;

/**
 * @var Http_Adapter_Interface
 */
private $httpAdapter = null;

/**
 * Default Constructor
 * @param string
 * @param string
 */
public function __construct($username, $password) {
    $this->setUsername( $username );
    $this->setPassword( $password );
}

/**
 * @param string $username
 * @throws Exception
 */
public function setUsername( $username ) {

    if( is_string( $username ) )
    {
        $this->username = $username;
    }
    else
    {
        throw new Exception( 'Username must be of type String!' );
    }
}

/**
 * @param string $password
 * @throws Exception
 */
public function setPassword( $password ) {

    if( is_string( $password ) )
    {
        $this->password = $password;
    }
    else
    {
        throw new Exception( 'Password must be of type String!' );
    }
}

/**
 * @return string
 */
private function _getUsername()
{
    return $this->username;
}

/**
 * @return string
 */
private function _getPassword()
{
    return $this->password;
}

/**
 * @param Http_Adapter_Interface $httpAdapter
 * @throws Exception
 */
public function setHttpAdapter( Http_Adapter_Interface $httpAdapter)
{
    if( is_null( $this->httpAdapter) )
    {
        $this->httpAdapter = $httpAdapter;
    }
    else
    {
        throw new Exception( 'Http Adapter already set!' );
    }
}

/**
 * @return Http_Adapter_Interface
 */
private function _getHttpAdapter()
{
    return $this->httpAdapter;
}

/**
 * @param string
 * @throws Exception
 * @return  void
 */
public function setURI($uri) {

    if( is_string( $uri ) )
    {
        $this->uri = $uri;
    }
    else
    {
        throw new Exception( 'Uri must be of type String!' );
    }

}

/**
 * @return string
 */
private function _getURI()
{
    return $this->uri;
}

/**
 * @param  string $method
 * @param array $data
 * @throws Exception
 * @return Http_Response
 */
public function request($method, $data = array() ) {


    if( !is_string( $method ) )
    {
        throw new Exception( '$method must be of type String!' );
    }

    if( !is_array( $data ) )
    {
        throw new Exception( '$data must be of type Array!' );
    }

    $httpAdapter = $this->_getHttpAdapter();
    $username = $this->_getUsername();
    $password = $this->_getPassword();
    $uri = $this->_getURI();


    if($method == 'GET')
    {
        return $httpAdapter->get( $username, $password, $uri );
    }

    if($method == 'POST')
    {
        return $httpAdapter->post( $username, $password, $uri, $data );
    }
}


}

and here are the test methods i have so far

public function testInstance()
{
    $this->assertInstanceOf('Http_Client', $this->Http_Client);
}


/**
 * @expectedException Exception
 */
public function testSetUsernameThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setUsername( 1 );
}

//test setPassword
/**
 * @expectedException Exception
 */
public function testSetPasswordThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setPassword( 1 );
}


/**
 * @expectedException Exception
 */
public function testSetHttpAdapterThrowsExceptionIfHttpAdapterIsAlreadySet()
{
    $httpAdapter = new Http_AdapterTest();
    $this->Http_Client->setHttpAdapter( $httpAdapter );

}


/**
 * @expectedException Exception
 */
public function testSetURIThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setURI( 1 );
}


//test request

/**
 * @expectedException Exception
 */
public function testRequestThrowsExceptionIfMethodParamIsNotString()
{
    $this->Http_Client->request( 1 );
}


public function testRequestReturnHttp_responseObject() {

    $response = $this->Http_Client->request( 'GET' );

    if( $response instanceof Http_Response )
    {
        $this->assertEquals( 200, $response->getStatusCode() );
    }
    else
    {
        $this->fail( 'Expected Http_Response, got something else!' );
    }
}

First of all i think it looks like a lot of duplication code, i have a lot of metotds where i test so an exception is thrown.

As i said, im a beginner when it comes to testing, and i have some trouble to figure out what i need to test.

some advice and tips would really help me :)

Was it helpful?

Solution

Test below is not enough. You should test exception for all possible types (float, bool etc..)

Write an method named getRandomNonStringValues() This method will return an array containing different type values except string and in foreach loop send all these values to setUsername method. And test you get exception for each of these values.

/**
 * @expectedException Exception
 */
public function testSetUsernameThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setUsername( 1 );
}

You should also test success case for setUsername method. Make method getRandomString() and test your method with this method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top