I am new to Zend Framework and am trying to implement a RESTful controller in Zend and trying to write unit tests for my controller. I am following the REST API available at: https://github.com/codeinchaos/restful-zend-framework but for every kind of test, PHPUnit always hits 404 (observable by asserting the response codes - the one I expect and the one that PHPUnit receives), thus failing the tests.
I have added all code in respective files as this REST API's readme instructs:
• I have added a module specific for REST: /application/modules/api. • In my application.ini, I have the following:
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
• In my application/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);
}
• In my /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);
}
• Out of my own experiments, I have also added a line $autoloader->registerNamespace('REST_') to my /public/index.php where $autoloader refers to an instance of Zend_Loader_Autoloader.
• My test's location: /tests/application/modules/api/controllers/RestClassTest.php • This is my one of my tests for 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);
}
• And this is my 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);
}
It is true that I am setting a 404 for no-result-found situation but confining specifically at getAction, it is hitting 404 with test for a recognized valid ID as well.
I checked numerous resources (including the example provided by this API and Matthew O'Phinney's post: http://www.mwop.net/blog/228-Building-RESTful-Services-with-Zend-Framework.html) and all instruct to follow the same things that I am already doing. It is evident that I am missing something they are telling or am not comprehending some factor. I'll be obliged if anyone guides me where I'm doing what wrong.
Thanks in advance.
Does it work if you remove:
$frontController->setRequest(new REST_Request);
in _initRest() ?