Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

ParserHookTest::getInstance()

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
	/**
18
	 * @since 2.0
19
	 * @return \ParserHook
20
	 */
21
	protected abstract function getInstance();
22
23
	/**
24
	 * @since 2.0
25
	 * @return array
26
	 */
27
	public abstract function parametersProvider();
28
29
	/**
30
	 * Triggers the render process with different sets of parameters to see if
31
	 * no errors or notices are thrown and the result indeed is a string.
32
	 *
33
	 * @dataProvider parametersProvider
34
	 * @since 2.0
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
		}
54
		else {
55
			$this->assertInternalType( 'array', $renderResult );
56
			$this->assertInternalType( 'string', $renderResult[0] );
57
		}
58
59
		if ( $expected !== null ) {
60
			$this->assertEquals( $expected, $renderResult[0] );
61
		}
62
	}
63
64
	public function processingProvider() {
65
		return [];
66
	}
67
68
	/**
69
	 * @dataProvider processingProvider
70
	 * @since 3.0
71
	 */
72
	public function testParamProcessing( array $parameters, array $expectedValues ) {
73
		$definitions = $this->getInstance()->getParamDefinitions();
74
75
		$processor = Processor::newDefault();
76
		$processor->setParameters( $parameters, $definitions );
77
78
		$result = $processor->processParameters();
79
80
		if ( $result->hasFatal() ) {
81
			$this->fail( 'Fatal error occurred during the param processing: ' . $processor->hasFatalError()->getMessage() );
82
		}
83
84
		$actual = $result->getParameters();
85
86
		$expectedValues = array_merge( $this->getDefaultValues(), $expectedValues );
87
88
		foreach ( $expectedValues as $name => $expected ) {
89
			$this->assertArrayHasKey( $name, $actual );
90
91
			$this->assertEquals(
92
				$expected,
93
				$actual[$name]->getValue(),
94
				'Expected ' . var_export( $expected, true )
95
					. ' should match actual '
96
					. var_export( $actual[$name]->getValue(), true )
97
			);
98
		}
99
	}
100
101
	/**
102
	 * Returns an array with the default values of the parameters.
103
	 *
104
	 * @since 3.0
105
	 *
106
	 * @return array
107
	 */
108
	protected function getDefaultValues() {
109
		$definitions = ParamDefinition::getCleanDefinitions( $this->getInstance()->getParamDefinitions() );
110
111
		$defaults = [];
112
113
		foreach ( $definitions as $definition ) {
114
			if ( !$definition->isRequired() ) {
115
				$defaults[$definition->getName()] = $definition->getDefault();
116
			}
117
		}
118
119
		return $defaults;
120
	}
121
122
	protected function arrayWrap( array $elements ) {
123
		return array_map(
124
			function ( $element ) {
125
				return [ $element ];
126
			},
127
			$elements
128
		);
129
	}
130
131
}