Merge "Clean up AuthManagerTest a bit"
[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 $this->assertSame( $instanceConstructor->getNumberOfParameters(),
16 $factoryConstructor->getNumberOfParameters(),
17 'Parser and ParserFactory constructors have an inconsistent number of parameters. ' .
18 'Did you add a parameter to one and not the other?' );
19 }
20
21 public function testAllArgumentsWerePassed() {
22 $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
23 $mocks = [];
24 foreach ( $factoryConstructor->getParameters() as $param ) {
25 $type = (string)$param->getType();
26 if ( $type === 'array' ) {
27 $val = [ 'porcupines will tell me your secrets' . count( $mocks ) ];
28 } elseif ( class_exists( $type ) || interface_exists( $type ) ) {
29 $val = $this->createMock( $type );
30 } elseif ( $type === '' ) {
31 // Optimistically assume a string is okay
32 $val = 'I will de-quill them first' . count( $mocks );
33 } else {
34 $this->fail( "Unrecognized parameter type $type in ParserFactory constructor" );
35 }
36 $mocks[] = $val;
37 }
38
39 $factory = new ParserFactory( ...$mocks );
40 $parser = $factory->create();
41
42 foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
43 $prop->setAccessible( true );
44 foreach ( $mocks as $idx => $mock ) {
45 if ( $prop->getValue( $parser ) === $mock ) {
46 unset( $mocks[$idx] );
47 }
48 }
49 }
50
51 $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
52 'found in Parser member variables' );
53 }
54 }