开发者

Testing Zend Controllers with Single-Instance Sessions

I am attempting to use Zend_Test_PHPUnit_ControllerTestCase in my testing and am experiencing difficulties.

My application bootstrap instantiates a library class that manages access to a Zend_Session_Namespace called 'LiveData'. LiveData maintains arrays of data for Current User, Current Organsation, etc. Once instantiated, the LiveData object is stored in the registry.

When I instantiate LiveData, I set singleInstance = true, ie. new Zend_Session_Namespace('LiveData', true) This is to reduce potential confusion about who does what to LiveData.

Anyhow, this seems to work fine. I browse the site, log开发者_如何学Pythonin, logout, etc, and my session data behaves as I would expect.

When trying to test, however, I get a message saying "A session namespace object already exists for this namespace".

My tests are bootstrapped in each test file as follows:

public function setUp()
{
    $this->bootstrap = new Zend_Application(APPLICATION_ENV, 
            APPLICATION_PATH . '/configs/application.ini');
    parent::setUp();
}

This was the default scaffolding placed there by Netbeans. Reading the Zend_Test documentation, this seems fine to me.

The problem seems to arise when I dispatch a controller in a test, as follows:

public function testIndexIsDefaultAction()
{
    $this->dispatch('/');

    // assertions
    $this->assertController('Index');
    $this->assertAction('index');
}

I think that the test's setUp() method is resulting in my application bootstrap running (and creating a copy of LiveData). Then $this->dispatch('/') is running the bootsrap again... I can't imagine why this would be the case, though. (Why would we want it to run twice?) If you can shed any light on my plight, I'd be most appreciative!


The setUp() method only tells Zend how to call your bootstrap which is done when you call dispatch(). However, your bootstrap will be run for every test method. Since you're telling Zend to prevent multiple instances of Zend_Session_Namespace with the same namespace, the second test method will fail.

Look in the source for Zend_Session_Namespace for a way to reset the flag. If it exists, add it to your test bootstrap before you call your application bootstrap.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜