Completed
Push — newparam ( c5fad3...ada48d )
by Jeroen De
01:58
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(
83
			MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays( $definitions )
84
		);
85
86
		$result = $processor->processParameters();
87
88
		if ( $result->hasFatal() ) {
89
			$this->fail(
90
				'Fatal error occurred during the param processing: ' . $processor->hasFatalError()->getMessage()
91
			);
92
		}
93
94
		$actual = $result->getParameters();
95
96
		$expectedValues = array_merge( $this->getDefaultValues(), $expectedValues );
97
98
		foreach ( $expectedValues as $name => $expected ) {
99
			$this->assertArrayHasKey( $name, $actual );
100
101
			$this->assertEquals(
102
				$expected,
103
				$actual[$name]->getValue(),
104
				'Expected ' . var_export( $expected, true )
105
				. ' should match actual '
106
				. var_export( $actual[$name]->getValue(), true )
107
			);
108
		}
109
	}
110
111
	/**
112
	 * Returns an array with the default values of the parameters.
113
	 */
114
	private function getDefaultValues() {
115
		$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...
116
117
		$defaults = [];
118
119
		foreach ( $definitions as $definition ) {
120
			if ( !$definition->isRequired() ) {
121
				$defaults[$definition->getName()] = $definition->getDefault();
122
			}
123
		}
124
125
		return $defaults;
126
	}
127
128
	protected function arrayWrap( array $elements ) {
129
		return array_map(
130
			function ( $element ) {
131
				return [ $element ];
132
			},
133
			$elements
134
		);
135
	}
136
137
}
138