Merge "Add checkDependencies.php"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserFactoryTest.php
1 <?php
2
3 /**
4 * @covers ParserFactory
5 */
6 class ParserFactoryTest extends MediaWikiTestCase {
7 /**
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.
11 */
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?' );
20 }
21
22 public function testAllArgumentsWerePassed() {
23 $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
24 $mocks = [];
25 foreach ( $factoryConstructor->getParameters() as $index => $param ) {
26 $type = (string)$param->getType();
27 if ( $index === 0 ) {
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 );
36 } else {
37 $this->fail( "Unrecognized parameter type $type in ParserFactory constructor" );
38 }
39 $mocks[] = $val;
40 }
41
42 $factory = new ParserFactory( ...$mocks );
43 $parser = $factory->create();
44
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] );
50 }
51 }
52 }
53
54 $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
55 'found in Parser member variables' );
56 }
57
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.' );
63
64 $mocks = [
65 [ 'the plural of platypus...' ],
66 $this->createMock( 'MagicWordFactory' ),
67 $this->createMock( 'Language' ),
68 '...is platypodes',
69 $this->createMock( 'MediaWiki\Special\SpecialPageFactory' ),
70 $mockConfig,
71 $this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
72 ];
73
74 yield 'args_without_namespace_info' => [
75 $mocks,
76 ];
77 yield 'args_with_namespace_info' => [
78 array_merge( $mocks, [ $this->createMock( 'NamespaceInfo' ) ] ),
79 ];
80 }
81
82 /**
83 * @dataProvider provideConstructorArguments
84 * @covers ParserFactory::__construct
85 */
86 public function testBackwardsCompatibleConstructorArguments( $args ) {
87 $this->hideDeprecated( 'ParserFactory::__construct with Config parameter' );
88 $factory = new ParserFactory( ...$args );
89 $parser = $factory->create();
90
91 // It is expected that these are not present on the parser.
92 unset( $args[5] );
93 unset( $args[0] );
94
95 foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
96 $prop->setAccessible( true );
97 foreach ( $args as $idx => $mockTest ) {
98 if ( $prop->getValue( $parser ) === $mockTest ) {
99 unset( $args[$idx] );
100 }
101 }
102 }
103
104 $this->assertCount( 0, $args, 'Not all arguments to the ParserFactory constructor were ' .
105 'found in Parser member variables' );
106 }
107 }