The old way of providing a callable to SkinFactory::register is still supported. Those callables expected the skin name as their first argument. Coincidentally, so does the constructor of Skin. Some skins might not define any constructor parameters at all, which is acceptable to PHP, as it will just discard the argument. The registration using $wgValidSkinNames has not been changed, and skins that want to define services to be injected will still need to manually register their skin to the skin factory. CodeSearch did not indicate any extensions or skins manually constructing a SkinFactory in tests, but for posterity, the old way of creating a SkinFactory for testing can be replaced with new SkinFactory( new ObjectFactory( $this->createMock( ContainerInterface::class ) ) ); Note that the constructor for SkinFactory for internal use only, in accordance with the Stable interface policy. You should use MediaWikiServices::getInstance()->getSkinFactory instead. Bug: T244466 Change-Id: I8ba9d869bddd9b6124e47697b789d752c0620b02
137 lines
3.8 KiB
PHP
137 lines
3.8 KiB
PHP
<?php
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
use Wikimedia\ObjectFactory;
|
|
|
|
class SkinFactoryTest extends \MediaWikiUnitTestCase {
|
|
|
|
private function createSkinFactory() : SkinFactory {
|
|
return new SkinFactory( new ObjectFactory( $this->createMock( ContainerInterface::class ) ) );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::register
|
|
*/
|
|
public function testRegisterWithInvalidCallable() {
|
|
$factory = $this->createSkinFactory();
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
$factory->register( 'invalid', 'Invalid', 'Invalid callback' );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::register
|
|
*/
|
|
public function testRegisterWithCallable() {
|
|
$factory = $this->createSkinFactory();
|
|
$instance = new SkinFallback();
|
|
|
|
$factory->register( 'fallback', 'Fallback', function () use ( $instance ) {
|
|
return $instance;
|
|
} );
|
|
|
|
$this->assertSame( $instance, $factory->makeSkin( 'fallback' ) );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::register
|
|
*/
|
|
public function testRegisterWithSpec() {
|
|
$factory = $this->createSkinFactory();
|
|
$factory->register( 'fallback', 'Fallback', [
|
|
'class' => SkinFallback::class
|
|
] );
|
|
|
|
$this->assertInstanceOf( SkinFallback::class, $factory->makeSkin( 'fallback' ) );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::makeSkin
|
|
*/
|
|
public function testMakeSkinWithNoBuilders() {
|
|
$factory = $this->createSkinFactory();
|
|
$this->expectException( SkinException::class );
|
|
$factory->makeSkin( 'nobuilderregistered' );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::makeSkin
|
|
*/
|
|
public function testMakeSkinWithInvalidCallback() {
|
|
$factory = $this->createSkinFactory();
|
|
$factory->register( 'unittest', 'Unittest', function () {
|
|
// Not a Skin object
|
|
return true;
|
|
} );
|
|
$this->expectException( UnexpectedValueException::class );
|
|
$factory->makeSkin( 'unittest' );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::makeSkin
|
|
*/
|
|
public function testMakeSkinWithValidCallback() {
|
|
$factory = $this->createSkinFactory();
|
|
$factory->register( 'testfallback', 'TestFallback', function () {
|
|
return new SkinFallback();
|
|
} );
|
|
|
|
$skin = $factory->makeSkin( 'testfallback' );
|
|
$this->assertInstanceOf( SkinFallback::class, $skin );
|
|
$this->assertEquals( 'fallback', $skin->getSkinName() );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::__construct
|
|
* @covers SkinFactory::makeSkin
|
|
*/
|
|
public function testMakeSkinWithValidSpec() {
|
|
$serviceInstance = new stdClass();
|
|
|
|
$serviceContainer = $this->createMock( ContainerInterface::class );
|
|
$serviceContainer->method( 'has' )->willReturn( true );
|
|
$serviceContainer->method( 'get' )->willReturn( $serviceInstance );
|
|
|
|
$args = [];
|
|
$factory = new SkinFactory( new ObjectFactory( $serviceContainer ) );
|
|
$factory->register( 'testfallback', 'TestFallback', [
|
|
'factory' => function ( $name, $services ) use ( &$args ) {
|
|
$args = [ $name, $services ];
|
|
return new SkinFallback();
|
|
},
|
|
'services' => [
|
|
'testservice'
|
|
]
|
|
] );
|
|
|
|
$skin = $factory->makeSkin( 'testfallback' );
|
|
$this->assertInstanceOf( SkinFallback::class, $skin );
|
|
$this->assertEquals( 'fallback', $skin->getSkinName() );
|
|
$this->assertSame( 'testfallback', $args[0] );
|
|
$this->assertSame( $serviceInstance, $args[1] );
|
|
}
|
|
|
|
/**
|
|
* @covers Skin::__construct
|
|
* @covers Skin::getSkinName
|
|
*/
|
|
public function testGetSkinName() {
|
|
$skin = new SkinFallback();
|
|
$this->assertEquals( 'fallback', $skin->getSkinName(), 'Default' );
|
|
$skin = new SkinFallback( 'testname' );
|
|
$this->assertEquals( 'testname', $skin->getSkinName(), 'Constructor argument' );
|
|
}
|
|
|
|
/**
|
|
* @covers SkinFactory::getSkinNames
|
|
*/
|
|
public function testGetSkinNames() {
|
|
$factory = $this->createSkinFactory();
|
|
$factory->register( 'skin1', 'Skin1', [] );
|
|
$factory->register( 'skin2', 'Skin2', [] );
|
|
|
|
$names = $factory->getSkinNames();
|
|
$this->assertEquals( 'Skin1', $names['skin1'] );
|
|
$this->assertEquals( 'Skin2', $names['skin2'] );
|
|
}
|
|
}
|