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


need some advice on what to test 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!' );
    }
}

首先,我认为它看起来像很多重复的代码,我有很多方法,我测试,所以一个异常被抛出。

就像我说的,我是一个测试的初学者,我有一些麻烦来弄清楚我需要测试什么。

一些建议和技巧真的会对我有帮助的。

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

编写一个名为getRandomNonStringValues()的方法,该方法将返回一个包含不同类型值的数组,除了字符串,并在foreach循环中将所有这些值发送给setUsername方法。然后测试得到每个值的异常

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

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