Completed
Pull Request — master (#560)
by Jeroen De
02:47
created

ParserHookTest::getCleanedDefinitions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
129
130
		$defaults = [];
131
132
		foreach ( $definitions as $definition ) {
133
			if ( !$definition->isRequired() ) {
134
				$defaults[$definition->getName()] = $definition->getDefault();
135
			}
136
		}
137
138
		return $defaults;
139
	}
140
141
	protected function arrayWrap( array $elements ) {
142
		return array_map(
143
			function ( $element ) {
144
				return [ $element ];
145
			},
146
			$elements
147
		);
148
	}
149
150
}
151