需要一些关于测试PHPUnit的建议

时间:2014-05-11 17:50:00

标签: php unit-testing testing phpunit

我在为代码编写测试时是一个初学者。我希望有人可以给我一些好的建议。

这是我要上课的课程。

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 );
    }
}


}

这是我到目前为止的测试方法

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!' );
    }
}

首先,我认为它看起来像很多重复代码,我有很多metotds我测试所以抛出异常。

正如我所说的那样,在测试方面我是一个初学者,而且我很难弄清楚我需要测试什么。

一些建议和提示对我有帮助:)。

1 个答案:

答案 0 :(得分:0)

以下测试是不够的。您应该测试所有可能类型的异常(float,bool等..)

编写一个名为getRandomNonStringValues()的方法此方法将返回一个包含除string之外的不同类型值的数组,并在foreach循环中将所有这些值发送到setUsername方法。并测试每个值的异常。

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

您还应该测试setUsername方法的成功案例。 make方法getRandomString()并使用此方法测试您的方法。