1

Zend_Controllerの単体テスト方法の例をたくさん見つけましたが、Zend_Rest_Controllerの単体テストの例を探しています。どんな助けでも本当にありがたいです。ありがとうございました!

4

2 に答える 2

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

于 2011-06-22T15:33:34.800 に答える
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
    }

}
于 2011-06-22T13:37:20.160 に答える