2010-07-28 69 views
1

Symfony似乎在運行單元測試時打開數據庫連接時出現問題。我有我的測試中ENV的config/databases.yml裏規定:運行symfony單元測試返回「沒有開放連接」

 
all: 
    doctrine: 
    class: sfDoctrineDatabase 
    param: 
     dsn: 'mysql:host=localhost;dbname=ms' 
     username: ms 
     password: xxx 
test: 
    doctrine: 
    class: sfDoctrineDatabase 
    param: 
     dsn: 'mysql:host=localhost;dbname=ms' 
     username: ms 
     password: xxx 
... 

,並在(測試/單位/ MSTest.php)的測試文件看起來如下:

 
require_once dirname(__FILE__).'/../bootstrap/unit.php'; 
$a = new Article(); 
$a->setHeadline("Article Headline"); 
$a->save(); 

當我嘗試運行測試使用「symfony測試:單位MS」它只是返回「沒有開放連接」並退出。單獨運行測試(PHP測試/單位/ MSTest.php)返回完整的堆棧跟蹤:

 
C:\phpworkspace\ms>php test/unit/MSTest.php 

Fatal error: Uncaught exception 'Doctrine_Connection_Exception' with message 'There is no open connection' in C:\phpworkspace\ms\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Manager.php:662 
Stack trace: 
#0 C:\phpworkspace\ms\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Manager.php(557): Doctrine_Manager->getCurrentConnection() 
#1 C:\phpworkspace\ms\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Core.php(1095): Doctrine_Manager->getConnectionForComponent('Article') 
#2 C:\phpworkspace\ms\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Record.php(219): Doctrine_Core::getTable('Article') 
#3 C:\phpworkspace\ms\test\unit\UniversalDemoTest.php(15) 
: Doctrine_Record->__construct() 
#4 {main} 
    thrown in C:\phpworkspace\ms\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Manager.php on line 662 

我周圍的一派,並好像所有的線索直接到database.yml的測試:環境中(看上面)是到位。我在命令行上測試了用戶名/密碼/數據庫名稱,並且用戶證明是有效的。該other solution described here是從

 
$configuration = ProjectConfiguration::hasActive() ? 
    ProjectConfiguration::getActive() : 
    new ProjectConfiguration(realpath($_test_dir.'/..')); 

替換unit.php行至

 
$configuration = ProjectConfiguration::getApplicationConfiguration(
    'frontend', 'test', true 
); 

但對我來說,似乎並沒有幫助的。它在兩種情況下加載配置,但返回不同的對象(print_r($ configuration)) - 默認情況下爲ProjectConfiguration,新代替行時爲frontendConfiguration;最後一個似乎有所有正確的配置指令(來自app.xml文件的app_xxx)。請諮詢 - 我可以在哪裏看?我一直在清理我的緩存(symfony cc),創建單獨的數據庫進行測試,重新加載模型/夾具,至今沒有任何工作。這些模型是有效的,並已在各地的應用程序中使用,因此它必須與測試環境有關。

我使用symfony 1.4(XAMPP)運行PHP 5.3.1,mysql 5.1.41。

謝謝。

回答

6

需要初始化數據庫管理器使用學說的模型類:

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true); 
new sfDatabaseManager($configuration); 
+0

這就是它!添加「新的sfDatabaseManager($配置);」在修改過的行之後做了這個訣竅,非常感謝! – dsomnus 2010-07-29 16:07:22