ParserHookTest::testRender()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Tests\Integration\ParserHooks;
6
7
use Maps\MapsFactory;
8
use MediaWiki\MediaWikiServices;
9
use ParamProcessor\Processor;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
abstract class ParserHookTest extends TestCase {
17
18
	public static function setUpBeforeClass(): void {
19
		if ( !defined( 'MEDIAWIKI' ) ) {
20
			self::markTestSkipped( 'MediaWiki is not available' );
21
		}
22
	}
23
24
	/**
25
	 * @since 2.0
26
	 * @return array
27
	 */
28
	public abstract function parametersProvider();
29
30
	/**
31
	 * Triggers the render process with different sets of parameters to see if
32
	 * no errors or notices are thrown and the result indeed is a string.
33
	 *
34
	 * @dataProvider parametersProvider
35
	 * @since 2.0
36
	 *
37
	 * @param array $parameters
38
	 * @param string|null $expected
39
	 */
40
	public function testRender( array $parameters, $expected = null ) {
41
		$parserHook = $this->getInstance();
42
43
		$parser = MediaWikiServices::getInstance()->getParser();
44
45
		$renderResult = call_user_func_array(
46
			[ $parserHook, 'renderFunction' ],
47
			array_merge( [ &$parser ], $parameters )
48
		);
49
50
		if ( is_string( $renderResult ) ) {
51
			$this->assertTrue( true );
52
		} else {
53
			$this->assertIsArray( $renderResult );
54
			$this->assertIsString( $renderResult[0] );
55
		}
56
57
		if ( $expected !== null ) {
58
			$this->assertEquals( $expected, $renderResult[0] );
59
		}
60
	}
61
62
	/**
63
	 * @since 2.0
64
	 * @return \ParserHook
65
	 */
66
	protected abstract function getInstance();
67
68
	public function processingProvider() {
69
		return [];
70
	}
71
72
	/**
73
	 * @dataProvider processingProvider
74
	 * @since 3.0
75
	 */
76
	public function testParamProcessing( array $parameters, array $expectedValues ) {
77
		$definitions = $this->getInstance()->getParamDefinitions();
78
79
		$processor = Processor::newDefault();
80
		$processor->setParameters( $parameters );
81
		$processor->setParameterDefinitions(
82
			MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays( $definitions )
83
		);
84
85
		$result = $processor->processParameters();
86
87
		if ( $result->hasFatal() ) {
88
			$this->fail(
89
				'Fatal error occurred during the param processing: ' . $processor->hasFatalError()->getMessage()
90
			);
91
		}
92
93
		$actual = $result->getParameters();
94
95
		$expectedValues = array_merge( $this->getDefaultValues(), $expectedValues );
96
97
		foreach ( $expectedValues as $name => $expected ) {
98
			$this->assertArrayHasKey( $name, $actual );
99
100
			$this->assertEquals(
101
				$expected,
102
				$actual[$name]->getValue(),
103
				'Expected ' . var_export( $expected, true )
104
				. ' should match actual '
105
				. var_export( $actual[$name]->getValue(), true )
106
			);
107
		}
108
	}
109
110
	/**
111
	 * Returns an array with the default values of the parameters.
112
	 */
113
	private function getDefaultValues() {
114
		$definitions = MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays(
115
			$this->getInstance()->getParamDefinitions()
116
		);
117
118
		$defaults = [];
119
120
		foreach ( $definitions as $definition ) {
121
			if ( !$definition->isRequired() ) {
122
				$defaults[$definition->getName()] = $definition->getDefault();
123
			}
124
		}
125
126
		return $defaults;
127
	}
128
129
	protected function arrayWrap( array $elements ) {
130
		return array_map(
131
			function ( $element ) {
132
				return [ $element ];
133
			},
134
			$elements
135
		);
136
	}
137
138
}
139