Completed
Push — merge-sm ( 44b830...c70fa4 )
by Jeroen De
10:03 queued 07:12
created

ParserHookTest::parametersProvider()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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