mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-27 17:01:07 +00:00
Avoid try…catch where if…else will do when using LanguageFactory
Needed for I869af06896b9757af18488b916211c5a41a8c563, where I am trying to change LanguageFactory in MediaWiki core not to use MWException. I truly feel mocked after working with this code. See if you can find the two lines in this diff where the meaningful changes are. Change-Id: Ifcb31dbb7113ce57526f06558cde2abedee317d7
This commit is contained in:
parent
4fde31757c
commit
1b7a46ff4f
|
@ -46,6 +46,7 @@ return [
|
|||
new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
|
||||
WikibaseClient::getRepoLinker( $services ),
|
||||
$services->getLanguageFactory(),
|
||||
$services->getLanguageNameUtils(),
|
||||
WikibaseClient::getEntityRevisionLookup( $services ),
|
||||
WikibaseClient::getFallbackLabelDescriptionLookupFactory( $services ),
|
||||
WikibaseClient::getSite( $services ),
|
||||
|
|
|
@ -360,6 +360,7 @@
|
|||
"services": [
|
||||
"Math.WikibaseConnector",
|
||||
"LanguageFactory",
|
||||
"LanguageNameUtils",
|
||||
"TitleFactory"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use DataValues\StringValue;
|
|||
use InvalidArgumentException;
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MWException;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Site;
|
||||
use Wikibase\Client\RepoLinker;
|
||||
|
@ -51,6 +51,9 @@ class MathWikibaseConnector {
|
|||
/** @var LanguageFactory */
|
||||
private $languageFactory;
|
||||
|
||||
/** @var LanguageNameUtils */
|
||||
private $languageNameUtils;
|
||||
|
||||
/** @var EntityRevisionLookup */
|
||||
private $entityRevisionLookup;
|
||||
|
||||
|
@ -85,6 +88,7 @@ class MathWikibaseConnector {
|
|||
* @param ServiceOptions $options
|
||||
* @param RepoLinker $repoLinker
|
||||
* @param LanguageFactory $languageFactory
|
||||
* @param LanguageNameUtils $languageNameUtils
|
||||
* @param EntityRevisionLookup $entityRevisionLookup
|
||||
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
|
||||
* @param Site $site
|
||||
|
@ -96,6 +100,7 @@ class MathWikibaseConnector {
|
|||
ServiceOptions $options,
|
||||
RepoLinker $repoLinker,
|
||||
LanguageFactory $languageFactory,
|
||||
LanguageNameUtils $languageNameUtils,
|
||||
EntityRevisionLookup $entityRevisionLookup,
|
||||
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
|
||||
Site $site,
|
||||
|
@ -106,6 +111,7 @@ class MathWikibaseConnector {
|
|||
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
||||
$this->repoLinker = $repoLinker;
|
||||
$this->languageFactory = $languageFactory;
|
||||
$this->languageNameUtils = $languageNameUtils;
|
||||
$this->entityRevisionLookup = $entityRevisionLookup;
|
||||
$this->labelDescriptionLookupFactory = $labelDescriptionLookupFactory;
|
||||
$this->site = $site;
|
||||
|
@ -167,9 +173,9 @@ class MathWikibaseConnector {
|
|||
* id does not exist
|
||||
*/
|
||||
public function fetchWikibaseFromId( string $qid, string $langCode ): MathWikibaseInfo {
|
||||
try {
|
||||
if ( $this->languageNameUtils->isValidCode( $langCode ) ) {
|
||||
$lang = $this->languageFactory->getLanguage( $langCode );
|
||||
} catch ( MWException $e ) {
|
||||
} else {
|
||||
throw new InvalidArgumentException( "Invalid language code specified." );
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,9 @@ use Html;
|
|||
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
||||
use MediaWiki\Extension\Math\MathWikibaseInfo;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Rest\Response;
|
||||
use MediaWiki\Rest\SimpleHandler;
|
||||
use MWException;
|
||||
use Title;
|
||||
use TitleFactory;
|
||||
use Wikimedia\ParamValidator\ParamValidator;
|
||||
|
@ -21,21 +21,27 @@ class Popup extends SimpleHandler {
|
|||
/** @var LanguageFactory */
|
||||
private $languageFactory;
|
||||
|
||||
/** @var LanguageNameUtils */
|
||||
private $languageNameUtils;
|
||||
|
||||
/** @var Title|null */
|
||||
private $specialPageTitle;
|
||||
|
||||
/**
|
||||
* @param MathWikibaseConnector $wikibase
|
||||
* @param LanguageFactory $languageFactory
|
||||
* @param LanguageNameUtils $languageNameUtils
|
||||
* @param TitleFactory $titleFactory
|
||||
*/
|
||||
public function __construct(
|
||||
MathWikibaseConnector $wikibase,
|
||||
LanguageFactory $languageFactory,
|
||||
LanguageNameUtils $languageNameUtils,
|
||||
TitleFactory $titleFactory
|
||||
) {
|
||||
$this->wikibase = $wikibase;
|
||||
$this->languageFactory = $languageFactory;
|
||||
$this->languageNameUtils = $languageNameUtils;
|
||||
$this->specialPageTitle = $titleFactory->newFromText( 'Special:MathWikibase' );
|
||||
}
|
||||
|
||||
|
@ -45,9 +51,9 @@ class Popup extends SimpleHandler {
|
|||
$uselang = 'en';
|
||||
}
|
||||
$rf = $this->getResponseFactory();
|
||||
try {
|
||||
if ( $this->languageNameUtils->isValidCode( $uselang ) ) {
|
||||
$langObj = $this->languageFactory->getLanguage( $uselang );
|
||||
} catch ( MWException $e ) {
|
||||
} else {
|
||||
return $rf->createHttpError( 400, [ 'message' => 'Invalid language code.' ] );
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,8 @@
|
|||
namespace MediaWiki\Extension\Math\Tests;
|
||||
|
||||
use DataValues\StringValue;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Logger\LoggerFactory;
|
||||
use MWException;
|
||||
use Wikibase\DataModel\Entity\BasicEntityIdParser;
|
||||
use Wikibase\DataModel\Entity\EntityIdParsingException;
|
||||
use Wikibase\DataModel\Entity\Item;
|
||||
|
@ -30,10 +29,10 @@ class MathWikibaseConnectorTest extends MathWikibaseConnectorTestFactory {
|
|||
}
|
||||
|
||||
public function testFetchInvalidLanguage() {
|
||||
$languageFactory = $this->createMock( LanguageFactory::class );
|
||||
$languageFactory->method( 'getLanguage' )
|
||||
->willThrowException( new MWException( 'Invalid code' ) );
|
||||
$mathWikibase = $this->getWikibaseConnector( $languageFactory );
|
||||
$languageNameUtils = $this->createMock( LanguageNameUtils::class );
|
||||
$languageNameUtils->method( 'isValidCode' )
|
||||
->willReturn( false );
|
||||
$mathWikibase = $this->getWikibaseConnector( null, $languageNameUtils );
|
||||
|
||||
$this->expectException( 'InvalidArgumentException' );
|
||||
$this->expectErrorMessage( 'Invalid language code specified.' );
|
||||
|
@ -44,7 +43,7 @@ class MathWikibaseConnectorTest extends MathWikibaseConnectorTestFactory {
|
|||
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
|
||||
$entityRevisionLookup->method( 'getEntityRevision' )
|
||||
->willThrowException( new StorageException( 'Invalid code' ) );
|
||||
$mathWikibase = $this->getWikibaseConnector( null, null, $entityRevisionLookup );
|
||||
$mathWikibase = $this->getWikibaseConnector( null, null, null, $entityRevisionLookup );
|
||||
|
||||
$this->expectException( 'InvalidArgumentException' );
|
||||
$this->expectErrorMessage( 'Non-existing Wikibase ID.' );
|
||||
|
@ -79,6 +78,7 @@ class MathWikibaseConnectorTest extends MathWikibaseConnectorTestFactory {
|
|||
|
||||
// non-existing properties should not result in errors on initialization
|
||||
$mathWikibase = $this->getWikibaseConnector(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$revisionLookupMock,
|
||||
|
@ -104,7 +104,7 @@ class MathWikibaseConnectorTest extends MathWikibaseConnectorTestFactory {
|
|||
}
|
||||
} );
|
||||
|
||||
$mathWikibase = $this->getWikibaseConnector( null, null, null, null, $parserMock );
|
||||
$mathWikibase = $this->getWikibaseConnector( null, null, null, null, null, $parserMock );
|
||||
$this->expectException( 'InvalidArgumentException' );
|
||||
$this->expectErrorMessage( 'Invalid Wikibase ID.' );
|
||||
$mathWikibase->fetchWikibaseFromId( '1', 'en' );
|
||||
|
|
|
@ -8,6 +8,7 @@ use MediaWiki\Config\ServiceOptions;
|
|||
use MediaWiki\Extension\Math\MathFormatter;
|
||||
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWikiUnitTestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Site;
|
||||
|
@ -75,12 +76,17 @@ class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase {
|
|||
$languageFactoryMock->method( 'getLanguage' )
|
||||
->with( 'en' )
|
||||
->willReturn( $languageMock );
|
||||
$languageNameUtilsMock = self::createMock( LanguageNameUtils::class );
|
||||
$languageNameUtilsMock->method( 'isValidCode' )
|
||||
->with( 'en' )
|
||||
->willReturn( true );
|
||||
$fallbackLabelDescriptionLookupFactoryMock->method( 'newLabelDescriptionLookup' )
|
||||
->with( $languageMock )
|
||||
->willReturnCallback( [ $this, 'newLabelDescriptionLookup' ] );
|
||||
|
||||
return self::getWikibaseConnector(
|
||||
$languageFactoryMock,
|
||||
$languageNameUtilsMock,
|
||||
$fallbackLabelDescriptionLookupFactoryMock,
|
||||
$revisionLookupMock,
|
||||
$logger,
|
||||
|
@ -90,6 +96,7 @@ class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase {
|
|||
|
||||
public function getWikibaseConnector(
|
||||
LanguageFactory $languageFactory = null,
|
||||
LanguageNameUtils $languageNameUtils = null,
|
||||
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory = null,
|
||||
EntityRevisionLookup $entityRevisionLookupMock = null,
|
||||
LoggerInterface $logger = null,
|
||||
|
@ -102,6 +109,10 @@ class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase {
|
|||
self::createMock( EntityRevisionLookup::class );
|
||||
|
||||
$languageFactory = $languageFactory ?: self::createMock( LanguageFactory::class );
|
||||
if ( !$languageNameUtils ) {
|
||||
$languageNameUtils = self::createMock( LanguageNameUtils::class );
|
||||
$languageNameUtils->method( 'isValidCode' )->willReturn( true );
|
||||
}
|
||||
|
||||
$site = self::createMock( Site::class );
|
||||
$site->method( 'getGlobalId' )->willReturn( '' );
|
||||
|
@ -129,6 +140,7 @@ class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase {
|
|||
] ),
|
||||
$repoConnector,
|
||||
$languageFactory,
|
||||
$languageNameUtils,
|
||||
$entityRevisionLookup,
|
||||
$labelDescriptionLookupFactory,
|
||||
$site,
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace MediaWiki\Extension\Math\Tests;
|
|||
|
||||
use MediaWiki\Extension\Math\Rest\Popup;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Rest\Handler;
|
||||
use MediaWiki\Rest\HttpException;
|
||||
use MediaWiki\Rest\RequestData;
|
||||
|
@ -45,13 +46,13 @@ class PopupTest extends MathWikibaseConnectorTestFactory {
|
|||
}
|
||||
|
||||
public function testInvalidLanguage() {
|
||||
$languageFactoryMock = $this->createMock( LanguageFactory::class );
|
||||
$languageFactoryMock->expects( $this->once() )
|
||||
->method( 'getLanguage' )
|
||||
$languageNameUtilsMock = $this->createMock( LanguageNameUtils::class );
|
||||
$languageNameUtilsMock->expects( $this->once() )
|
||||
->method( 'isValidCode' )
|
||||
->with( 'tmp' )
|
||||
->willThrowException( new \MWException() );
|
||||
->willReturn( false );
|
||||
|
||||
$popupHandler = $this->getPopup( $languageFactoryMock );
|
||||
$popupHandler = $this->getPopup( null, $languageNameUtilsMock );
|
||||
$response = $this->executeHandler( $popupHandler, $this->getRequest( '1', 'tmp' ) );
|
||||
$this->assertEquals( 400, $response->getStatusCode() );
|
||||
$data = json_decode( $response->getBody(), true );
|
||||
|
@ -62,7 +63,7 @@ class PopupTest extends MathWikibaseConnectorTestFactory {
|
|||
* @dataProvider provideItemSetups
|
||||
*/
|
||||
public function testExistingId( Item $item ) {
|
||||
$popupHandler = $this->getPopup( null, $item );
|
||||
$popupHandler = $this->getPopup( null, null, $item );
|
||||
|
||||
$request = $this->getRequest( '1', 'en' );
|
||||
$data = $this->executeHandlerAndGetBodyData( $popupHandler, $request );
|
||||
|
@ -109,12 +110,17 @@ class PopupTest extends MathWikibaseConnectorTestFactory {
|
|||
|
||||
private function getPopup(
|
||||
LanguageFactory $languageFactoryMock = null,
|
||||
LanguageNameUtils $languageNameUtilsMock = null,
|
||||
Item $item = null
|
||||
): Popup {
|
||||
$languageFactoryMock = $languageFactoryMock ?: $this->createMock( LanguageFactory::class );
|
||||
if ( !$languageNameUtilsMock ) {
|
||||
$languageNameUtilsMock = self::createMock( LanguageNameUtils::class );
|
||||
$languageNameUtilsMock->method( 'isValidCode' )->willReturn( true );
|
||||
}
|
||||
$mathWikibaseConnectorMock = $item ?
|
||||
$this->getWikibaseConnectorWithExistingItems( new EntityRevision( $item ) ) :
|
||||
$this->getWikibaseConnector( $languageFactoryMock );
|
||||
$this->getWikibaseConnector( $languageFactoryMock, $languageNameUtilsMock );
|
||||
|
||||
$titleMock = $this->createMock( Title::class );
|
||||
$titleMock->method( 'getLocalURL' )->willReturn( 'special/Q1' );
|
||||
|
@ -124,7 +130,7 @@ class PopupTest extends MathWikibaseConnectorTestFactory {
|
|||
->method( 'newFromText' )
|
||||
->willReturn( $titleMock );
|
||||
|
||||
return new Popup( $mathWikibaseConnectorMock, $languageFactoryMock, $titleFactoryMock );
|
||||
return new Popup( $mathWikibaseConnectorMock, $languageFactoryMock, $languageNameUtilsMock, $titleFactoryMock );
|
||||
}
|
||||
|
||||
public function provideItemSetups(): array {
|
||||
|
|
Loading…
Reference in a new issue