From dc18b813e81bbd83ff9986fedd1eef1b201132f4 Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 23 Oct 2013 17:01:33 +0100 Subject: [PATCH] Cleanup Api phpunit Tests - Splits multiple classes into individual files - Adds @covers tags - Fixes scope Change-Id: I7d2816d3574fa53a2aaa8e2a84b7a7ecdd245252 --- tests/TestsAutoLoader.php | 6 +- tests/phpunit/includes/api/ApiBlockTest.php | 24 ++----- ...ationTest.php => ApiCreateAccountTest.php} | 4 +- .../phpunit/includes/api/ApiEditPageTest.php | 2 + tests/phpunit/includes/api/ApiOptionsTest.php | 10 ++- tests/phpunit/includes/api/ApiParseTest.php | 2 + tests/phpunit/includes/api/ApiPurgeTest.php | 2 + tests/phpunit/includes/api/ApiTestCase.php | 65 +------------------ tests/phpunit/includes/api/ApiTestContext.php | 21 ++++++ tests/phpunit/includes/api/ApiUnblockTest.php | 31 +++++++++ tests/phpunit/includes/api/MockApi.php | 20 ++++++ .../includes/api/PrefixUniquenessTest.php | 2 + tests/phpunit/includes/api/UserWrapper.php | 25 +++++++ 13 files changed, 127 insertions(+), 87 deletions(-) rename tests/phpunit/includes/api/{ApiAccountCreationTest.php => ApiCreateAccountTest.php} (98%) create mode 100644 tests/phpunit/includes/api/ApiTestContext.php create mode 100644 tests/phpunit/includes/api/ApiUnblockTest.php create mode 100644 tests/phpunit/includes/api/MockApi.php create mode 100644 tests/phpunit/includes/api/UserWrapper.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 00ce13c893..dec8e22dd1 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -55,10 +55,10 @@ $wgAutoloadClasses += array( # tests/phpunit/includes/api 'ApiFormatTestBase' => "$testDir/phpunit/includes/api/format/ApiFormatTestBase.php", 'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php", - 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestCase.php", - 'MockApi' => "$testDir/phpunit/includes/api/ApiTestCase.php", + 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestContext.php", + 'MockApi' => "$testDir/phpunit/includes/api/MockApi.php", + 'UserWrapper' => "$testDir/phpunit/includes/api/UserWrapper.php", 'RandomImageGenerator' => "$testDir/phpunit/includes/api/RandomImageGenerator.php", - 'UserWrapper' => "$testDir/phpunit/includes/api/ApiTestCase.php", # tests/phpunit/includes/content 'DummyContentHandlerForTesting' => "$testDir/phpunit/includes/content/ContentHandlerTest.php", diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index 8afb748a8d..d98eec6a67 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiBlock */ class ApiBlockTest extends ApiTestCase { protected function setUp() { @@ -11,7 +13,7 @@ class ApiBlockTest extends ApiTestCase { $this->doLogin(); } - function getTokens() { + protected function getTokens() { return $this->getTokenList( self::$users['sysop'] ); } @@ -63,17 +65,13 @@ class ApiBlockTest extends ApiTestCase { } /** - * Attempting to block without a token should give a UsageException with - * error message: - * "The token parameter must be set" - * - * @dataProvider provideBlockUnblockAction * @expectedException UsageException + * @expectedExceptionMessage The token parameter must be set */ - public function testBlockingActionWithNoToken( $action ) { + public function testBlockingActionWithNoToken( ) { $this->doApiRequest( array( - 'action' => $action, + 'action' => 'block', 'user' => 'UTApiBlockee', 'reason' => 'Some reason', ), @@ -82,14 +80,4 @@ class ApiBlockTest extends ApiTestCase { self::$users['sysop']->user ); } - - /** - * Just provide the 'block' and 'unblock' action to test both API calls - */ - public static function provideBlockUnblockAction() { - return array( - array( 'block' ), - array( 'unblock' ), - ); - } } diff --git a/tests/phpunit/includes/api/ApiAccountCreationTest.php b/tests/phpunit/includes/api/ApiCreateAccountTest.php similarity index 98% rename from tests/phpunit/includes/api/ApiAccountCreationTest.php rename to tests/phpunit/includes/api/ApiCreateAccountTest.php index 68f80ac9e8..a7232457b6 100644 --- a/tests/phpunit/includes/api/ApiAccountCreationTest.php +++ b/tests/phpunit/includes/api/ApiCreateAccountTest.php @@ -4,9 +4,11 @@ * @group Database * @group API * @group medium + * + * @covers ApiCreateAccount */ class ApiCreateAccountTest extends ApiTestCase { - function setUp() { + protected function setUp() { parent::setUp(); LoginForm::setCreateaccountToken(); $this->setMwGlobals( array( 'wgEnableEmail' => true ) ); diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 0c49b12b97..7521dcf743 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -8,6 +8,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiEditPage */ class ApiEditPageTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiOptionsTest.php b/tests/phpunit/includes/api/ApiOptionsTest.php index ad1e73ab4a..3168f323c0 100644 --- a/tests/phpunit/includes/api/ApiOptionsTest.php +++ b/tests/phpunit/includes/api/ApiOptionsTest.php @@ -4,10 +4,18 @@ * @group API * @group Database * @group medium + * + * @covers ApiOptions */ class ApiOptionsTest extends MediaWikiLangTestCase { - private $mTested, $mUserMock, $mContext, $mSession; + /** @var PHPUnit_Framework_MockObject_MockObject */ + private $mUserMock ; + /** @var ApiOptions */ + private $mTested; + private $mSession; + /** @var DerivativeContext */ + private $mContext; private $mOldGetPreferencesHooks = false; diff --git a/tests/phpunit/includes/api/ApiParseTest.php b/tests/phpunit/includes/api/ApiParseTest.php index 2d714e65a2..d303d4b0d2 100644 --- a/tests/phpunit/includes/api/ApiParseTest.php +++ b/tests/phpunit/includes/api/ApiParseTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiParse */ class ApiParseTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiPurgeTest.php b/tests/phpunit/includes/api/ApiPurgeTest.php index 28b5ff4d4d..d25a4c1d31 100644 --- a/tests/phpunit/includes/api/ApiPurgeTest.php +++ b/tests/phpunit/includes/api/ApiPurgeTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiPurge */ class ApiPurgeTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index 94ef9c6868..ad297dd935 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -117,7 +117,7 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { * @param $params Array: key-value API params * @param $session Array|null: session array * @param $user User|null A User object for the context - * @return result of the API call + * @return mixed result of the API call * @throws Exception in case wsToken is not set in the session */ protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) { @@ -188,66 +188,3 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { ); } } - -class UserWrapper { - public $userName; - public $password; - public $user; - - public function __construct( $userName, $password, $group = '' ) { - $this->userName = $userName; - $this->password = $password; - - $this->user = User::newFromName( $this->userName ); - if ( !$this->user->getID() ) { - $this->user = User::createNew( $this->userName, array( - "email" => "test@example.com", - "real_name" => "Test User" ) ); - } - $this->user->setPassword( $this->password ); - - if ( $group !== '' ) { - $this->user->addGroup( $group ); - } - $this->user->saveSettings(); - } -} - -class MockApi extends ApiBase { - public function execute() { - } - - public function getVersion() { - } - - public function __construct() { - } - - public function getAllowedParams() { - return array( - 'filename' => null, - 'enablechunks' => false, - 'sessionkey' => null, - ); - } -} - -class ApiTestContext extends RequestContext { - - /** - * Returns a DerivativeContext with the request variables in place - * - * @param $request WebRequest request object including parameters and session - * @param $user User or null - * @return DerivativeContext - */ - public function newTestContext( WebRequest $request, User $user = null ) { - $context = new DerivativeContext( $this ); - $context->setRequest( $request ); - if ( $user !== null ) { - $context->setUser( $user ); - } - - return $context; - } -} diff --git a/tests/phpunit/includes/api/ApiTestContext.php b/tests/phpunit/includes/api/ApiTestContext.php new file mode 100644 index 0000000000..43637c2344 --- /dev/null +++ b/tests/phpunit/includes/api/ApiTestContext.php @@ -0,0 +1,21 @@ +setRequest( $request ); + if ( $user !== null ) { + $context->setUser( $user ); + } + + return $context; + } +} \ No newline at end of file diff --git a/tests/phpunit/includes/api/ApiUnblockTest.php b/tests/phpunit/includes/api/ApiUnblockTest.php new file mode 100644 index 0000000000..2c2370a8eb --- /dev/null +++ b/tests/phpunit/includes/api/ApiUnblockTest.php @@ -0,0 +1,31 @@ +doLogin(); + } + + /** + * @expectedException UsageException + */ + public function testWithNoToken( ) { + $this->doApiRequest( + array( + 'action' => 'unblock', + 'user' => 'UTApiBlockee', + 'reason' => 'Some reason', + ), + null, + false, + self::$users['sysop']->user + ); + } +} diff --git a/tests/phpunit/includes/api/MockApi.php b/tests/phpunit/includes/api/MockApi.php new file mode 100644 index 0000000000..3686048cc0 --- /dev/null +++ b/tests/phpunit/includes/api/MockApi.php @@ -0,0 +1,20 @@ + null, + 'enablechunks' => false, + 'sessionkey' => null, + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/includes/api/PrefixUniquenessTest.php b/tests/phpunit/includes/api/PrefixUniquenessTest.php index d9be85e320..a1fc2c296e 100644 --- a/tests/phpunit/includes/api/PrefixUniquenessTest.php +++ b/tests/phpunit/includes/api/PrefixUniquenessTest.php @@ -6,6 +6,7 @@ * @group API */ class PrefixUniquenessTest extends MediaWikiTestCase { + public function testPrefixes() { $main = new ApiMain( new FauxRequest() ); $query = new ApiQuery( $main, 'foo', 'bar' ); @@ -13,6 +14,7 @@ class PrefixUniquenessTest extends MediaWikiTestCase { $prefixes = array(); foreach ( $modules as $name => $class ) { + /** @var ApiMain $module */ $module = new $class( $main, $name ); $prefix = $module->getModulePrefix(); if ( isset( $prefixes[$prefix] ) ) { diff --git a/tests/phpunit/includes/api/UserWrapper.php b/tests/phpunit/includes/api/UserWrapper.php new file mode 100644 index 0000000000..3262e6ccbc --- /dev/null +++ b/tests/phpunit/includes/api/UserWrapper.php @@ -0,0 +1,25 @@ +userName = $userName; + $this->password = $password; + + $this->user = User::newFromName( $this->userName ); + if ( !$this->user->getID() ) { + $this->user = User::createNew( $this->userName, array( + "email" => "test@example.com", + "real_name" => "Test User" ) ); + } + $this->user->setPassword( $this->password ); + + if ( $group !== '' ) { + $this->user->addGroup( $group ); + } + $this->user->saveSettings(); + } +} \ No newline at end of file -- 2.20.1