4 * @covers ParserFactory
6 class ParserFactoryTest
extends MediaWikiTestCase
{
8 * For backwards compatibility, all parameters to the parser constructor are optional and
9 * default to the appropriate global service, so it's easy to forget to update ParserFactory to
10 * actually pass the parameters it's supposed to.
12 public function testConstructorArgNum() {
13 $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
14 $instanceConstructor = new ReflectionMethod( 'Parser', '__construct' );
15 // Subtract one for the ParserFactory itself
16 $this->assertSame( $instanceConstructor->getNumberOfParameters() - 1,
17 $factoryConstructor->getNumberOfParameters(),
18 'Parser and ParserFactory constructors have an inconsistent number of parameters. ' .
19 'Did you add a parameter to one and not the other?' );
22 public function testAllArgumentsWerePassed() {
23 $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
25 foreach ( $factoryConstructor->getParameters() as $index => $param ) {
26 $type = (string)$param->getType();
28 $val = $this->createMock( 'MediaWiki\Config\ServiceOptions' );
29 } elseif ( $type === 'array' ) {
30 $val = [ 'porcupines will tell me your secrets' . count( $mocks ) ];
31 } elseif ( class_exists( $type ) ||
interface_exists( $type ) ) {
32 $val = $this->createMock( $type );
33 } elseif ( $type === '' ) {
34 // Optimistically assume a string is okay
35 $val = 'I will de-quill them first' . count( $mocks );
37 $this->fail( "Unrecognized parameter type $type in ParserFactory constructor" );
42 $factory = new ParserFactory( ...$mocks );
43 $parser = $factory->create();
45 foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
46 $prop->setAccessible( true );
47 foreach ( $mocks as $idx => $mock ) {
48 if ( $prop->getValue( $parser ) === $mock ) {
49 unset( $mocks[$idx] );
54 $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
55 'found in Parser member variables' );
58 public function provideConstructorArguments() {
59 // Create a mock Config object that will satisfy ServiceOptions::__construct
60 $mockConfig = $this->createMock( 'Config' );
61 $mockConfig->method( 'has' )->willReturn( true );
62 $mockConfig->method( 'get' )->willReturn( 'I like otters.' );
65 [ 'the plural of platypus...' ],
66 $this->createMock( 'MagicWordFactory' ),
67 $this->createMock( 'Language' ),
69 $this->createMock( 'MediaWiki\Special\SpecialPageFactory' ),
71 $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
74 yield
'args_without_namespace_info' => [
77 yield
'args_with_namespace_info' => [
78 array_merge( $mocks, [ $this->createMock( 'NamespaceInfo' ) ] ),
83 * @dataProvider provideConstructorArguments
84 * @covers ParserFactory::__construct
86 public function testBackwardsCompatibleConstructorArguments( $args ) {
87 $this->hideDeprecated( 'ParserFactory::__construct with Config parameter' );
88 $factory = new ParserFactory( ...$args );
89 $parser = $factory->create();
91 // It is expected that these are not present on the parser.
95 foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
96 $prop->setAccessible( true );
97 foreach ( $args as $idx => $mockTest ) {
98 if ( $prop->getValue( $parser ) === $mockTest ) {
104 $this->assertCount( 0, $args, 'Not all arguments to the ParserFactory constructor were ' .
105 'found in Parser member variables' );