PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu May 24, 2018 10:45 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Couldn't fetch mysqli
PostPosted: Thu Mar 18, 2010 12:40 pm 
Offline
Forum Newbie

Joined: Fri Jul 17, 2009 12:57 pm
Posts: 8
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:

Syntax: [ Download ] [ Hide ]
 
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:
Syntax: [ Download ] [ Hide ]
 
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.


Top
 Profile  
 
PostPosted: Thu Mar 18, 2010 1:27 pm 
Offline
Forum Commoner
User avatar

Joined: Tue Mar 16, 2010 4:06 pm
Posts: 60
Location: San Francisco, California, US
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.


Top
 Profile  
 
PostPosted: Thu Mar 18, 2010 4:12 pm 
Offline
Forum Newbie

Joined: Fri Jul 17, 2009 12:57 pm
Posts: 8
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.


Top
 Profile  
 
PostPosted: Thu Mar 18, 2010 6:27 pm 
Offline
Forum Commoner
User avatar

Joined: Tue Mar 16, 2010 4:06 pm
Posts: 60
Location: San Francisco, California, US
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.

Syntax: [ Download ] [ Hide ]
 
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.

Syntax: [ Download ] [ Hide ]
 
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;
}
 


Top
 Profile  
 
PostPosted: Fri Mar 19, 2010 8:55 am 
Offline
Forum Newbie

Joined: Fri Jul 17, 2009 12:57 pm
Posts: 8
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.


Top
 Profile  
 
PostPosted: Fri Mar 19, 2010 12:21 pm 
Offline
Forum Commoner
User avatar

Joined: Tue Mar 16, 2010 4:06 pm
Posts: 60
Location: San Francisco, California, US
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.


Top
 Profile  
 
PostPosted: Wed Apr 07, 2010 10:24 am 
Offline
Forum Newbie

Joined: Fri Jul 17, 2009 12:57 pm
Posts: 8
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group