Merge "Paranoia, escape image alignment parameters before outputting."
[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 $param ) {
26 $type = (string)$param->getType();
27 if ( $type === 'array' ) {
28 $val = [ 'porcupines will tell me your secrets' . count( $mocks ) ];
29 } elseif ( class_exists( $type ) || interface_exists( $type ) ) {
30 $val = $this->createMock( $type );
31 } elseif ( $type === '' ) {
32 // Optimistically assume a string is okay
33 $val = 'I will de-quill them first' . count( $mocks );
34 } else {
35 $this->fail( "Unrecognized parameter type $type in ParserFactory constructor" );
36 }
37 $mocks[] = $val;
38 }
39
40 $factory = new ParserFactory( ...$mocks );
41 $parser = $factory->create();
42
43 foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
44 $prop->setAccessible( true );
45 foreach ( $mocks as $idx => $mock ) {
46 if ( $prop->getValue( $parser ) === $mock ) {
47 unset( $mocks[$idx] );
48 }
49 }
50 }
51
52 $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
53 'found in Parser member variables' );
54 }
55 }