MediaWiki PHP unit tests : Reading contents from a file ( @dataProvider )

While writing php unit tests for the BounceHandler extension, I came across a scenario in which I had to read contents from a text file and feed that to the test function as a variable. With the dataProvider functions it was solved simple.

* function testProcessTextfile( $foo ) function requires $foo to be read from a file from bar/foo.txt

In the class extending ApiTestCase give :

public static function provideFoo() {
	$foo = file_get_contents( __DIR__ .'/bar/foo.txt' );
	return array (
		array ( $foo )

* @dataProvider provideFoo
* @param $foo
function testProcessTextfile( $foo ) {
	//code to process $foo

That would do ! Happy hacking!
PS: Please note that the following wont work without the correct function comments and headers 🙂

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s