Page MenuHomePhabricator

MediaWikiIntegrationTestCase does not clear tablesUsed before first test
Open, Needs TriagePublic

Description

MediaWikiIntegrationTestCase has some code in its run() method that’s supposed to clear the tables which the test uses before the first test run, added by @daniel in I8c33be7b1b:

if ( $this->oncePerClass() ) {
	$this->setUpSchema( $this->db );
	$this->resetDB( $this->db, $this->tablesUsed );
	$this->addDBDataOnce();
}

However, if you step through this with the debugger, you can see that $this->tablesUsed is actually empty at this point. This is because it’s usually populated in setUp() – but that method is only called when MediaWikiIntegrationTestCase::run() calls parent::run(), which hasn’t happened at this point yet. (This may have been different in an earlier PHPUnit version, I don’t know.) Is there some way to fix this?

Event Timeline

Just hit the same curiosity and it is indeed rather annoying. Used 'truncateTable' directly in the setup, but that's not the right way to go.

I think addDBDataOnce is more fundamentally broken, and shouldn't be used.

But tablesUsed works correctly for addDBData, as long as you mutate it in addDBData. That seems to be the expected use case.

Having tablesUsed be empty in the first call to resetDB is correct -- there are other things which get reset there, so the call isn't a no-op, but the DB should still be clean at that point. (If it's not then you are probably defensive coding against some *other* test case which is leaving crap in the DB, and you should instead fix that other test case not to leave crap around.) You shouldn't mutate the DB in setup, you should do that in addDBData or addDBDataOnce.

In addDBDataOnce you should really have a tablesUsedOnce instance variable which is mutated only by addDBDataOnce. And what's missing is a 'tearDownOnce` which would then call resetDB at the very end of the class on the tablesUsedOnce.

That way every test class starts with a clean DB, and ends with a clean DB.