Page 1 of 1

Couldn't fetch mysqli

Posted: Thu Mar 18, 2010 12:40 pm
by Delpheno
For PHPUnit, it seems like I get an error Couldn't fetch mysqli for mysqli::real_escape_string when it comes to database testing.

Here's what's happening. Pretend FunctionA() uses the method real_escape_string() inside:

Code: Select all

 
class Test
{
 
    public function __construct
    {
        //stuff
     }
 
    public getConnection()
    {
        //stuff
    }
    public function testFunctionA()
    {
        FunctionA();
    }
 
    public function testFuncationAForAnotherFeature()
    {
        //do stuff
        FunctionA();
    }
}
 
I'll get the Couldn't fetch error when testFunctionAForAnotherFeature() runs.

However, if I have this:

Code: Select all

 
public function testFunctionA()
{
    FunctionA();
 
    //do stuff
    FunctionA();
}
 
And eliminate testFunctionAForAnotherFeature()... everything works fine.

The thing is, I want separate test functions to organize what I'm testing, but PHPUnit will give me a mysqli error if I have more than one test function.

Is there a way around this?

Thank you.

Re: Couldn't fetch mysqli

Posted: Thu Mar 18, 2010 1:27 pm
by Sofw_Arch_Dev
It's not clear when your getConnection() function is getting called from the code you show, but maybe a first step would be to ensure that you have a connection at the start of each test. Generally you get all necessary resources for a test in your public setUp function, which is called by PHPUnit before each test is run. Similarly your public tearDown function is called after each test is run, thus ensuring a clean slate for each test.

Re: Couldn't fetch mysqli

Posted: Thu Mar 18, 2010 4:12 pm
by Delpheno
I did use the setUp() function, but I used __construct in its place. From what I understand, the getConnection() needs to exist for PHPUnit database tests. I'll make a few changes and see where that gets me.

Thank you.

Re: Couldn't fetch mysqli

Posted: Thu Mar 18, 2010 6:27 pm
by Sofw_Arch_Dev
You're right. getConnection() is a PHPUnit function called by PHPUnit on your test class. So you should be safe there. If the problem occurs going from one test to another, I wonder if it isn't an issue with how the connection is being released. I'm looking at this PHPUnit definition of getConnection() from the PHPUnit manual.

Code: Select all

 
protected function getConnection()
{
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
    return $this->createDefaultDBConnection($pdo, 'testdb');
}
 
Maybe put an echo (or var_dump) in the getConnection() function before the first line so you can tell if/when the function getting called. Perhaps something like this would be revealing.

Code: Select all

 
private $_myConn  = null;
...
protected function getConnection()
{
    var_dump( $this->_myConn );
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
    $this->_myConn =  $this->createDefaultDBConnection($pdo, 'testdb');
    return $_myConn;
}
 

Re: Couldn't fetch mysqli

Posted: Fri Mar 19, 2010 8:55 am
by Delpheno
Okay so apparently we don't need getConnection() or getDataSet(), at least from what I'm testing (I commented the two functions out, ran the test with no PHPUnit error). Those are needed if you want to compare tables, and that is not what I'm doing. So I don't think the problem is in getConnection().

To restate so we're all on the same page here, the problem is moving from one test function to another using mysqli. Doing so provokes the couldn't fetch mysqli error. This prevents me from using having a testsuite of database testcases and/or organizing what I'm testing in each class by having separate functions.

For now I'm going to run all my tests in one function, and not have a testsuite until a solution pops up.

Thank you.

Re: Couldn't fetch mysqli

Posted: Fri Mar 19, 2010 12:21 pm
by Sofw_Arch_Dev
It's interesting that you have this problem. Too bad I can't point out the issue. I have numerous tests which involve writing to and reading from DB tables yet I have not encountered this problem. We deploy the Zend framework, and extend Zend_Db_Adapter_Mysqli. We don't implement the getConnection() function in any of our tests. We simply instantiate a Zend_Db_Adapter_Mysqli object stored as a global, and have a standard way for beans to get that Mysqli object to do their DB interactions. The test doesn't know anything about the DB connection. It just instructs the bean to do its work, and asserts what is expected. If you ever resolve it, please post an update.

Re: Couldn't fetch mysqli

Posted: Wed Apr 07, 2010 10:24 am
by Delpheno
So I figured out how to solve this I think. I tried to implement it but it didn't work.

What you do is you connect to the database in the test suite class, not the test case class. Again, I haven't implemented it, but it seems like that's the solution.