mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-23 23:25:02 +00:00
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:
parent
a8052d564b
commit
a274beae78
|
@ -8,6 +8,7 @@ use MediaWiki\Extension\Math\MathWikibaseConnector;
|
||||||
use MediaWiki\Extension\Math\Render\RendererFactory;
|
use MediaWiki\Extension\Math\Render\RendererFactory;
|
||||||
use MediaWiki\Logger\LoggerFactory;
|
use MediaWiki\Logger\LoggerFactory;
|
||||||
use MediaWiki\MediaWikiServices;
|
use MediaWiki\MediaWikiServices;
|
||||||
|
use Wikibase\Client\WikibaseClient;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'Math.CheckerFactory' => static function ( MediaWikiServices $services ): InputCheckFactory {
|
'Math.CheckerFactory' => static function ( MediaWikiServices $services ): InputCheckFactory {
|
||||||
|
@ -39,9 +40,19 @@ return [
|
||||||
},
|
},
|
||||||
'Math.WikibaseConnector' => static function ( MediaWikiServices $services ): MathWikibaseConnector {
|
'Math.WikibaseConnector' => static function ( MediaWikiServices $services ): MathWikibaseConnector {
|
||||||
return new MathWikibaseConnector(
|
return new MathWikibaseConnector(
|
||||||
MathWikibaseConfig::getDefaultMathWikibaseConfig(),
|
$services->get( 'Math.WikibaseConfig' ),
|
||||||
$services->get( 'WikibaseClient.RepoLinker' ),
|
WikibaseClient::getRepoLinker( $services ),
|
||||||
|
$services->getLanguageFactory(),
|
||||||
LoggerFactory::getInstance( 'Math' )
|
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()
|
||||||
|
);
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -2,11 +2,8 @@
|
||||||
|
|
||||||
namespace MediaWiki\Extension\Math;
|
namespace MediaWiki\Extension\Math;
|
||||||
|
|
||||||
use MediaWiki\Logger\LoggerFactory;
|
use Config;
|
||||||
use MediaWiki\MediaWikiServices;
|
|
||||||
use MWException;
|
|
||||||
use Site;
|
use Site;
|
||||||
use Wikibase\Client\WikibaseClient;
|
|
||||||
use Wikibase\DataModel\Entity\EntityIdParser;
|
use Wikibase\DataModel\Entity\EntityIdParser;
|
||||||
use Wikibase\DataModel\Entity\PropertyId;
|
use Wikibase\DataModel\Entity\PropertyId;
|
||||||
use Wikibase\Lib\Store\EntityRevisionLookup;
|
use Wikibase\Lib\Store\EntityRevisionLookup;
|
||||||
|
@ -52,29 +49,25 @@ class MathWikibaseConfig {
|
||||||
*/
|
*/
|
||||||
private $propertyIdDefiningFormula;
|
private $propertyIdDefiningFormula;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var MathWikibaseConfig
|
|
||||||
*/
|
|
||||||
private static $defaultConfig;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param EntityIdParser $entityIdParser
|
* @param EntityIdParser $entityIdParser
|
||||||
* @param EntityRevisionLookup $entityRevisionLookup
|
* @param EntityRevisionLookup $entityRevisionLookup
|
||||||
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
|
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
|
||||||
* @param Site $site
|
* @param Site $site
|
||||||
|
* @param Config $config
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EntityIdParser $entityIdParser,
|
EntityIdParser $entityIdParser,
|
||||||
EntityRevisionLookup $entityRevisionLookup,
|
EntityRevisionLookup $entityRevisionLookup,
|
||||||
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
|
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
|
||||||
Site $site
|
Site $site,
|
||||||
|
Config $config
|
||||||
) {
|
) {
|
||||||
$this->idParser = $entityIdParser;
|
$this->idParser = $entityIdParser;
|
||||||
$this->entityRevisionLookup = $entityRevisionLookup;
|
$this->entityRevisionLookup = $entityRevisionLookup;
|
||||||
$this->labelLookupFactory = $labelDescriptionLookupFactory;
|
$this->labelLookupFactory = $labelDescriptionLookupFactory;
|
||||||
$this->site = $site;
|
$this->site = $site;
|
||||||
|
|
||||||
$config = MediaWikiServices::getInstance()->getMainConfig();
|
|
||||||
$this->propertyIdHasPart = $this->idParser->parse(
|
$this->propertyIdHasPart = $this->idParser->parse(
|
||||||
$config->get( "MathWikibasePropertyIdHasPart" )
|
$config->get( "MathWikibasePropertyIdHasPart" )
|
||||||
);
|
);
|
||||||
|
@ -143,27 +136,4 @@ class MathWikibaseConfig {
|
||||||
public function getPropertyIdDefiningFormula(): PropertyId {
|
public function getPropertyIdDefiningFormula(): PropertyId {
|
||||||
return $this->propertyIdDefiningFormula;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ namespace MediaWiki\Extension\Math;
|
||||||
|
|
||||||
use DataValues\StringValue;
|
use DataValues\StringValue;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Language;
|
use MediaWiki\Languages\LanguageFactory;
|
||||||
use MWException;
|
use MWException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Wikibase\Client\RepoLinker;
|
use Wikibase\Client\RepoLinker;
|
||||||
|
@ -38,18 +38,24 @@ class MathWikibaseConnector {
|
||||||
/** @var RepoLinker */
|
/** @var RepoLinker */
|
||||||
private $repoLinker;
|
private $repoLinker;
|
||||||
|
|
||||||
|
/** @var LanguageFactory */
|
||||||
|
private $languageFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param MathWikibaseConfig $config
|
* @param MathWikibaseConfig $config
|
||||||
* @param RepoLinker $repoLinker
|
* @param RepoLinker $repoLinker
|
||||||
|
* @param LanguageFactory $languageFactory
|
||||||
* @param LoggerInterface $logger
|
* @param LoggerInterface $logger
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
MathWikibaseConfig $config,
|
MathWikibaseConfig $config,
|
||||||
RepoLinker $repoLinker,
|
RepoLinker $repoLinker,
|
||||||
|
LanguageFactory $languageFactory,
|
||||||
LoggerInterface $logger
|
LoggerInterface $logger
|
||||||
) {
|
) {
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->repoLinker = $repoLinker;
|
$this->repoLinker = $repoLinker;
|
||||||
|
$this->languageFactory = $languageFactory;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +70,7 @@ class MathWikibaseConnector {
|
||||||
*/
|
*/
|
||||||
public function fetchWikibaseFromId( $qid, $langCode ) {
|
public function fetchWikibaseFromId( $qid, $langCode ) {
|
||||||
try {
|
try {
|
||||||
$lang = Language::factory( $langCode );
|
$lang = $this->languageFactory->getLanguage( $langCode );
|
||||||
} catch ( MWException $e ) {
|
} catch ( MWException $e ) {
|
||||||
throw new InvalidArgumentException( "Invalid language code specified." );
|
throw new InvalidArgumentException( "Invalid language code specified." );
|
||||||
}
|
}
|
||||||
|
|
73
tests/phpunit/unit/MathWikibaseConfigTest.php
Normal file
73
tests/phpunit/unit/MathWikibaseConfigTest.php
Normal 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() );
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,20 +2,27 @@
|
||||||
|
|
||||||
namespace MediaWiki\Extension\Math\Tests;
|
namespace MediaWiki\Extension\Math\Tests;
|
||||||
|
|
||||||
|
use HashConfig;
|
||||||
use MediaWiki\Extension\Math\MathWikibaseConfig;
|
use MediaWiki\Extension\Math\MathWikibaseConfig;
|
||||||
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
||||||
use MediaWikiIntegrationTestCase;
|
use MediaWiki\Languages\LanguageFactory;
|
||||||
|
use MediaWikiUnitTestCase;
|
||||||
|
use MWException;
|
||||||
|
use Site;
|
||||||
use TestLogger;
|
use TestLogger;
|
||||||
use Wikibase\Client\RepoLinker;
|
use Wikibase\Client\RepoLinker;
|
||||||
use Wikibase\Client\WikibaseClient;
|
|
||||||
use Wikibase\DataAccess\DatabaseEntitySource;
|
use Wikibase\DataAccess\DatabaseEntitySource;
|
||||||
use Wikibase\DataAccess\EntitySourceDefinitions;
|
use Wikibase\DataAccess\EntitySourceDefinitions;
|
||||||
|
use Wikibase\DataModel\Entity\BasicEntityIdParser;
|
||||||
|
use Wikibase\Lib\Store\EntityRevisionLookup;
|
||||||
|
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
|
||||||
use Wikibase\Lib\SubEntityTypesMapper;
|
use Wikibase\Lib\SubEntityTypesMapper;
|
||||||
|
use const MWException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \MediaWiki\Extension\Math\MathWikibaseConnector
|
* @covers \MediaWiki\Extension\Math\MathWikibaseConnector
|
||||||
*/
|
*/
|
||||||
class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
|
class MathWikibaseConnectorTest extends MediaWikiUnitTestCase {
|
||||||
|
|
||||||
private const EXAMPLE_URL = 'https://example.com/';
|
private const EXAMPLE_URL = 'https://example.com/';
|
||||||
|
|
||||||
|
@ -26,7 +33,11 @@ class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFetchInvalidLanguage() {
|
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->expectException( 'InvalidArgumentException' );
|
||||||
$this->expectErrorMessage( 'Invalid language code specified.' );
|
$this->expectErrorMessage( 'Invalid language code specified.' );
|
||||||
$mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
|
$mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
|
||||||
|
@ -61,13 +72,26 @@ class MathWikibaseConnectorTest extends MediaWikiIntegrationTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param LanguageFactory|null $languageFactory
|
||||||
* @return MathWikibaseConnector
|
* @return MathWikibaseConnector
|
||||||
*/
|
*/
|
||||||
public function getWikibaseConnector(): MathWikibaseConnector {
|
public function getWikibaseConnector( LanguageFactory $languageFactory = null ): MathWikibaseConnector {
|
||||||
return new MathWikibaseConnector( new MathWikibaseConfig( WikibaseClient::getEntityIdParser(),
|
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
|
||||||
WikibaseClient::getStore()->getEntityRevisionLookup(),
|
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
|
||||||
WikibaseClient::getFallbackLabelDescriptionLookupFactory(),
|
$languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class );
|
||||||
WikibaseClient::getSite() ), $this->newConnector(), new TestLogger() );
|
return new MathWikibaseConnector( new MathWikibaseConfig(
|
||||||
|
new BasicEntityIdParser(),
|
||||||
|
$entityRevisionLookup,
|
||||||
|
$labelDescriptionLookupFactory,
|
||||||
|
new Site(),
|
||||||
|
new HashConfig( [
|
||||||
|
'MathWikibasePropertyIdHasPart' => 'P1',
|
||||||
|
'MathWikibasePropertyIdDefiningFormula' => 'P2',
|
||||||
|
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
|
||||||
|
] ) ),
|
||||||
|
$this->newConnector(),
|
||||||
|
$languageFactory,
|
||||||
|
new TestLogger() );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue