ZF controller testing

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

ZF controller testing

Post by josh »

Requires Zend Framework & SimpleTest, can assert actual actions are dispatched as well as match text in the response body, test forms.. etc..
Usage:

Code: Select all

<?php
require_once( MODULE_DEFAULT_PATH . '/controllers/AccountController.php' );
Mock::generate('Zend_Controller_Action_HelperBroker','MockHelperBroker');
Mock::generate('Zend_Controller_Action_Helper_FlashMessenger','MockFlashMessenger');
 
class AccountControllerTest extends  ControllerTestCase
{
    protected $helperBroker;
    protected $flashMessenger;
    
    function doSetUp()
    {
        $this->helperBroker = new MockHelperBroker;         
        $this->flashMessenger = new MockFlashMessenger;
        $this->helperBroker->setReturnValue( '__get', $this->flashMessenger, array('FlashMessenger') );
        
    }
    
    function doTearDown()
    { 
        unset( $this->helperBroker, $this->flashMessenger );
    }
    
    function testLoginForm()
    {
        $form = new default_Form_Login();
        $this->assertTrue( $form->isValid( array( 'username' => 'bob', 'password' => 'secret' ) ) );
        $this->assertFalse( $form->isValid( array( 'username' => 'bob', 'password' => '' ) ) );
    }
    
    function testPromptsLogin()
    {
        $this->setUri( 'http://localhost/Account/login' )->dispatch()->assertDispatched( 'default', 'Account', 'login' );
        $this->assertContains( $this->getBody(), '</form>' );
    }
 
    function testIdentifyActionSendsFlashMessage()
    {
        $this->setUri( 'http://localhost/Account/login' );
        $this->flashMessenger->expectOnce(
            'addMessage',
            array('Please provide a username and password.'));
        $controller = new AccountController( $this->request, $this->response );
        
    } 
 
}
 
 

Code: Select all

 
class ControllerTestCase  extends UnitTestCase
{
    protected $frontController;
    protected $response;
    protected $request;
    /**
    * @var stores response HTML for pattern matching
    */
    protected $body='';
    
    public function assertContains( $haystack, $needle )
    {
        $this->assertTrue( strstr( $haystack, $needle ) );
    }
    
    public function assertDispatched( $action, $controller=NULL, $module=NULL  )
    {
        $this->assertTrue(
            ( is_null( $module ) || $this->request->getParam('module') == $module )
            && ( is_null( $controller ) || $this->request->getParam('controller') == $controller )
            && ( is_null( $action ) || $this->request->getParam('action') == $action ));
    }
    
    protected function getBody( )
    {
        return $this->body;
    }
    
    public function setUp()
    {
        $this->response = new Zend_Controller_Response_Http();
        $this->frontController = Zend_Controller_Front::getInstance()->returnResponse(true)->setResponse( $this->response );
        
        $this->doSetUp();
    }
    
    public function dispatch()
    {
        $this->frontController->dispatch();
        $this->body = $this->response->getBody();
        return $this;
    }
    
    public function setUri( $uri )
    {
        return $this->setRequest( new Zend_Controller_Request_Http( $uri ) );
    }
    
    public function setRequest( $request )
    {
        $this->request  = $request;
        $this->frontController->setRequest( $request );
        return $this;   
    }
    
    public function doSetUp(){}
    
    public function tearDown()
    {
        unset( $this->request );
        unset( $this->response );
        
        $this->doTearDown();  
    }
    
    public function doTearDown(){}     
}
Post Reply