2014-01-21 41 views
0

我有一個簡單的問題。可能我錯過了一些東西。有沒有可能在Zend Framework中測試正常的功能,而不是操作?如何在Zend中用PHPUnit測試函數?

例如,我想爲這個功能運行測試:

public function isMature($age){ 
    if($age>=18) true; 
     else false; 
} 

它位於IndexController中。我試着

$this->indexController = new IndexController(); 
$this->assertFalse($this->indexController->isMature(5)); 

但PHPUnit的說,我必須通過Zend_Controller_Request_Abstract的實例__construct()。這是否是適當的方式來實現這一點?如何以良好的方式準備這個測試?

謝謝。

回答

1

你並不需要使用控制器在所有測試功能已經看清楚。您的代碼與Zend Framework無關,因此您無需關心控制器。

class MatureTest extends PHPUnit_Framework_TestCase { 
    /** 
    * @dataProvider dataIsMature 
    */ 
    public function TestIsMature($age, $expected) { 
     $this->assertSame($expected, isMature($age)); 
    } 

    public function dataIsMature() { 
     return array(
      'mature' => array(18, true), 
      'not mature' => array(17, false), 
      'really mature' => array(99, true) 
     ); 
    } 
} 

只要確保你包括與函數的文件在您的測試類

雖然你真的應該建立某種形式的一個包裝此功能,而不是建立一個函數模型/服務類的。

1

當控制器不是一個動作時,測試這些功能並不那麼容易。我會將這樣的「業務邏輯」轉移到ServiceClass或Model或者什麼以及如何測試。

class My_Age_Service() 
{ 
    public function isMature($age){ 
     if($age>=18) true; 
     else false; 
    } 
} 

進行單元測試控制器動作,在「Zend_Test_PHPUnit_ControllerTestCase」 Zend Controller Tests