krisacorn
krisacorn

Reputation: 831

PHPUnit_Extensions_Database_Testcase returns undefined method

Im getting a Fatal error: Call to undefined method PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection::prepare() on a phpunit test where I'm preparing a PDO statement. If the default database connection is a copy of the pdo object that successfully connects, wouldn't it have access to its methods?

My Class and the function in question:

class User
{ 
protected $db; 

public function __construct($db) 
{ 
$this->db = $db; 
} 

public function deleteItem($itemId)
{
$sql = "
DELETE FROM Users WHERE id = ?";
$sth = $this->db->prepare($sql);//this is the failed line works on other tests
return $sth->execute(array($itemId));
}

My Test:

class RosterDBTest extends PHPUnit_Extensions_Database_Testcase 
{ 

public function getConnection() 
{
 $pdo = new PDO('mysql:host=localhost;dbname=users','root','root');
return $this->createDefaultDBConnection($pdo,"users"); 
} 

public function getDataSet()  
{  
return $this->createFlatXMLDataset(  
dirname(__FILE__) . '/users.xml');  
}  

public function setup()
{
$this->db = $this->getConnection();
}

public function testRemoveUser()
{
$testUser = new User($this->db);
$expectedUsers = 123;
$testUser->deleteItem(91);
$totalUsers = $testUsers->getAllUsers();
$this->assertEquals( $expectedUsers,count($totalUsers), 'Did not delete User 91' );
} 

Upvotes: 1

Views: 848

Answers (1)

user2448375
user2448375

Reputation: 11

I've just been stuck on a similar problem where I had my abstract database testcase class. Solved it by changing

public function setup()
{
  $this->db = $this->getConnection();
}

to

public function setup()
{
  $this->db = $this->getConnection()->getConnection();
}

Not sure if this helps with this but hopefully will help someone.

Upvotes: 1

Related Questions