2009年10月12日星期一

Re: [fw-mvc] unit test

Of course.

bootstrap.php:

<?php

error_reporting(E_ALL|E_STRICT);

// 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';

ControllerTestCase.php:

<?php

require_once 'Zend/Application.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 ();
}
//bootstrap the application
public function appBootstrap() {
$this->application = new Zend_Application ( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' );
$this->application->bootstrap ();
}
}

?>
 

And bootstrap file in application:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initControllers() 

{
$this->bootstrap ( 'FrontController' );
$front = $this->frontController;
$front->throwExceptions ( true );
$front->addModuleDirectory ( '../application/modules' );
}
protected function _initConfig(){
$config=new Zend_Config(array(APPLICATION_PATH . '/configs/application.ini'));
Zend_Registry::set('config',$config);
}
protected function _initDatabase() {
$dbresource = $this->getPluginResource ( 'db' );
$db = $dbresource->getDbAdapter ();
Zend_Registry::set ( 'db', $db );
Zend_Db_Table::setDefaultAdapter ( $db );
}
protected function _initLog() {
$logconfigs = $this->getOption('log');
$logger = new Zend_Log ( );
$writer = new Zend_Log_Writer_Stream ( $logconfigs['filepath'] );
$logger->addWriter ( $writer );
Zend_Registry::set ( 'logger', $logger );
}
protected function _initModels() {
$autoloader = Zend_Loader_Autoloader::getInstance ();
$autoloader->registerNamespace ( 'Mt' );
}
}




2009/10/12 Marcus Ramsden <mmr@ecs.soton.ac.uk>
Could I see the controllertestcase and bootstrap files you are using?

On 12 Oct 2009, at 10:37, huajun qi wrote:

I believe your way can work in production environment, but i doubt whether it works in testing environment?

And even though it works,  I still can no get configs i registered, so i don't think path is the essence of problem.

2009/10/12 Marcus Ramsden <mmr@ecs.soton.ac.uk>
Just a thought, it is a bit of a basic one but just to cover all of the bases. Are you defining your autoloader with an absolute path or a relative one. Here is an example where I am bootstrapping from an absolute path;

class Authentication_Bootstrap
{

protected function _initModuleRoot()
{
defined("AUTHENTICATION_ROOT") || define("AUTHENTICATION_ROOT", realpath(dirname(__FILE__)));
}
protected function _initAutoloader()
{
$moduleLoader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'admin',
'basePath' => AUTHENTICATION_ROOT
));
$moduleLoader->addResourceType('forms', '/forms', 'Form');
$moduleLoader->addResourceType('models', '/models', 'Model');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader($moduleLoader);
}

// More bootstrap functions follow...

}

As you can see I am creating a module root constant as the first function in the bootstrap. If you are doing it relatively it may be that your test is messing up since you aren't running the application from the traditional start location in {SITE_ROOT}/public/index.php.

Also it may be worth double checking that you are including your library directory in your application, unless you have installed Zend Framework using PEAR which I assume means that it is already on your include path. Either, you need includePaths.library = APPLICATION_PATH "/../library" in your application.ini, it's XML equivalent, or if you can put a set_include_path command in your initial application startup for the tests;

set_include_path(implode(PATH_SEPARATOR, array(
APPLICATION_PATH.'/../library',
get_include_path()
));

On 12 Oct 2009, at 09:56, huajun qi wrote:

We are trying to build a stable and easy-to-use testing system within Zend Framework.

I created a module named user, and some models such as User_Model_User class under application/modules/user/models,  actually made it work with your help. All models have extended Zend_Db_Table class.

Now I want to test the model. 

I watched a great video tutorial at: http://www.zendcasts.com/unit-testing-with-the-zend-framework-with-zend_test-and-phpunit/2009/06/, and I followed it to bulid testing framework. I have written phpunit.xml, bootstrap file, controllertestcast.php, and UserTest.php file. I add a testing function:

public function testCanItWork(){
$this->assertTrue(true);
}

in UserTest.php. Everything goes well.

When I try to test the model really, errors are reported that User_Model_User are not found, I include it and got another error message "Zend_Db_Table are not found",

I include all required files, but still get errors: Zend_Db_Table has no adapter.

I am totally confused. I don't know how to fix it.

I have found that the application has been bootstraped, but why the classes I need are not autoloaded? 

I have read configurations and register it, set default adapter for Zend_Db_Table in application's bootstrap file, and the application are bootstraped successfully, but why I can not get it?


Marcus Ramsden
Technical Project Assistant
Learning Societies Laboratory, Electronics and Computer Science
University of Southampton
Room 3069, Building 32, Highfield Campus
Southampton SO17 1BJ
------------------------------------
Skype: cuscus1986






--
Location:

Marcus Ramsden
Technical Project Assistant
Learning Societies Laboratory, Electronics and Computer Science
University of Southampton
Room 3069, Building 32, Highfield Campus
Southampton SO17 1BJ
------------------------------------
Skype: cuscus1986






--
Location:

没有评论: