Testing modulaire zend application with PHPUnit

For programming and general questions on Zend Framework

Testing modulaire zend application with PHPUnit

Postby dirtybolle on Thu May 03, 2012 8:17 am

So i have a full zend application with modules and i want to setup PHPUnit testing

this is the project folder


Code: Select all
   - project/
       -application/
          - controllers/
              - indexController.php
          - modules/
             - mycore/
                - controllers/
                    -  ActionsController.php
                - views/
                    - ...
       - ...
       - tests/
          - application/
             - controllers/
                -IndexControllerTest.php
             - modules/
                - mycore/
                    - controllers/
                       - ActionsControllerTest.php
          ControllerTestCase.php
          Bootstrap.php
          phpunit.xml

This is the content of each setup file in the test folder

ControllerTestCase.php

Code: Select all
    require_once 'Zend/Application.php';
    require_once 'Zend/Auth.php';
    require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
   
    class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
        protected $application
   
        public function setUp() {
            $this->bootstrap = array($this, 'appBootstrap');
            parent::setUp();
        }
   
        public function appBootstrap() {
            $this->application = new Zend_Application(
                APPLICATION_ENV,
                APPLICATION_PATH . '/configs/application.ini');
            $bootstrap = $this->application->getBootstrap()->bootstrap();
   
            $front = Zend_Controller_Front::getInstance();
            $front->setControllerDirectory(APPLICATION_PATH . '/controllers','default');
            $front->addModuleDirectory(APPLICATION_PATH . '/modules');
           
            return $bootstrap;
        }
   
        public function tearDown() {
            Zend_Auth::getInstance()->clearIdentity();
            $this->resetRequest();
            $this->resetResponse();
            parent::tearDown();
        }
   
        protected  function _doLogin($identity = null) {
            if ( $identity === null ) {
                $identity = $this->_generateFakeIdentity();
            }
            Zend_Auth::getInstance()->getStorage()->write( $identity );
        }
       
        protected function _generateFakeIdentity() {
            $identity = new stdClass();
            $identity->RecID                     = 3;
            $identity->user_firstname            = '****';
            $identity->user_lastname             = '********';
            $identity->confirmed                 = true;
            $identity->enabled                   = true;
   
            return $identity;
        }
   
        protected  function _doLogout() {
            Zend_Auth::getInstance()->clearIdentity();
        }
    }

Bootstrap.php

Code: Select all
error_reporting(E_ALL);
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
   
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
   
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/../library'),
        get_include_path(),
    )));
     
    require_once 'Zend/Loader/Autoloader.php';


phpunit.xml

Code: Select all
<phpunit bootstrap="./bootstrap.php" colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true" >
        <testsuite name="Application Test Suite">
            <directory>./</directory>
        </testsuite>
        <testsuite name="Library Test Suite">
            <directory>./library</directory>
        </testsuite>
       
        <filter>
            <!-- If Zend Framework is inside your project's library, uncomment this filter -->
            <!--
            <whitelist>
                <directory suffix=".php">../../library/Zend</directory>
            </whitelist>
            -->
        </filter>
    </phpunit>


And this is the content of the module test

ActionsControllerTest.php

Code: Select all
class Mycore_ActionsControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
    {
       
        public $module;
       
        public function setUp()
        {
            $this->module = 'mycore';
            $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
            $_SERVER['HTTP_HOST'] = 'unittest_host';
            $_SERVER['REQUEST_URI'] = '/';
            parent::setUp();
        }
   
        public function testIndexAction()
        {
            $this->dispatch("/");
            // assertions
            $this->assertModule('mycore');
            $this->assertController('actions');
            $this->assertAction('index');       
        }
   
   
    }


And this is the result:

Code: Select all
Starting test 'IndexControllerTest::testIndexAction'.
    .
    Starting test 'Mycore_ActionsControllerTest::testIndexAction'.
    F
   
    Time: 1 second, Memory: 14.00Mb
   
    There was 1 failure:
   
    1) Mycore_ActionsControllerTest::testIndexAction
    Failed asserting last module used <"default"> was "mycore"
   
    /project/library/Zend/Test/PHPUnit/ControllerTestCase.php:929
    /project/tests/application/modules/mycore/controllers/ActionsControllerTest.php:21

All the tests with a module are working fine but when I start testing module controllers i get this error.
I have searched all over the internet but couldn't find a fix for this error, so i hope anyone can help me.

Thanks in advance
dirtybolle
 
Posts: 1
Joined: Thu May 03, 2012 8:15 am

Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 1 guest