2010-08-19 25 views
2

這裏有人使用Zend Framework,ZFDoctrine和PHPUnit在一起嗎?ZF,ZFDoctrine和PHPUnit安裝程序

如何在每次測試運行時重建數據庫? 如何區分本地/生產/測試環境?

你會分享你的單元測試設置嗎?

我一直在嘗試這樣的事情:

// /tests/bootstrap.php 
// ... setup paths and constants here 
require_once 'Zend/Application.php'; 
// Create application, bootstrap, and run 
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini' 
); 

$application->bootstrap('doctrine'); 
$provider = new ZFDoctrine_Tool_DoctrineProvider; 
$provider->generateModelsFromYaml(); 
//$provider->buildProject(true); 

但這結束在:不會產生

Notice: Constant APPLICATION_PATH already defined in /home/user/www/library/ZendFramework/1.10.7/library/Zend/Tool/Project/Context/Zf/BootstrapFile.php on line 106 

Fatal error: Call to a member function getResponse() on a non-object in /home/user/www/library/zf-doctrine/library/ZFDoctrine/Tool/DoctrineProvider.php on line 271 

模型。

我得到的運行類似的錯誤:

$provider->createDatabase(); 

但在這種情況下,創建數據庫。
其他提供程序命令不起作用。


解決辦法:

$provider = new ZFDoctrine_Tool_DoctrineProvider; 
$registry = new Zend_Tool_Framework_Registry; 
$provider->setRegistry($registry); 
@$provider->buildProject(true); 

如果有人知道一個更好的方法,請大家指正。

+0

很好,謝謝! – Daff 2010-12-22 20:50:38

回答

0

我沒有使用ZFDoctrine,但只是簡單的教條1.2。我不知道如果我的解決方案是更好,但我想我後,如果ANY1有興趣,這裏是在我的測試文件夾中的bootstrap.php:

<?php 

// Define path to application directory 
defined('APPLICATION_PATH') 
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application')); 

// Define application environment 
/** 
* In the application.ini: 
[testing : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
doctrine.dsn = "mysql://my_user:[email protected]/my_phpunit_test_db" 
*/ 
define('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'; 

// Create application, bootstrap, and run 
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/../configs/application.ini' 
); 

$application->getBootstrap()->bootstrap(); 

// Can run out if too small 
ini_set('memory_limit', '512M'); 

// Get the doctrine settings 
$config = $application->getOption('doctrine'); 
$cli = new Doctrine_Cli($config); 
$cli->run(array("doctrine", "build-all-reload","force")); 

這裏的關鍵實際上是重建所有數據庫創建的最後一行每個測試都有一個乾淨的環境。