Completed
Pull Request — master (#18)
by Jeroen De
17:37 queued 07:42
created

FunctionRunnerTest::newMockHookHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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