unit tests: MDL-18607 new way to do unit tests involving the database.

This is not as ambitious as the abortive FakeDBUnitTests scheme, but this one works for simple cases.

There is a new test case class UnitTestCaseUsingDatabase to inherit from. I hope it is sufficiently well documented in its PHPdocs.
* It users $CFG->unittestprefix.
* You can access that database using $this->testdb.
* That database is empty by default, you have to call create_test_table to create the ones you want, and drop_test_table to clean them up in the end. The table definitions are read from the XMLDB file.
* When you are ready to call real Moodle code that users $DB, call switch_to_test_db and then revert_to_real_db when you are done.
* If you forget to call drop_test_table or switch_to_test_db, the class will attempt to clean up after you, but will also print rude developer debug messages telling you not to be stupid.
* There is also a load_test_data method for populating a table from an array.

The is an example of its use in lib/simpletest/testunittestusingdb.php.
This commit is contained in:
tjhunt
2009-03-23 04:12:37 +00:00
parent 67d5e9df39
commit f68cb08bbe
4 changed files with 234 additions and 10 deletions
+9 -8
View File
@@ -595,17 +595,18 @@ class TestSuite {
for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) {
if (is_string($this->_test_cases[$i])) {
$class = $this->_test_cases[$i];
$test = &new $class();
// moodle hack start
// moodle hack start - need to do this before the constructor call, because of FakeDBUnitTestCase.
global $CFG;
if (empty($CFG->unittestprefix)) {
if ($test instanceof FakeDBUnitTestCase) {
// do not execute this test because test tables not present!
unset($test);
continue;
}
if (is_subclass_of($class, 'FakeDBUnitTestCase')) {
// Do not execute this test because the test tables system no longer works.
continue;
}
if (is_subclass_of($class, 'UnitTestCaseUsingDatabase') && empty($CFG->unittestprefix)) {
// Do not execute this test because $CFG->unittestprefix is not set, but it will be required.
continue;
}
// moodle hack end
$test = &new $class();
$test->run($reporter);
unset($test);
} else {