summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/tests/Integration/MediaWiki/ParserHooks/ParserHookTest.php
blob: 791f5d44519f523d8d3f4a251a2b9c2c5654729d (plain)
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
<?php

namespace Maps\Tests\Integration\MediaWiki\ParserHooks;

use Maps\MapsFactory;
use ParamProcessor\Processor;
use PHPUnit\Framework\TestCase;

/**
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
abstract class ParserHookTest extends TestCase {

	public static function setUpBeforeClass() {
		if ( !defined( 'MEDIAWIKI' ) ) {
			self::markTestSkipped( 'MediaWiki is not available' );
		}
	}

	/**
	 * @since 2.0
	 * @return array
	 */
	public abstract function parametersProvider();

	/**
	 * Triggers the render process with different sets of parameters to see if
	 * no errors or notices are thrown and the result indeed is a string.
	 *
	 * @dataProvider parametersProvider
	 * @since 2.0
	 *
	 * @param array $parameters
	 * @param string|null $expected
	 */
	public function testRender( array $parameters, $expected = null ) {
		$parserHook = $this->getInstance();

		$parser = new \Parser();
		$parser->mOptions = new \ParserOptions();
		$parser->clearState();
		$parser->setTitle( \Title::newMainPage() );

		$renderResult = call_user_func_array(
			[ $parserHook, 'renderFunction' ],
			array_merge( [ &$parser ], $parameters )
		);

		if ( is_string( $renderResult ) ) {
			$this->assertTrue( true );
		} else {
			$this->assertInternalType( 'array', $renderResult );
			$this->assertInternalType( 'string', $renderResult[0] );
		}

		if ( $expected !== null ) {
			$this->assertEquals( $expected, $renderResult[0] );
		}
	}

	/**
	 * @since 2.0
	 * @return \ParserHook
	 */
	protected abstract function getInstance();

	public function processingProvider() {
		return [];
	}

	/**
	 * @dataProvider processingProvider
	 * @since 3.0
	 */
	public function testParamProcessing( array $parameters, array $expectedValues ) {
		$definitions = $this->getInstance()->getParamDefinitions();

		$processor = Processor::newDefault();
		$processor->setParameters( $parameters );
		$processor->setParameterDefinitions(
			MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays( $definitions )
		);

		$result = $processor->processParameters();

		if ( $result->hasFatal() ) {
			$this->fail(
				'Fatal error occurred during the param processing: ' . $processor->hasFatalError()->getMessage()
			);
		}

		$actual = $result->getParameters();

		$expectedValues = array_merge( $this->getDefaultValues(), $expectedValues );

		foreach ( $expectedValues as $name => $expected ) {
			$this->assertArrayHasKey( $name, $actual );

			$this->assertEquals(
				$expected,
				$actual[$name]->getValue(),
				'Expected ' . var_export( $expected, true )
				. ' should match actual '
				. var_export( $actual[$name]->getValue(), true )
			);
		}
	}

	/**
	 * Returns an array with the default values of the parameters.
	 */
	private function getDefaultValues() {
		$definitions = MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays(
			$this->getInstance()->getParamDefinitions()
		);

		$defaults = [];

		foreach ( $definitions as $definition ) {
			if ( !$definition->isRequired() ) {
				$defaults[$definition->getName()] = $definition->getDefault();
			}
		}

		return $defaults;
	}

	protected function arrayWrap( array $elements ) {
		return array_map(
			function ( $element ) {
				return [ $element ];
			},
			$elements
		);
	}

}