I am trying to work with PHPUnit on some legacy code. The idea is I need to find out if using PHPUnit in the future is viable or feasible for the company. One problem I'm having is that we use a lot of define()
functions to set up our database information. Specifically, we use 'DB_HOST'
to substitute over our database's name. I'm noticing that PHPUnit isn't liking this method of getting everything uniform. Here is the relevant code + error.
require_once("./includes/class.casestatusprovider.php");
require_once("./config/config_db.php");
require_once("./config/config_tables.php");
class TestCaseStatusProvider extends TestCase
{
public function setUp()
{
$csp = new CaseStatusProvider();
}
/**
* @covers CaseStatusProvider::insert
*/
public function testInsert_csrIdActive()
{
require_once("./config/config_db.php");
require_once("./config/config_tables.php");
$this->case_id=10;
$this->csr_id=1;
$this->assertNotFalse(insert($this->case_id, $this->csr_id));
}
}
CODE TO BE TESTED
abstract class CaseStatusProvider
{
public static function insert($case_id, $csr_id)
{
global $DBH;
global $session;
if($session->isLoggedIn())
{
try{
$query = "INSERT into ".DB_NAME.".".TBL_CASE_STATUS." (case_id, csr_id, created_by, effective_date, isPast) Values (?, ?, ?, ?, ?) ";
$data = array($case_id, $csr_id, $session->user_id, time(), 0);
$STH = $DBH->prepare($query);
$STH->execute($data);
$fetched = $STH->fetch();
return $fetched;
}catch(PDOException $e) {
echo $e->getMessage();
return false;
}
}
return false;
}
ERROR
Could not connect: Unknown MySQL server host 'DB_HOST'
So, what should we be doing to make this work? As an aside that's slightly relevant, we also can't figure out how to get global variables to work properly for objects (we have a session object and a database object, I don't know why - I'm an intern and this was coded long before I was here).