开发者

PHPUnit configure test

class ControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $object;

    public function  setUp() {
        $this->bootstrap = array($this, 'boostrap');
        parent::setUp();
    }

    public f开发者_运维百科unction bootstrap(){
        $this->application = new Zend_Application(
                    APPLICATION_ENV,
                    APPLICATION_PATH . '/configs/application.ini'
                );
        $this->application->bootstrap();

    }

    public function testIndexAction(){
        // body
    }

}

This is the class for the test. My question is how to implement the testIndexAction where the actual command on the command prompt is:

php zfrun.php -a ..index


Without seeing exactly what zfrun.php does, I can only guess, and it sounds like you need to ditch ControllerTestCase. ControllerTestCase is designed to mimic an HTTP request to send through the Zend dispatcher, but you don't need that.

Instead, you can try to mimic calling zfrun.php from the command line by setting up $argv as it would look and executing zfrun.php yourself:

function testIndexAction() {
    $argv = array(
            '-a',
            'module_name.controller_name.index',
        );
    require 'zfrun.php';
}

The problem is that this works for only one test, assuming zfrun.php defines classes or functions and cannot be required multiple times. Therefore, you'll need to do whatever zfrun.php does in a new test case base class without using zfrun.php itself. Essentially refactor its code into a reusable test helper method.

function executeControllerAction($module, $controller, $action) {
    ... whatever magic zfrun.php does ...
}


If this is the test for your home page, use

$this->dispatch('/');

If not, you'll have to give it the URL that will trigger the route to load this controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜