【发布时间】:2014-05-11 17:49:59
【问题描述】:
在为我的代码编写测试方面,我是一个初学者。我希望有人能给我一些好的建议。
这是我要测试的课程。
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,我在其中进行测试,所以会抛出异常。
正如我所说,在测试方面我是个初学者,我很难弄清楚我需要测试什么。
一些建议和技巧对我很有帮助:)
【问题讨论】:
标签: php unit-testing testing phpunit