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