I am writing some tests with phpunit for several classes. each of the classes has the following method:
function getDbh() {
if ($this->dbh === null){
$this->dbh = Slim::getInstance()->db->getConnection();
}
return $this->dbh;
}
but the problem is that after the first test slim create this Environment singleton that i have no clue i can i use in following tests.
to make my specific issue a bit clearer, each of my test classes has this method:
public function testGetDbh_dbhIsNull()
{
$fixture = new testedClass();
$app = new Slim();
$DB = $this->getMockBuilder('DB')
->disableOriginalConstructor()
->getMock();
$DB->method('getConnection')->willReturn('connection');
$app->db = $DB;
$this->assertEquals($fixture->getDbh(), 'connection');
}
but from the second test, the test fails due to the following error:
1) GroupTest::testSlim
Failed asserting that 'connection' matches expected null.
any idea how can i use the Slim singleton in each of the tests? thx
Your test is wrong :)
public function testGetDbh_dbhIsNull()
{
$fixture = new testedClass();
$app = new Slim();
$DB = $this->getMockBuilder('DB')
->disableOriginalConstructor()
->getMock();
$DB->method('getConnection')->willReturn('connection');
$app->db = $DB;
//This points to the wrong object... This should fix it
$this->assertEquals($app->db, 'connection');
}