Wikia Developers Wiki


< Nirvana

889pages on
this wiki
Add New Page
Talk0 Share


class WikiaBaseTest extends PHPUnit_Framework_TestCase {...}

This documentation provides examples for Wikia's Base Test class. It has some helpers to make it easier to work with $wf, $wg and other Nirvana features in unit tests. All test classes should extend WikiaBaseTest.


protected function setUp() {...}

The setUp function initializes $app and $appMock and loads the setupFile for the extension if it has one. Most extensions do, and we used to have a hardcoded include_once at the top of every file. This has been moved into the BaseTest, so you just have to define your local setupFile variable and it will be loaded for you. Always call the parent::setUp function.

// Example of usage in your own extension
public function setUp() {
	$this->setupFile =  dirname(__FILE__) . '/../MyExtension_setup.php';

mockClass (deprecated)

protected function mockClass($className, $mock) {...}

The mockClass function basically just calls F::setInstance( $className, $mock ) for you. Since we are deprecating usage of F::build and F::setInstance, this method should also be considered deprecated.

proxyClass (replaces mockClass)

protected function proxyClass($className, $mock, $functionName = null) {...}

The proxyClass function creates a WikiaMockProxy object and overloads the built in PHP "new" function (see the code for more extensive documentation). This allows you to mock an object which is created by a "new" call in your code. In general we should use dependency injection or some other technique to make code more testable, this is a work-around. The only limitation right now is that you can only have one Proxy object per "class". So, you can have a TitleProxy and a UserProxy, but not two different values for Title. Example usage:

$mockArticle = $this->getMock('Article', array('getTitle'), array($mockTitle));
$this->proxyClass('Article', $mockArticle, 'newFromID');
// now anything in your code that calls "Article::newFromID" receives your mock object
  • TODO: we figured out how to make this work with more than one instance, we just have to do it


protected function mockClassWithMethods($className, Array $methods = array(), $staticConstructor = '') {...}

The mockClassWithMethods function calls proxyClass, passes in an array of methods (and optionally a static constructor) and sets up all the mocked methods for you, hopefully eliminating some boilerplate code. It does not otherwise do something useful for you so you have to use the returned mock for something yourself. Example usage:

$mockTitle = $this->mockClassWithMethods('Title', array('exists' => false), 'newFromText');
$this->mockGlobalVariable('wgTitle', $mockTitle);
  • TODO: we should rename this to proxyClassWithMethods since that's what it does
  • TODO: Robert recommends using the PHPUnit MockBuilder fluent interface instead of this, write up how


protected function mockGlobalVariable( $globalName, $returnValue ) {...}

Helper function to mock a global variable. This only works if you use $this->wg in your code.

$wgRequest = $this->getMock('WebRequest', array('getInt', 'getArray'));
// set up wgRequest
$this->mockGlobalVariable('wgRequest', $wgRequest);
// you don't have to just mock objects:
$this->mockGlobalVariable( 'wgCityId', self::TEST_CITY_ID );


protected function mockGlobalFunction( $functionName, $returnValue, $callsNum = 1, $inputParams = array() ) {...}

Helper function to mock a global function.

$this->mockGlobalFunction( 'getDB', $dbMock );
// mockMessage only overloads the "msg" function so you might have to do this:
$this->mockGlobalFunction( 'MsgExt', $expMsg, $mockMsgExtCount );


protected function mockClassStaticMethod($className, $methodName, $retVal) {...}

Helper function to mock a static method of an object.

// mock API response
$respMock = json_encode(array(
    'response' => array(
    'code' => $apiResponse
$this->mockClassStaticMethod('Http', 'post', $respMock);


protected function mockMessage($messageName, $messageContent) {

This just calls mockGlobalFunction for "msg".

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.