1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\Tests\Integration\parserhooks; |
4
|
|
|
|
5
|
|
|
use ParamProcessor\ParamDefinition; |
6
|
|
|
use ParamProcessor\Processor; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @licence GNU GPL v2+ |
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
abstract class ParserHookTest extends TestCase { |
14
|
|
|
|
15
|
|
|
public static function setUpBeforeClass() { |
16
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) { |
17
|
|
|
self::markTestSkipped( 'MediaWiki is not available' ); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @since 2.0 |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public abstract function parametersProvider(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Triggers the render process with different sets of parameters to see if |
29
|
|
|
* no errors or notices are thrown and the result indeed is a string. |
30
|
|
|
* |
31
|
|
|
* @dataProvider parametersProvider |
32
|
|
|
* @since 2.0 |
33
|
|
|
* |
34
|
|
|
* @param array $parameters |
35
|
|
|
* @param string|null $expected |
36
|
|
|
*/ |
37
|
|
|
public function testRender( array $parameters, $expected = null ) { |
38
|
|
|
$parserHook = $this->getInstance(); |
39
|
|
|
|
40
|
|
|
$parser = new \Parser(); |
41
|
|
|
$parser->mOptions = new \ParserOptions(); |
42
|
|
|
$parser->clearState(); |
43
|
|
|
$parser->setTitle( \Title::newMainPage() ); |
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->assertInternalType( 'array', $renderResult ); |
54
|
|
|
$this->assertInternalType( 'string', $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, $definitions ); |
81
|
|
|
|
82
|
|
|
$result = $processor->processParameters(); |
83
|
|
|
|
84
|
|
|
if ( $result->hasFatal() ) { |
85
|
|
|
$this->fail( |
86
|
|
|
'Fatal error occurred during the param processing: ' . $processor->hasFatalError()->getMessage() |
87
|
|
|
); |
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
|
|
|
private function getDefaultValues() { |
111
|
|
|
$definitions = ParamDefinition::getCleanDefinitions( $this->getInstance()->getParamDefinitions() ); |
112
|
|
|
|
113
|
|
|
$defaults = []; |
114
|
|
|
|
115
|
|
|
foreach ( $definitions as $definition ) { |
116
|
|
|
if ( !$definition->isRequired() ) { |
117
|
|
|
$defaults[$definition->getName()] = $definition->getDefault(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $defaults; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function arrayWrap( array $elements ) { |
125
|
|
|
return array_map( |
126
|
|
|
function ( $element ) { |
127
|
|
|
return [ $element ]; |
128
|
|
|
}, |
129
|
|
|
$elements |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |