MediaWiki Unit Tests : Truncating table after each test

Writing PHP unit tests for the BounceHandler extension to prune old Bounce Records, I got into a situation where I had to truncate the ‘bounce_records’ and ‘user’ table after each single test. You would want the same when the test manipulates part of your table – and you dont want those changes to show up in your next test. Initially, I did this by manually truncating rows in both the table, but later I came across the use of

$this->tablesUsed = array( ‘table_name1’, ‘table_name2’ );

Implemented by :

class DoSomethingTest extends MediaWikiTestCase {
	protected function setUp() {
		// Your test initialization code here
		$this->tablesUsed = array( 'tableName' );

This would truncate ‘tableName’ table after each test ! Yay.
protected $tablesUsed = array(); is declared in MediaWikiTestCase.php[1] and its contents truncated after each test run !

[Ref : ]

Writing PHP unit-tests : How to add a fake user table entry

Flexibility of the Mediawiki PHP unit-tests wrapper extends to the fact that fake database entries can be made and tested upon without causing any harm to the actual one.
As I scribbled in the earlier post, the class-comments play a vital role, and dont forget to give them like this:

 * Class to test table lookups
 * @group Database
class ModifyUserTableTest extends MediaWikiTestCase {
	function testModifyUserTable() {
        // Lets add a fake user named TestUser, and check that his email is confirmed and then get his User Id  
		$user = User::newFromName( 'TestUser' );
		$user->setEmail( 'bob@example.ext' );


		$this->assertTrue( $user->isEmailConfirmed() );

		$uid = $user->getId();

This would create a fake user TestUser in a fake table – mediawiki_unittest. Later this user can be used for any other testing purpose within the class ! Yay!