2011-06-21 173 views
1

我發現了一堆示例如何單元測試Zend_Controller,但我正在尋找Zend_Rest_Controller單元測試的示例。任何幫助真的很感激。謝謝!Zend_Rest_Controller單元測試示例

+0

是不是一個控制器以及?與標準控制器不同的是,您不能使用控制器的標準測試嗎? – hakre

+0

方法PUT和DELETE - 請參考[Wiki on rest](http://en.wikipedia.org/wiki/Representational_State_Transfer) – eistrati

+0

我知道剩下的是什麼。測試PUT和DELETE時有什麼問題? – hakre

回答

1

所以,基本上你的問題是如何模擬調用你的控制器測試PUTDELETE
由於這顯然不起作用:

$this->request->setMethod('PUT'); 

可以通過提供_method參數訪問這兩個操作與普通HTTP POST

所以叫PUT

$this->request->setMethod('POST'); 
$this->dispatch('articles/123?_method=put'); 

要調用DELETE

$this->request->setMethod('POST'); 
$this->dispatch('articles/123?_method=delete'); 

更多閱讀關於如何應對這裏的RESTful路由 - http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest

0
/** 
* Sample class to test a controller 
*/ 
class ArticleControllerTest extends Zend_Test_PHPUnit_ControllerTestCase 
{ 

    public $bootstrap; 

    public function setUp() 
    { 
     // When bootstrap is called it will run function 'appBootstrap' 
     $this->bootstrap = array($this, 'appBootstrap'); 
     parent::setUp(); 
    } 

    public function appBootstrap() 
    { 
     $this->application = new Zend_Application(
      APPLICATION_ENV, 
      APPLICATION_PATH . '/configs/application.ini'); 
     $this->application->bootstrap(); 
     $bootstrap = $this->application->getBootstrap(); 
     $front = $bootstrap->getResource('FrontController'); 
     $front->setParam('bootstrap', $bootstrap); 
    } 

    public function tearDown() 
    { 
     Zend_Controller_Front::getInstance()->resetInstance(); 
     $this->resetRequest(); 
     $this->resetResponse(); 
     parent::tearDown(); 
    } 

    public function testIndexAction() 
    { 
     $testCases = array(
      '/article/', 
      '/article/id/123/', 
      '/article/authorId/777/limit/5/', 
      '/article/commentId/999/startDate/2011-06-01/endDate/2011-06-01/', 
     ); 

     foreach ($testCases as $url) { 
      $this->request->setHeader('Content-Type', 'text/json'); 
      $this->dispatch($url); 

      $this->assertResponseCode(200); 
      $this->assertModule('default'); 
      $this->assertController('article'); 
      $this->assertAction('get'); 

      $body = json_decode($this->response->getBody(), true); 
      $this->assertNotEmpty($body); 
      ... 

      $this->resetRequest(); 
      $this->resetResponse(); 
     } 
    } 

    public function testGetAction() 
    { 
     // Same as $this->testIndexAction() 
    } 

    public function testPostAction() 
    { 
     // Similar to $this->testIndexAction() 
     // Add $this->request->setMethod('POST'); before dispatch 
     // Change $this->assertResponseCode(200); to 201 as REST requires 
    } 

    public function testPutAction() 
    { 
     // Similar to $this->testIndexAction() 
     // Add $this->request->setMethod('PUT'); before dispatch 
    } 

    public function testDeleteAction() 
    { 
     // Similar to $this->testIndexAction() 
     // Add $this->request->setMethod('DELETE'); before dispatch 
    } 

} 
+0

這裏有一個關於如何創建REST API的好資源:http://techchorus.net/create-restful-applications-using-zend-骨架 – eistrati