Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")
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.
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.
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.
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.
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.
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.
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.
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.
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.