Question

I'm trying to test my Yii-Application with PHPUnit and getting a CDbException. I'm getting the same Exception when I'm using Yii::app()->db.

My PHP-Test-Code:

$connection=new CDbConnection('mysql:host=localhost;dbname=testdb','user','pw');
$sql = "SELECT * FROM Site";
$command=$connection->createCommand($sql);
$rowCount=$command->execute();
echo $rowCount;

The Exception:

CDbException: CDbConnection failed to open the DB connection.

/opt/lampp/htdocs/cms/framework/db/CDbConnection.php:388
/opt/lampp/htdocs/cms/framework/db/CDbConnection.php:330
/opt/lampp/htdocs/cms/framework/db/CDbConnection.php:480
/opt/lampp/htdocs/cms/protected/tests/unit/TestTest.php:26

When I'm calling index-test.php in my Browser, Yii connects correctly to the test DB, I've defined in the test.php config.

I hope you can help me with this little problem.

Was it helpful?

Solution 2

I've come up with two solutions for this problem:

First adding the mysql.sock to the test.php config:

'db'=>array(
      'connectionString'=>'mysql:host=localhost;dbname=trackstar_dev;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
      'username'=>'root',
      'password'=>'root',
      'emulatePrepare'=>true
),

Second using the xampp php to run phpunit:

$ /pathtoxampp/bin/php phpunit /yourtestpath/

OTHER TIPS

Once I have also suffered from the similar problem. the following helped for me. Try this once The solution was to add the line below in php-cli.ini :

extension=php_pdo_mysql.dll

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top