1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<?php
use Psr\Container\ContainerInterface;
use Wikimedia\ObjectFactory\ObjectFactory;
/**
* @covers SkinFactory
*/
class SkinFactoryTest extends \MediaWikiUnitTestCase {
private function createSkinFactory( $service = null, $options = [] ): SkinFactory {
$objectFactory = $service
? new ObjectFactory( $service )
: new ObjectFactory( $this->createMock( ContainerInterface::class ) );
return new SkinFactory( $objectFactory, $options );
}
public function testRegisterWithInvalidCallable() {
$factory = $this->createSkinFactory();
$this->expectException( InvalidArgumentException::class );
$factory->register( 'invalid', 'Invalid', 'Invalid callback' );
}
public function testRegisterWithCallable() {
$factory = $this->createSkinFactory();
$instance = new SkinFallback();
$factory->register( 'fallback', 'Fallback', static function () use ( $instance ) {
return $instance;
}, true );
$this->assertSame( $instance, $factory->makeSkin( 'fallback' ) );
}
public function testRegisterWithSpec() {
$factory = $this->createSkinFactory();
$factory->register( 'fallback', 'Fallback', [
'class' => SkinFallback::class
], true );
$this->assertInstanceOf( SkinFallback::class, $factory->makeSkin( 'fallback' ) );
}
public function testMakeSkinWithNoBuilders() {
$factory = $this->createSkinFactory();
$this->expectException( SkinException::class );
$factory->makeSkin( 'nobuilderregistered' );
}
public function testMakeSkinWithInvalidCallback() {
$factory = $this->createSkinFactory();
$factory->register( 'unittest', 'Unittest', static function () {
// Not a Skin object
return true;
} );
$this->expectException( UnexpectedValueException::class );
$factory->makeSkin( 'unittest' );
}
public function testMakeSkinWithValidCallback() {
$factory = $this->createSkinFactory();
$factory->register( 'testfallback', 'TestFallback', static function () {
return new SkinFallback();
} );
$skin = $factory->makeSkin( 'testfallback' );
$this->assertInstanceOf( SkinFallback::class, $skin );
$this->assertEquals( 'fallback', $skin->getSkinName() );
}
public function testMakeSkinWithValidSpec() {
$serviceInstance = (object)[];
$serviceContainer = $this->createMock( ContainerInterface::class );
$serviceContainer->method( 'has' )->willReturn( true );
$serviceContainer->method( 'get' )->willReturn( $serviceInstance );
$args = [];
$factory = $this->createSkinFactory( $serviceContainer );
$factory->register( 'testfallback', 'TestFallback', [
'factory' => static function ( $service, $options ) use ( &$args ) {
$args = [ $service, $options ];
return new SkinFallback();
},
'services' => [
'testservice'
]
] );
$skin = $factory->makeSkin( 'testfallback' );
$this->assertInstanceOf( SkinFallback::class, $skin );
$this->assertEquals( 'fallback', $skin->getSkinName() );
$this->assertSame( 'testfallback', $args[1]['name'] );
$this->assertSame( $serviceInstance, $args[0] );
}
public function testRegisterReplaces() {
$factory = $this->createSkinFactory();
$s1 = $this->createMock( Skin::class );
$factory->register( 'foo', 'Skin 1',
static function () use ( $s1 ) {
return $s1;
},
true
);
$this->assertEquals( [ 'foo' => 'Skin 1' ], $factory->getSkinNames() );
$this->assertSame( $s1, $factory->makeSkin( 'foo' ) );
$this->assertSame( [], $factory->getAllowedSkins(), 'skipped' );
// Skippable state from previous register() call must not leak to replacement
$s2 = $this->createMock( Skin::class );
$factory->register( 'foo', 'Skin 2',
static function () use ( $s2 ) {
return $s2;
}
);
$this->assertEquals( [ 'foo' => 'Skin 2' ], $factory->getSkinNames() );
$this->assertSame( $s2, $factory->makeSkin( 'foo' ) );
$this->assertSame( [ 'foo' => 'Skin 2' ], $factory->getAllowedSkins(), 'not skipped' );
}
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'] );
}
public function testGetAllowedSkins() {
$sf = $this->createSkinFactory( null, [ 'quux' ] );
$sf->register( 'foo', 'Foo', [] );
$sf->register( 'apioutput', 'ApiOutput', [], true );
// Skippable state is unspecified here and must inherit from site config,
// which we seeded with 'quux', and thus skipped from allowed skins.
$sf->register( 'quux', 'Quux', [] );
$sf->register( 'fallback', 'Fallback', [], true );
$sf->register( 'bar', 'Barbar', [] );
$this->assertEquals(
[ 'foo' => 'Foo', 'bar' => 'Barbar' ],
$sf->getAllowedSkins()
);
}
public function testGetAllowedSkinsEmpty() {
$sf = $this->createSkinFactory();
$sf->register( 'apioutput', 'ApiOutput', [], true );
$sf->register( 'fallback', 'Fallback', [], true );
$this->assertEquals( [], $sf->getAllowedSkins() );
}
}
|