This is probably an easy one for some of you. I'm trying to test a protected method on a small DB connection class I have.
Relevant code is as follows:
class DbConnect{
/**
* Connexion MSSQL local
*/
protected function localConnect($localconfig){
$connectionInfo = array("UID" => $localconfig->uid,
"PWD" =>$localconfig->pwd,
"Database"=> $localconfig->DB);
$this->localConnection = sqlsrv_connect($localconfig->serverName,
$connectionInfo);
if( $this->localConnection === false ){
$sql_error = sqlsrv_errors();
throw new DBException("Error in DB Connection.\r\n
SQL ERROR:" . $sql_error);
}
}
}
To test the method, I had the bright idea (probably from a post here somewhere) to subclass and call from there. I created a subclass, right at bottom of my test file. I obviously could not override the visibility of the method to public, so decided another approach in the stub: declare a public method that calls the parent's protected localConnect method:
class DBConnectStub extends DBconnect{
public function callLocalConnect($localConfig){
parent::localConnect($localConfig);
}
}
My test now looks like this:
/**
* @expectedException DBException
*/
public function test_localConnectError(){
$localconfig = (object) array ( 'serverName' => 'nohost',
'uid' => 'nouid',
'pwd' => 'noPwd',
'DB' => 'noDB'
);
$db = DbConnectStub::getInstance($localconfig, array());
$db->callLocalConnect($localConfig);
unset($db);
}
The weird part, when I run the test, php spits out:
Fatal error: Call to undefined method DbConnect::callLocalConnect() in C:\tirelinkCRMsync\test \tirelinkCRMSync\DBConnectTest.php on line 82.
The object is properly instanciated, but why is the method not defined, surely there is a detail that has eluded me. Is this approach valid or is there a better way?