When using PHPUnit to test a class that relies on the database, the getDataSet() method provides data to serve as the test fixture for the entire suite of tests. That's great, but how does one provide data for a specific test? It's not unusual, especially when using minimized data sets, for each test to require different database data on top of the common data that all tests use. I know the PDO object is available, and in our case, the application's native DB object is also available (meaning, we can run raw queries or use other functionality in the app), but it would be nice to have a way to insert data that's driven off PHPUnit's DataSet containers so that all test data are handled in the same fashion for improved clarity and easier maintenance.
Is there some way to accomplish this?
You could follow such dirty trick:
protected function getDataSet()
{
if (in_array($this->getName(), array('testA', 'testB', '...'))) {
return $this->createXMLDataSet(__DIR__ . '/_fixtures/fistureA.xml');
}
return $this->createXMLDataSet(__DIR__ . '/_fixtures/fixtureB.xml');
}
Small note: $this->getName()
returns the current test method name
An alternative approach is to re-run set up operations in the begin of the test:
public function testA()
{
$this->getDatabaseTester()->setDataSet($this->createFlatXMLDataSet(__DIR__ . '/_fixtures/fixtureForTestA.xml'));
$this->getDatabaseTester()->onSetUp();
/* your test code */
}
It's a late answer but it could still be useful to some people I guess...
You can accomplish this by calling the execute
method of a IDatabaseOperation
which you can get from PHPUnit_Extensions_Database_Operation_Factory
. You would basically use CLEAN_INSERT
or INSERT
.
As zerkms' second approach, you would call it at the beginning of each test that needs specific data. For instance:
public function testA() {
PHPUnit_Extensions_Database_Operation_Factory::INSERT()
->execute($this->getConnection(), $this->createXMLDataSet(__DIR__.'/fixtureA.xml'));
// Test code
}
However, the advantage of this solution is that the dataset of the whole test case remains unchanged, so:
INSERT
operation (not CLEAN_INSERT
), it will insert rows specific to the test after all rows from the common dataset. You could also use the DELETE
operation to remove some unwanted rows from that common dataset.onSetUp()
makes this work even if the set up operation of the test case has been changed.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With