2013-08-18 51 views
0

我是Zend Framework的新手,我試圖在Zend中實現一個RESTful控制器,並試圖爲我的控制器編寫單元測試。我正在關注REST API:https://github.com/codeinchaos/restful-zend-framework,但對於每種測試,PHPUnit總是命中404(通過聲明響應代碼來觀察 - 我期望的和PHPUnit接收的代碼),從而使測試失敗。Zend 1.x中的REST風格路由

我已經添加在各自的文件中的所有代碼,因爲這REST API的自述指示:

•我已經加入特定的REST的模塊:/應用/模塊/ API。 •在我的application.ini,我有以下幾點:

autoloaderNamespaces[] = "REST_" 

rest.default = "xml" 
rest.formats[] = "json" 
rest.formats[] = "xml" 

resources.frontController.plugins.ErrorHandler.class = "Zend_Controller_Plugin_ErrorHandler" 
resources.frontController.plugins.ErrorHandler.options.module = "default" 
resources.frontController.plugins.ErrorHandler.options.controller = "error" 
resources.frontController.plugins.ErrorHandler.options.action = "error" 

resources.router.routes.rest.type = Zend_Rest_Route 

•在我的應用程序/ bootstrap.php中:

public function _initREST() 
{ 
    $frontController = Zend_Controller_Front::getInstance(); 

    // set custom request object 
    $frontController->setRequest(new REST_Request); 
    $frontController->setResponse(new REST_Response); 
    $frontController->addModuleDirectory(APPLICATION_PATH . '/modules'); 

    // Add the REST route for the API module only 
    $restRoute = new Zend_Rest_Route($frontController, array(), array('api')); 
    $frontController->getRouter()->addRoute('rest', $restRoute); 
} 

•在我的/application/modules/api/Bootstrap.php:

public function _initREST() 
    { 
     $frontController = Zend_Controller_Front::getInstance(); 

     // Register the RestHandler plugin 
     $frontController->registerPlugin(new REST_Controller_Plugin_RestHandler($frontController)); 

     // Add REST contextSwitch helper 
     $contextSwitch = new REST_Controller_Action_Helper_ContextSwitch(); 
     Zend_Controller_Action_HelperBroker::addHelper($contextSwitch); 

     // Add restContexts helper 
     $restContexts = new REST_Controller_Action_Helper_RestContexts(); 
     Zend_Controller_Action_HelperBroker::addHelper($restContexts); 
    } 

•超出我自己的實驗,我還添加了一行$ autoloader-> registerNamespace(「REST_」)我/public/index.php其中$自動加載機是指Zend_Loader_Autoloader的一個實例。

•我的測試位置:/tests/application/modules/api/controllers/RestClassTest.php •這是對的getAction()我對我的測試之一:

public function testGetAction() 
{ 
    $this->request->setMethod('GET'); 
    // Have tried other variations of URI too but all bear same result (getting 404) in tests indicating that routing is not set at the fundamental level. 
    $this->dispatch('/api/learn-to-rest?id=51fc287fc55ffc4006410bca'); 

    $body = $this->getResponse()->getBody(); 
    $json = json_decode($body, true); 
    $this->assertEquals('John Doe', $json['name']);  

    $httpResponse = $this->getResponse()->getHttpResponseCode(); 
    $this->assertEquals(200, $httpResponse); 
} 

•這是我的getAction ():

public function getAction() 
{ 
    $request = $this->getRequest(); 
    $objectId = $request->getParam('id'); 

    // Validation for ID 
    $this->_validateMongoObjectIdAction($objectId); 

    // Perform DB record selection 
    $records = new Db_Collection_Class(new Db_Mongo_Class); 
    $result = $records->findInCollection(array('_id' => new MongoId($objectId)));  
    if ($result === null) { 
     // Source: http://www.w3.org/Protocols/HTTP/HTRESP.html 
     $this->getResponse()->setHttpResponseCode(404); 
    } 

    // Dispatch records in JSON format 
    $this->_helper->json($result); 
} 

這是我設置一個404沒有結果發現的情況,但在具體的getAction圍是真實的,它是打404測試認可的有效身份證件爲好。

我查了很多資源(包括這個API提供的例子和Matthew O'Phinney的帖子:http://www.mwop.net/blog/228-Building-RESTful-Services-with-Zend-Framework.html),所有的指示都遵循我已經做的相同的事情。很明顯,我錯過了他們正在說的或者沒有理解某些因素。如果有人引導我在哪裏做錯了什麼,我將不得不承擔責任。

在此先感謝。

回答

0

請問,如果你刪除它的工作:

$frontController->setRequest(new REST_Request); 

在_initRest()?

+0

不,但事實證明,我不需要這個REST API的自述文件指示的_/application/modules/api/Bootstrap.php_。爲什麼API作者會擁有它超越了我;任何人都知道它,請分享。其次,我認爲無論我在測試或我的代碼中做什麼,都不會在任何地方反映出來。因此,關於Zend的一些更深入的研究使得我們知道,對於模塊目錄中的控制器類,模塊名稱必須預先加上類名,即class Api_RestClassController。現在,至少PHPUnit正在查找我的代碼類。 – patternMatch