1

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' );
} 
2
  • Same problem here...
    – Sliq
    Commented Sep 28, 2014 at 22:17
  • 1
    Panique, in your connection try, the socket instead of localhost. ie: 'mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock'
    – krisacorn
    Commented Sep 29, 2014 at 23:19

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.