Zend_Rest_Controller单元测试的示例

时间:2011-06-21 20:13:31

标签: php zend-framework phpunit zend-rest restful-architecture

我找到了一些如何对Zend_Controller进行单元测试的例子,但我正在寻找Zend_Rest_Controller单元测试的例子。任何帮助都非常感谢。谢谢!

2 个答案:

答案 0 :(得分:1)

所以,基本上你的问题是如何在控制器测试中模拟调用PUTDELETE
因为这显然不起作用:

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

通过提供HTTP POST参数,您可以使用普通_method访问这两项操作。

所以致电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

答案 1 :(得分: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
    }

}