From: Aryeh Gregor Date: Tue, 31 Jul 2018 13:19:10 +0000 (+0300) Subject: New helper ApiTestCase::setExpectedApiException() X-Git-Tag: 1.31.2~18 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=2f6c4da1aead080b8b390b5c9d419bd36b15bf03 New helper ApiTestCase::setExpectedApiException() This allows setting the expected exception message by the message key, not text, so it remains correct if the message is updated. This function could be defined to work with other exception types too, but it seems useful to have shortcuts for common types like ApiUsageException or MWException. Change-Id: Ic86278e9e1e91eea0c045d2b93342f018e1d8e66 --- diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index a5ee7ddb8c..974e9a2dfc 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -244,4 +244,17 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"' ); } + + /** + * Expect an ApiUsageException to be thrown with the given parameters, which are the same as + * ApiUsageException::newWithMessage()'s parameters. This allows checking for an exception + * whose text is given by a message key instead of text, so as not to hard-code the message's + * text into test code. + */ + protected function setExpectedApiException( + $msg, $code = null, array $data = null, $httpCode = 0 + ) { + $expected = ApiUsageException::newWithMessage( null, $msg, $code, $data, $httpCode ); + $this->setExpectedException( ApiUsageException::class, $expected->getMessage() ); + } }