1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParserHooks\Tests; |
4
|
|
|
|
5
|
|
|
use ParamProcessor\ProcessedParam; |
6
|
|
|
use ParamProcessor\ProcessingResult; |
7
|
|
|
use ParamProcessor\Processor; |
8
|
|
|
use ParserHooks\FunctionRunner; |
9
|
|
|
use ParserHooks\HookDefinition; |
10
|
|
|
use ParserHooks\HookHandler; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers ParserHooks\FunctionRunner |
15
|
|
|
* @covers ParserHooks\Internal\Runner |
16
|
|
|
* |
17
|
|
|
* @group ParserHooks |
18
|
|
|
* |
19
|
|
|
* @licence GNU GPL v2+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class FunctionRunnerTest extends TestCase { |
23
|
|
|
|
24
|
|
|
public function optionsProvider() { |
25
|
|
|
return [ |
26
|
|
|
[ |
27
|
|
|
[ |
28
|
|
|
FunctionRunner::OPT_DO_PARSE => true, |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
[ |
33
|
|
|
FunctionRunner::OPT_DO_PARSE => false, |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
const HOOK_HANDLER_RESULT = 'hook handler result'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @dataProvider optionsProvider |
43
|
|
|
*/ |
44
|
|
|
public function testRun( array $options ) { |
45
|
|
|
$definition = new HookDefinition( 'someHook' ); |
46
|
|
|
|
47
|
|
|
$parser = $this->createMock( \Parser::class ); |
48
|
|
|
|
49
|
|
|
$inputParams = [ |
50
|
|
|
'foo=bar', |
51
|
|
|
'baz=42', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$processedParams = new ProcessingResult( [ |
55
|
|
|
'foo' => new ProcessedParam( 'foo', 'bar', false ) |
56
|
|
|
] ); |
57
|
|
|
|
58
|
|
|
$paramProcessor = $this->newMockParamProcessor( $inputParams, $processedParams ); |
59
|
|
|
|
60
|
|
|
$hookHandler = $this->newMockHookHandler( $processedParams, $parser ); |
61
|
|
|
|
62
|
|
|
$runner = new FunctionRunner( |
63
|
|
|
$definition, |
64
|
|
|
$hookHandler, |
65
|
|
|
$options, |
66
|
|
|
$paramProcessor |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$frame = $this->createMock( \PPFrame::class ); |
70
|
|
|
|
71
|
|
|
$frame->expects( $this->exactly( count( $inputParams ) ) ) |
72
|
|
|
->method( 'expand' ) |
73
|
|
|
->will( $this->returnArgument( 0 ) ); |
74
|
|
|
|
75
|
|
|
$result = $runner->run( |
76
|
|
|
$parser, |
77
|
|
|
$inputParams, |
78
|
|
|
$frame |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->assertResultIsValid( $result, $options ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function assertResultIsValid( $result, array $options ) { |
85
|
|
|
$expected = [ self::HOOK_HANDLER_RESULT ]; |
86
|
|
|
|
87
|
|
|
if ( !$options[FunctionRunner::OPT_DO_PARSE] ) { |
88
|
|
|
$expected['noparse'] = true; |
89
|
|
|
$expected['isHTML'] = true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->assertEquals( $expected, $result ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function newMockHookHandler( $expectedParameters, $parser ) { |
96
|
|
|
$hookHandler = $this->createMock( HookHandler::class ); |
97
|
|
|
|
98
|
|
|
$hookHandler->expects( $this->once() ) |
99
|
|
|
->method( 'handle' ) |
100
|
|
|
->with( |
101
|
|
|
$this->equalTo( $parser ), |
102
|
|
|
$this->equalTo( $expectedParameters ) |
103
|
|
|
) |
104
|
|
|
->will( $this->returnValue( self::HOOK_HANDLER_RESULT ) ); |
105
|
|
|
|
106
|
|
|
return $hookHandler; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function newMockParamProcessor( $expandedParams, $processedParams ) { |
110
|
|
|
$paramProcessor = $this->createMock( Processor::class ); |
111
|
|
|
|
112
|
|
|
$paramProcessor->expects( $this->once() ) |
113
|
|
|
->method( 'setFunctionParams' ) |
114
|
|
|
->with( $this->equalTo( $expandedParams ) ); |
115
|
|
|
|
116
|
|
|
$paramProcessor->expects( $this->once() ) |
117
|
|
|
->method( 'processParameters' ) |
118
|
|
|
->will( $this->returnValue( $processedParams ) ); |
119
|
|
|
|
120
|
|
|
return $paramProcessor; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|