1
votes

Symfony seems to have a problem opening a database connection while running unit test. I have my test env specified in config/databases.yml:

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
...

and the test file at (test/unit/MSTest.php) looks following:

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

When i try to run the test using "symfony test:unit MS" it simply returns "There is no open connection" and exits. Running the test alone (php test/unit/MSTest.php) returns full stack trace:

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

I googled it around and seems like all the hints direct to database.yml test: environment which (look above) is in place. I tested that username/password/db name on command line and the user is certanly valid. The other solution described here is to replace the line in unit.php from

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

to

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

but in my case that doesn't seem to help either. It loads the configuration in both cases but returns different object (print_r($configuration)) as a result - ProjectConfiguration in case of default and frontendConfiguration in case of newly replaced line; the last one seems to have all proper configuration directives (app_xxx from app.xml file). Please advice - where else could i look? I have been clearing my cache (symfony cc), creating separate database for testing, reloading models/fixtures and so far nothing works. The models are valid and already used in application all over the place, so it has to be something to do with test environment.

I'm running PHP 5.3.1, mysql 5.1.41 with symfony 1.4 (XAMPP).

Thanks.

1

1 Answers

6
votes

You need to initialize database manager to use doctrine's model classes:

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