开发者

Test protected methods of ZF controllers with PHPUnit

I want to test a protected method which is inside a controller:

class AuthenticateController extends Zend_Controller_Action
{
    protected function开发者_如何学JAVA getAuthAdapter(){}
}

My test (following this post) look like this:

public function testGetAuthAdapterShouldReturnZendAuthAdapterDbTable()
{
    require_once(APPLICATION_PATH.'/controllers/AuthenticateController.php');
    $method = new Zend_Reflection_Method('AuthenticateController', 
                                         'getAuthAdapter');
    $class = new AuthenticateController();
    $result = $method->invoke($class);

    // assert result value
    $this->assertInstanceOf('Zend_Auth_Adapter_DbTable', $result);
}

When I execute the test, I got this runtime exception

RuntimeException: Fatal error: Call to a member function getModuleName() on a non-object in /Users/padawan_rodrigo/Documents/workspace/ZendFramework-1.11.5/library/Zend/Controller/Action/Helper/ViewRenderer.php on line 226
Call Stack:
0.0030     396924   1. {main}() -:0
0.0755    4593044   2. __phpunit_run_isolated_test() -:207
0.0771    4718524   3. PHPUnit_Framework_TestCase->run() -:22
0.0785    4798460   4. PHPUnit_Framework_TestResult->run() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:576
0.3565    4827948   5. PHPUnit_Framework_TestCase->runBare() /usr/local/pear/share/pear/PHPUnit/Framework/TestResult.php:666
0.6696   10508956   6. PHPUnit_Framework_TestCase->runTest() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:628
0.6697   10509780   7. ReflectionMethod->invokeArgs() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:738
0.6697   10509796   8. AuthenticateControllerTest->testGetAuthAdapterShouldReturnZendAuthAdapterDbTable() /project/code/tests/application/controllers/AuthenticateControllerTest.php:0
0.6794   10753940   9. Zend_Controller_Action->__construct() /project/code/tests/application/controllers/AuthenticateControllerTest.php:172

I also tried to mock the class with same results. Any ideas?

Thanks


You need to pass in a request and response to the controller's constructor.

$class = new AuthenticateController(
        new Zend_Controller_Request_HttpTestCase(), 
        new Zend_Controller_Response_HttpTestCase()
    );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜