How to make PHPUnit tests use a different DB?

Hi, I would like to add a few unit tests to our test cases that (contrary to before) also would write some records into the DB. I would like to setup an own test-DB for this purpose to avoid any remnants from failed unit-tests. Is there a way to tell phpunit to use other DB-credentials than the one specified in the config.php?

I found this page but this whole setup seems to be extreme overkill for what I am trying to achieve: https://docs.suitecrm.com/developer/automatedtesting/#_setup

Is there maybe more light-scale way of sneaking in other host/db-name/user/pw credentials on phpunit loading?

Without having tested or knowledge if this will work, you could try adding and removing the values to config_override.php

$sugar_config['db_password'] = 'tempdbpassword';

This may if it works may get the db to use the other db until the values are removed from the config_override.pgp

Thanks. Not exactly what I wanted to do. What could work (it seems to work at least) you override the connection to another system after the ‘default’ environment started via phpunit.

I just would have to make sure that the central DB handle is overwritten globally or that each unittest really sets this up correct. If you do a truncate by accident on a production DB with the intention to clean up your unittest records you dig your own grave :o.

e.g.

  class LiveDBTest extends PHPUnit_Framework_TestCase{
    	    
    	public function testDB(){
    		$config =   array (
    					'db_host_name' => 'localhost',
    					'db_host_instance' => 'SQLEXPRESS',
    					'db_user_name' => 'yyyyy',
    					'db_password' => 'xxxx',
    					'db_name' => 'ZZZZZ',
    					'db_type' => 'mysql',
    					'db_port' => '',
    					'db_manager' => 'MysqliManager'
    				  );
    		$myDB = $GLOBALS['db'];
                 # reconnect forcefully
    		$myDB->connect($config, false);
    		$result = $myDB->query("SELECT * from accounts LIMIT 1");
    		while ( $row = $myDB->fetchByAssoc($result) ){
    			echo $row['id'] . " " . $row['name'];
    		}
    	}
    }

I have ran into some problems when I am trying to unittest a class that uses internally the SuiteCRM Bean API.
I get as error:

Undefined index: rname

which origins in data\SugarBean.php:3998

the bean-call that causes this exception looks like this:
$bean->get_list("name", "<module>.name = '$name'");

within the regular interative usage this works without problems. Just calling it in a unittest environment the call fails. Is there more to do to setup this SugarBean API correctly? The code in the reported lines indeed expects a field ‘rname’ to be there. I am not sure when/how to populate it in a unittest setup?

I also faced the same problem.
I configured the following settings to connect to a different database in the test environment.
This is the solution for version 7.14.

  1. copy tests/config.test.dist.php to tests/config.test.php and add the code below.
if (file_exists(__DIR__ . '/../.env.test')) {
     $dotenv = Dotenv\Dotenv::create(__DIR__ . '/../', '.env.test');
     $dotenv->overload();
}
  1. create .env.test file and define DATABASE_NAME
  2. add the code below to config_override.php
if (getenv('DATABASE_NAME')) {
     $sugar_config['dbconfig']['db_name'] = getenv('DATABASE_NAME');
}