2010-03-24 9 views
11

J'ai une application avec la structure de répertoire par défaut, pour une application sans modules personnalisés (voir figure de structure à la fin).Obtention d'une exception "Aucun module par défaut défini pour cette application" lors de l'exécution de tests unitaires dans zend framework

J'ai écrit un fichier ControllerTestCase.php comme indiqué dans de nombreux didacticiels, et j'ai également créé le fichier bootstrap approprié (encore une fois, voir les figures à la fin). J'ai écrit quelques tests de modèle qui fonctionnent bien, mais quand j'ai commencé à écrire le fichier de test du contrôleur d'index, avec un seul test, avec une seule ligne dedans ("$ this-> dispatch ('/ «);"), je reçois l'exception suivante lors de l'exécution PHPUnit (mais la navigation avec le navigateur au même endroit - tout est bon et travail):

1) IndexControllerTest::test_indexAction_noParams 
Zend_Controller_Exception: No default module defined for this application 

Pourquoi? Qu'est ce que j'ai mal fait ?

Annexes:
Structure du répertoire:

-proj/ 
    -application/ 
    -controllers/ 
     -IndexController.php 
     -ErrorController.php 
    -config/ 
    -layouts/ 
    -models/ 
    -views/ 
     -helpers/ 
     -scripts/ 
     -error/ 
      -error.phtml 
     -index/ 
      -index.phtml 
    -Bootstrap.php 
    -library/ 
    -tests/ 
    -application/ 
     -controllers/ 
     -IndexControllerTest.php 
     -models/ 
     -bootstrap.php 
     -ControllerTestCase.php 
    -library/ 
    -phpunit.xml 
    -public/ 
    -index.php 

(. Fondamentalement, j'ai quelques autres fichiers dans le répertoire des modèles, mais ce n'est pas pertinente à cette question)

application.ini :

[production] 
phpSettings.display_startup_errors = 0 
phpSettings.display_errors = 0 
includePaths.library = APPLICATION_PATH "/../library" 
bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 
bootstrap.class = "Bootstrap" 
appnamespace = "My" 
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.frontController.params.displayExceptions = 0 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" 
resources.view[] = 
phpSettings.date.timezone = "America/Los_Angeles" 

[staging : production] 

[testing : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 

[development : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 

tests/application/fichier bootstrap.php:

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

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

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

require_once 'ControllerTestCase.php'; 

fichier ControllerTestCase.php:

<?php 
require_once ('Zend/Test/PHPUnit/ControllerTestCase.php'); 
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase { 
    /** 
    * 
    * @var Zend_Application 
    */ 
    protected $application; 

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

    public function appBootstrap(){ 
     $this->application = new Zend_Application(APPLICATION_ENV, 
                APPLICATION_PATH . '/configs/application.ini'); 

    } 
} 

Répondre

11

C'est un bug connu.
Voici ma solution pour résoudre ce problème:

<?php 
require_once 'Zend/Application.php'; 
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; 

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase 
{ 
    protected $_application; 

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

    public function appBootstrap() 
    { 
     $this->_application = new Zend_Application(APPLICATION_ENV, 
       APPLICATION_PATH . '/configs/application.ini' 
     ); 
     $this->_application->bootstrap(); 

     /** 
     * Fix for ZF-8193 
     * http://framework.zend.com/issues/browse/ZF-8193 
     * Zend_Controller_Action->getInvokeArg('bootstrap') doesn't work 
     * under the unit testing environment. 
     */ 
     $front = Zend_Controller_Front::getInstance(); 
     if($front->getParam('bootstrap') === null) { 
      $front->setParam('bootstrap', $this->_application->getBootstrap()); 
     } 
    } 
} 
+0

En fait, j'utilise zend framework 1.10.2, donc ce bug vous » Cependant, vous ne m'avez pas parlé du tout, mais vous m'avez amené à la solution, à savoir que l'appel de $ this -> _application-> bootstrap() dans la méthode appBootstrap me manquait. Veuillez modifier votre réponse pour supprimer la référence au bogue, car ce n'est pas le cas. Merci – Doron

+0

je suis à 1.10.6 maintenant mais il me semble encore l'expérience de ce bug ... ne sais pas si j'ai fait quelque chose de mal tho. si je supprime ce correctif, il échoue –

+0

@jiewmeng Jetez un coup d'oeil à l'installation décrite par Matthew: [Test Zend Framework Applications] (http://www.slideshare.net/weierophinney/testing-zend-framework-applications) – takeshin

0

Ma solution a été d'inclure cette ligne après parent::setUp()

$this->_application->getBootstrap()->getPluginResource('frontcontroller')->init();