1

I have found a bunch of examples how to unit test Zend_Controller, but I'm looking for examples on Zend_Rest_Controller Unit Testing. Any help is really appreciated. Thank you!

6
  • Isn't that a controller as well? What makes it so different to standard controllers that you can not use the standard testing for controllers? Commented Jun 21, 2011 at 20:17
  • Methods PUT and DELETE -- Please refer to Rest on Wiki Commented Jun 21, 2011 at 20:22
  • I know what rest is. What's the problem in testing PUT and DELETE? Commented Jun 21, 2011 at 20:33
  • 1
    Well, try it out by yourself and let me know if you succeed. I didn't place the question just in vain or just for fun ;) Commented Jun 21, 2011 at 20:36
  • 1
    You're right, it not that different from regular Zend_Controller. It was my mistake! I thought to share some of my code, just in case... Commented Jun 22, 2011 at 13:41

2 Answers 2

1

So, basically your question is how to emulate calling PUT and DELETE in your controller tests?
Since this apparently doesn't work:

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

You can access both these actions with plain HTTP POST by providing _method parameter.

So to call PUT:

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

To call DELETE:

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

More reading on how to deal with RESTful routing here - http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.rest

Sign up to request clarification or add additional context in comments.

Comments

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
    }

}

1 Comment

Here is a good resource on how to create REST API in the first place: techchorus.net/create-restful-applications-using-zend-framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.