2
votes

I try to write test for controller. I use OS Windows, zend framework and my libraries are in C:/library which is added to the include_path of php.ini. When I run test testLoginAction I get an error No default module define for the application. But I don't use modules at all. Do you know how to solve this problem?

IndexControllerTest.php

class Controller_IndexControllerTest extends ControllerTestCase 
{
    public function testIsEverythingOK()
    {
        $this->assertTrue(true);
    }

    public function testLoginAction()
    {
            $this->dispatch('/login/index');
            $this->assertModule('default');
            $this->assertController('login');
            $this->assertAction('index');
    }
}

ControllerTestCase.php

require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $_application;

    public function setUp()
    {
        $this->bootstap = array($this, 'appBootstrap');     
        parent::setUp();
    }

    public function appBootstrap()
    {
    //       require dirname(__FILE__) . '/bootstrap.php';


        $this->front->setControllerDirectory('../application/controllers');     
        $this->_application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini');
        $this->_application->bootstrap();
    }   

}

My phpunit.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./application/bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    stopOnFailure="true"
    syntaxCheck="true">

    <!-- запускаем все тесты из корневой директории -->
    <testsuite name="Main Test Suite">
        <directory>./</directory>
    </testsuite>

        <filter>            <!-- смотрим лишь на следующие директории -->
        <whitelist>
                  <directory suffix=".php">../application</directory>
 <!--               <directory suffix=".php">../library</directory>-->
            <exclude>
                <directory suffix=".phtml">../application</directory>
                <directory>../application/forms</directory>
                <directory>../application/models</directory>                    
                <directory>../library</directory>                    
                <file>../application/Bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <!-- логирование и создание отчета -->
        <log type="coverage-html" target="./report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
    </logging>
</phpunit>

My bootstrap.php in tests/application:

// 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') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';

My Base class for tests:

require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $_application;

    public function setUp()
    {
        $this->bootstap = array($this, 'appBootstrap');     
        parent::setUp();
    }

    public function appBootstrap()
    {
//       require dirname(__FILE__) . '/bootstrap.php';


        $this->front->setControllerDirectory('../application/controllers');     
        $this->_application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini');
        $this->_application->bootstrap();
    }   
}

Best regards, Oleg.

3

3 Answers

0
votes

Make sure your application.ini does not contain configuration like resources.frontController.moduledirectory = APPLICATION_PATH "/modules" or resources.frontController.params.prefixDefaultModule = 1 or resources.modules[] =

0
votes

You need to modify your setUp() method from the ControllerTestCase class, after parent::setUp() line:

$this->getFrontController()->setControllerDirectory(APPLICATION_PATH . '/controllers');

and remove the following line from your appBoostrap method:

$this->front->setControllerDirectory('../application/controllers');

0
votes

$this->front-> won't work because the front controller property is $this->frontController-> or $this->getFrontController()->.

Even so, your tests probably won't work anyways because of the way you're bootstrapping. Are you configuring your front controller from an ini configuration file? If yes, then you don't need to configure the frontController in your tests, you need to configure your bootstrap by way of Zend_Application.

$this->bootstrap = new Zend_Application(
   'testing',
   APPLICATION_PATH . '/configs/application.ini'
);

You won't need to call bootstrap() on your Zend_Application instance because Zend_Test_PHPUnit_ControllerTestCase will do that when you dispatch a request.

So your base class might loook like this:

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        // Assign and instantiate in one step:
        $this->bootstrap = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );
        parent::setUp();
    }
}

And your controller test, extending your base class, will look like this:

class Controller_IndexControllerTest extends ControllerTestCase 
{
    public function testIsEverythingOK()
    {
        $this->assertTrue(true);
    }

    public function testLoginAction()
    {
            $this->dispatch('/login/index');
            $this->assertModule('default');
            $this->assertController('login');
            $this->assertAction('index');
    }
}

Done.

Resources

Bootstraping your TestCase