2012-12-07 100 views
-1

測試功能我有這樣的代碼:需要與Oracle存儲過程

protected function _checkUserVisibility() 
{ 
    try { 
     if (!$params->getUsrParametr(self::ACTIVE_FIF)) { // calling oracle stored proc 
      throw new Unitex_Exception('ALARM'); 
     } 
    } 
    catch (Exception $e) { 
     $this->logOut(); 
     throw $e; 
    } 
} 

此FUNC從另一個caled(等)。

一個問題: 如何得到工作單元測試的代碼部分?

EDIT1:

先抽放嘿嘿http://framework.zend.com/manual/1.12/en/zend.test.phpunit.html 比improoved(希望) 測試PROC是:

class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase { 
.......... 
public function testLoginAction() 
{ 
    $request = $this->getRequest(); 
    $request->setMethod('POST') 
     ->setHeader('X_REQUESTED_WITH', 'XMLHttpRequest') 
     ->setPost(array(
       'user'  => 'test_user', 
       'password' => 'test_pwd', 
     )); 

     $filialId = 1; 
     $stmt1 = Zend_Test_DbStatement::createUpdateStatement(); 
     $this->getAdapter()->appendStatementToStack($stmt1); 
     $this->getAdapter()->appendStatementToStack($stmt1); 
     $this->getAdapter()->appendStatementToStack($stmt1); 
     $this->getAdapter()->appendStatementToStack($stmt1); 

     $stmt1Rows = array(array('IRL_ALIAS' => 'RO_COMMON', 'ISADM' => 'N')); 
     $stmt1 = Zend_Test_DbStatement::createSelectStatement($stmt1Rows); 
     $this->getAdapter()->appendStatementToStack($stmt1); 

     $this->dispatch('/user/login');// <-- crash here 
    $this->assertController('user'); 
    $this->assertAction('login'); 
    $this->assertNotRedirect(); 
    $this->_getResponseJson(); 
} 
+0

僅供參考,你要尋找的是PHPUnit的測試PHP。 Oracle DB與你將如何測試這個PHP代碼沒有任何關係。 – jsteinmann

回答

4

在你的單元測試definitly你不希望任何數據庫交互。你的問題的答案是使用數據庫功能的存根。

假設$ paramsSomeClass的財產包含getUsrParametr哪些例如從數據庫中獲得某些東西。您正在測試_checkUserVisibility方法,因此您不關心SomeClass中發生了什麼。這是這樣,你測試看起來就像是:

class YourClass 
{ 
    protected $params; 

    public function __construct(SomeClass $params) 
    { 
     $this->params = $params; 
    } 

    public function doSomething() 
    { 
     $this->_checkUserVisibility(); 
    } 

    protected function _checkUserVisibility() 
    { 
     try { 
      if (!$this->params->getUsrParametr(self::ACTIVE_FIF)) { // calling oracle stored proc 
       throw new Unitex_Exception('ALARM'); 
      } 
     } 
     catch (Exception $e) { 
      $this->logOut(); 
      throw $e; 
     } 
    } 
} 

當然,單元測試的唯一方法,你考的是公共的,但你蓋通過測試公衆一個受保護的方法。

public function testDoSomethingAlarm() 
{ 
    // set expected exception: 
    $this->setExpectedException('Unitex_Exception', 'ALARM'); 

    // create the stub 
    $params = $this->getMock('SomeClass', array('getUsrParametr')); 

    // and set desired result 
    $params->expects($this->any()) 
     ->method('getUsrParametr') 
     ->will($this->returnValue(false)); 

    $yourClass = new YourClass($params); 
    $yourClass->doSomething(); 
} 

和第二測試,其測試情況下,如果getUsrParametr將返回true

public function testDoSomethingLogout() 
{ 
    // set expected exception: 
    $this->setExpectedException('SomeOtherException'); 

    // create the stub 
    $params = $this->getMock('SomeClass', array('getUsrParametr')); 

    // set throw desired exception to test logout 
    $params->expects($this->any()) 
     ->method('getUsrParametr') 
     ->will($this->throwException('SomeOtherException')); 

    // now you want create mock instead of real object beacuse you want check if your class will call logout method: 
    $yourClass = $this->getMockBuilder('YourClass') 
     ->setMethods(array('logOut')) 
     ->setConstructorArgs(array($params)) 
     ->getMock(); 

    // now you want ensure that logOut will be called 
    $yourClass->expects($this->once()) 
     ->method('logOut'); 

    // pay attention that you've mocked only logOut method, so doSomething is real one 
    $yourClass->doSomething(); 
} 
+0

您的答案看起來不錯,但不明白在哪裏以及如何。我添加了edit1。能否請你幫忙? – Subdigger

+0

「哪裏和如何」什麼? :)我不確定是否完全理解你。但根據我的理解:問題的發生是因爲在你使用數據庫做某事的「調度」方法中,然後崩潰。如果是這樣,那麼你只需要嘲笑你的「調度」方法(你可以基於我的答案中的例子),這將是好的 – Cyprian

+0

認爲你是Zend程序員=)。 – Subdigger

2

如果你使用PHP 5.3.2+ PHPUnit的,你可以測試您的私有和受保護方法通過使用反射設置它們在運行測試之前公開,否則通過正確測試使用受保護/私有方法的公共方法來測試受保護/私有方法。一般來說兩個選項以後一般是你應該怎麼做,但如果你想使用反射,這裏有一個普通的例子:

protected static function getMethod($name) { 
    $class = new ReflectionClass('MyClass'); 
    $method = $class->getMethod($name); 
    $method->setAccessible(true); 
    return $method; 
} 

public function testFoo() { 
    $foo = self::getMethod('foo'); 
    $obj = new MyClass(); 
    $foo->invokeArgs($obj, array(...)); 
    ... 
} 
+0

問題不在測試私人func。在測試鸚鵡func的問題中,調用private func包含調用storet proc並等待結果存儲在proc params中。但如果沒有調用存儲過程 - 沒有parans,因此 - 一種方法 - 異常 – Subdigger