Transform MathWikibaseConfig into a service

* add some basic tests
* migrate MathWikibaseConnectorTest to a unit test

Bug: T313331
Depends-On: Ic3b8d4f685d5cf648a02696284b6ee499502a12a
Change-Id: I38d6425eb5e2c52ae4362c4b8656223a8d9d90a5
This commit is contained in:
Moritz Schubotz (physikerwelt) 2022-07-21 10:02:56 +02:00
parent a8052d564b
commit a274beae78
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
5 changed files with 131 additions and 47 deletions

View file

@ -8,6 +8,7 @@ use MediaWiki\Extension\Math\MathWikibaseConnector;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Wikibase\Client\WikibaseClient;
return [
'Math.CheckerFactory' => static function ( MediaWikiServices $services ): InputCheckFactory {
@ -39,9 +40,19 @@ return [
},
'Math.WikibaseConnector' => static function ( MediaWikiServices $services ): MathWikibaseConnector {
return new MathWikibaseConnector(
MathWikibaseConfig::getDefaultMathWikibaseConfig(),
$services->get( 'WikibaseClient.RepoLinker' ),
$services->get( 'Math.WikibaseConfig' ),
WikibaseClient::getRepoLinker( $services ),
$services->getLanguageFactory(),
LoggerFactory::getInstance( 'Math' )
);
},
'Math.WikibaseConfig' => static function ( MediaWikiServices $services ): MathWikibaseConfig {
return new MathWikibaseConfig(
WikibaseClient::getEntityIdParser( $services ),
WikibaseClient::getEntityRevisionLookup( $services ),
WikibaseClient::getFallbackLabelDescriptionLookupFactory( $services ),
WikibaseClient::getSite( $services ),
$services->getMainConfig()
);
},
];

View file

@ -2,11 +2,8 @@
namespace MediaWiki\Extension\Math;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MWException;
use Config;
use Site;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Lib\Store\EntityRevisionLookup;
@ -52,29 +49,25 @@ class MathWikibaseConfig {
*/
private $propertyIdDefiningFormula;
/**
* @var MathWikibaseConfig
*/
private static $defaultConfig;
/**
* @param EntityIdParser $entityIdParser
* @param EntityRevisionLookup $entityRevisionLookup
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
* @param Site $site
* @param Config $config
*/
public function __construct(
EntityIdParser $entityIdParser,
EntityRevisionLookup $entityRevisionLookup,
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
Site $site
Site $site,
Config $config
) {
$this->idParser = $entityIdParser;
$this->entityRevisionLookup = $entityRevisionLookup;
$this->labelLookupFactory = $labelDescriptionLookupFactory;
$this->site = $site;
$config = MediaWikiServices::getInstance()->getMainConfig();
$this->propertyIdHasPart = $this->idParser->parse(
$config->get( "MathWikibasePropertyIdHasPart" )
);
@ -143,27 +136,4 @@ class MathWikibaseConfig {
public function getPropertyIdDefiningFormula(): PropertyId {
return $this->propertyIdDefiningFormula;
}
/**
* @return MathWikibaseConfig default config
*/
public static function getDefaultMathWikibaseConfig(): MathWikibaseConfig {
if ( !self::$defaultConfig ) {
$site = null;
try {
$site = WikibaseClient::getSite();
} catch ( MWException $e ) {
$logger = LoggerFactory::getInstance( 'Math' );
$logger->warning( "Cannot get Site handler: " . $e->getMessage() );
}
self::$defaultConfig = new MathWikibaseConfig(
WikibaseClient::getEntityIdParser(),
WikibaseClient::getStore()->getEntityRevisionLookup(),
WikibaseClient::getFallbackLabelDescriptionLookupFactory(),
$site
);
}
return self::$defaultConfig;
}
}

View file

@ -4,7 +4,7 @@ namespace MediaWiki\Extension\Math;
use DataValues\StringValue;
use InvalidArgumentException;
use Language;
use MediaWiki\Languages\LanguageFactory;
use MWException;
use Psr\Log\LoggerInterface;
use Wikibase\Client\RepoLinker;
@ -38,18 +38,24 @@ class MathWikibaseConnector {
/** @var RepoLinker */
private $repoLinker;
/** @var LanguageFactory */
private $languageFactory;
/**
* @param MathWikibaseConfig $config
* @param RepoLinker $repoLinker
* @param LanguageFactory $languageFactory
* @param LoggerInterface $logger
*/
public function __construct(
MathWikibaseConfig $config,
RepoLinker $repoLinker,
LanguageFactory $languageFactory,
LoggerInterface $logger
) {
$this->config = $config;
$this->repoLinker = $repoLinker;
$this->languageFactory = $languageFactory;
$this->logger = $logger;
}
@ -64,7 +70,7 @@ class MathWikibaseConnector {
*/
public function fetchWikibaseFromId( $qid, $langCode ) {
try {
$lang = Language::factory( $langCode );
$lang = $this->languageFactory->getLanguage( $langCode );
} catch ( MWException $e ) {
throw new InvalidArgumentException( "Invalid language code specified." );
}

View file

@ -0,0 +1,73 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use HashConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWikiUnitTestCase;
use Site;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
/**
* @covers \MediaWiki\Extension\Math\MathWikibaseConfig
*/
class MathWikibaseConfigTest extends MediaWikiUnitTestCase {
private function getConfig() {
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
return new MathWikibaseConfig(
new BasicEntityIdParser(),
$entityRevisionLookup,
$labelDescriptionLookupFactory,
new Site(),
new HashConfig( [
'MathWikibasePropertyIdHasPart' => 'P1',
'MathWikibasePropertyIdDefiningFormula' => 'P2',
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
] ) );
}
public function testGetEntityRevisionLookup() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\Lib\Store\EntityRevisionLookup', $config->getEntityRevisionLookup() );
}
public function testGetSite() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Site', $config->getSite() );
}
public function testGetPropertyIdQuantitySymbol() {
$config = $this->getConfig();
$this->assertEquals( 'P3', $config->getPropertyIdQuantitySymbol()->getLocalPart() );
}
public function testGetLabelLookupFactory() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory',
$config->getLabelLookupFactory() );
}
public function testGetIdParser() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\BasicEntityIdParser', $config->getIdParser() );
}
public function testHasSite() {
$config = $this->getConfig();
$this->assertTrue( $config->hasSite() );
}
public function testGetPropertyIdHasPart() {
$config = $this->getConfig();
$this->assertEquals( 'P1', $config->getPropertyIdHasPart()->getLocalPart() );
}
public function testGetPropertyIdDefiningFormula() {
$config = $this->getConfig();
$this->assertEquals( 'P2', $config->getPropertyIdDefiningFormula()->getLocalPart() );
}
}

View file

@ -2,20 +2,27 @@
namespace MediaWiki\Extension\Math\Tests;
use HashConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWiki\Extension\Math\MathWikibaseConnector;
use MediaWikiIntegrationTestCase;
use MediaWiki\Languages\LanguageFactory;
use MediaWikiUnitTestCase;
use MWException;
use Site;
use TestLogger;
use Wikibase\Client\RepoLinker;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataAccess\DatabaseEntitySource;
use Wikibase\DataAccess\EntitySourceDefinitions;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
use Wikibase\Lib\SubEntityTypesMapper;
use const MWException;
/**
* @covers \MediaWiki\Extension\Math\MathWikibaseConnector
*/
class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
class MathWikibaseConnectorTest extends MediaWikiUnitTestCase {
private const EXAMPLE_URL = 'https://example.com/';
@ -26,7 +33,11 @@ class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
}
public function testFetchInvalidLanguage() {
$mathWikibase = $this->getWikibaseConnector();
$languageFactory = $this->createMock( LanguageFactory::class );
$languageFactory->method( 'getLanguage' )
->willThrowException( new MWException( 'Invalid code' ) );
$mathWikibase = $this->getWikibaseConnector( $languageFactory );
$this->expectException( 'InvalidArgumentException' );
$this->expectErrorMessage( 'Invalid language code specified.' );
$mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
@ -61,13 +72,26 @@ class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
}
/**
* @param LanguageFactory|null $languageFactory
* @return MathWikibaseConnector
*/
public function getWikibaseConnector(): MathWikibaseConnector {
return new MathWikibaseConnector( new MathWikibaseConfig( WikibaseClient::getEntityIdParser(),
WikibaseClient::getStore()->getEntityRevisionLookup(),
WikibaseClient::getFallbackLabelDescriptionLookupFactory(),
WikibaseClient::getSite() ), $this->newConnector(), new TestLogger() );
public function getWikibaseConnector( LanguageFactory $languageFactory = null ): MathWikibaseConnector {
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
$languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class );
return new MathWikibaseConnector( new MathWikibaseConfig(
new BasicEntityIdParser(),
$entityRevisionLookup,
$labelDescriptionLookupFactory,
new Site(),
new HashConfig( [
'MathWikibasePropertyIdHasPart' => 'P1',
'MathWikibasePropertyIdDefiningFormula' => 'P2',
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
] ) ),
$this->newConnector(),
$languageFactory,
new TestLogger() );
}
}