views:

78

answers:

2

Hi, I'm using Zend_Test_PHPUnit_ControllerTestCase to test my controllers. This class provides various ways to test the rendered output, but I don't want to get my view scripts involved. I'd like to test my view's vars. Is there a way to access to the controllers view object?

Here is an example, what I'm trying to do:

<?php
class Controller extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this-view->foo = 'bar';
    }
}

class ControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

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

        //doesn't work, there is no such method:
        $this->assertViewVar('foo', 'bar');

        //doesn't work, end_Test_PHPUnit_ControllerTestCase has no getView method:
        $this->assertEquals(
            'bar',
            $this->getView()->foo
        );

    }
}
A: 

1) Zend_Test_PHPUnit_ControllerTestCase::_resetPlaceholders() uses the singelton obtained in Zend_Registry::getInstance() and searches it for placeholders. Maybe you could mimic that behaviour.

2) Did you try $view = Zend_Layout::getMvcInstance()->getView() already? I haven't tested controllers yet but since the test cases includes singeltons, perhaps that wouldn't be such a far out guess.

chelmertz
None of these solution works. The dispacher unsets the controller object after the action runs, so I'm afraid I have to subclass the dispatcher.
erenon
+2  A: 

If you really must assert against the view, get it with Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view and assert against it.

The intention of Zend_Test however is that you would assert against the actual response, using an xpath query or something similar. this will allow you to fully test your application, instead of just a part of it.

if you simply assert that the view contains a var and that it is equal to a given thing, you are not really testing that it has been used in the right way.

Bittarman
Thats a key insight, +1
chelmertz