Provide static Factory Class for Math related services

Initially we provide one service. More services will come in
the future.

Change-Id: I98a0f5f181f729e123a063dabf3597b5effa55b8
This commit is contained in:
Moritz Schubotz (physikerwelt) 2022-07-21 17:47:52 +02:00
parent a274beae78
commit 73c41b4626
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
5 changed files with 105 additions and 1 deletions

View file

@ -2,6 +2,7 @@
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\InputCheck\InputCheckFactory;
use MediaWiki\Extension\Math\Math;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWiki\Extension\Math\MathWikibaseConnector;
@ -33,7 +34,7 @@ return [
RendererFactory::CONSTRUCTOR_OPTIONS,
$services->getMainConfig()
),
$services->get( 'Math.Config' ),
Math::getMathConfig( $services ),
$services->getUserOptionsLookup(),
LoggerFactory::getInstance( 'Math' )
);

27
src/Math.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace MediaWiki\Extension\Math;
use MediaWiki\MediaWikiServices;
use Psr\Container\ContainerInterface;
/**
* Top level factory for the Math extension.
*
* @license GPL-2.0-or-later
*/
final class Math {
/**
* @codeCoverageIgnore
*/
private function __construct() {
// should not be instantiated
}
public static function getMathConfig( ContainerInterface $services = null ): MathConfig {
return ( $services ?: MediaWikiServices::getInstance() )
->get( 'Math.Config' );
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use MediaWiki\Extension\Math\Math;
/**
* @covers \MediaWiki\Extension\Math\Math
*/
class MathIntegrationTest extends \MediaWikiIntegrationTestCase {
public function testGetMathConfigNull() {
$config = Math::getMathConfig();
$this->assertInstanceOf( '\MediaWiki\Extension\Math\MathConfig', $config );
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use HashConfig;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\Math;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\MediaWikiServices;
use MediaWikiUnitTestCase;
/**
* @covers \MediaWiki\Extension\Math\Math
*/
class MathTest extends MediaWikiUnitTestCase {
public function testGetMathConfigService() {
$config = new HashConfig( [
'MathDisableTexFilter' => MathConfig::NEW,
'MathValidModes' => [ MathConfig::MODE_SOURCE ]
] );
$services = new MediaWikiServices( $config );
$services->defineService( 'Math.Config',
static function ( MediaWikiServices $services ){
return new MathConfig(
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $services->get( 'BootstrapConfig' ) )
);
}
);
$mathConfig = Math::getMathConfig( $services );
$this->assertStringContainsString( 'new', $mathConfig->texCheckDisabled() );
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use Generator;
use MediaWikiUnitTestCase;
/**
* @coversNothing
*/
class ServiceWiringTest extends MediaWikiUnitTestCase {
private const EXTENSION_PREFIX = 'Math.';
/**
* @dataProvider provideWiring
*/
public function testAllWiringsAreProperlyShaped( $name, $definition ): void {
$this->assertStringStartsWith( self::EXTENSION_PREFIX, $name );
$this->assertIsCallable( $definition );
}
public function provideWiring(): Generator {
$wiring = require __DIR__ . '/../../../ServiceWiring.php';
foreach ( $wiring as $name => $definition ) {
yield $name => [ $name, $definition ];
}
}
}