Test Failed
Push — master ( 7969f2...0db1fe )
by Jeroen De
09:42
created

HookRunnerTest::testRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 4
dl 0
loc 19
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
use ParserHooks\HookRunner;
10
11
/**
12
 * @covers ParserHooks\HookRunner
13
 * @covers ParserHooks\Internal\Runner
14
 *
15
 * @group ParserHooks
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class HookRunnerTest extends \PHPUnit_Framework_TestCase {
21
22
	public function optionsProvider() {
23
		return array(
24
			array(
25
				array(
26
					HookRunner::OPT_DO_PARSE => true,
27
				),
28
				array(),
29
				array(),
30
				array(),
31
			),
32
			array(
33
				array(
34
					HookRunner::OPT_DO_PARSE => false,
35
				),
36
				array(),
37
				array(),
38
				array(),
39
			),
40
			array(
41
				array(
42
					HookRunner::OPT_DO_PARSE => true,
43
				),
44
				array(
45
					'foo' => 'bar',
46
					'baz' => 'bah',
47
				),
48
				array(),
49
				array(
50
					'foo' => 'bar',
51
					'baz' => 'bah',
52
				),
53
			),
54
			array(
55
				array(
56
					HookRunner::OPT_DO_PARSE => true,
57
				),
58
				array(
59
					'foo' => 'bar',
60
					'baz' => 'bah',
61
				),
62
				array(
63
					'ohi',
64
					'there',
65
				),
66
				array(
67
					'ohi' => self::INPUT_TEXT,
68
					'foo' => 'bar',
69
					'baz' => 'bah',
70
				),
71
			),
72
		);
73
	}
74
75
	const HOOK_HANDLER_RESULT = 'hook handler result';
76
	const PARSE_RESULT = 'the parsed result';
77
	const INPUT_TEXT = 'input text';
78
79
	protected $options;
80
81
	protected $frame;
82
	protected $parser;
83
84
	/**
85
	 * @dataProvider optionsProvider
86
	 */
87
	public function testRun( array $options, array $params, array $defaultParams, array $expectedParams ) {
88
		$this->options = $options;
89
90
		$this->frame = $this->getMock( 'PPFrame' );
91
		$this->parser = $this->newMockParser();
92
93
		$runner = $this->newHookRunner( $defaultParams, $expectedParams );
94
95
		$result = $runner->run(
96
			self::INPUT_TEXT,
97
			$params,
98
			$this->parser,
99
			$this->frame
100
		);
101
102
		$expectedResult = $this->options[HookRunner::OPT_DO_PARSE] ? self::PARSE_RESULT : self::HOOK_HANDLER_RESULT;
103
104
		$this->assertEquals( $expectedResult, $result );
105
	}
106
107
	protected function newHookRunner( array $defaultParams, array $expectedParams ) {
108
		$processedParams = new ProcessingResult( array(
109
			'foo' => new ProcessedParam( 'foo', 'bar', false )
110
		) );
111
112
		$definition = new HookDefinition(
113
			'someHook',
114
			array(),
115
			$defaultParams
116
		);
117
118
		$paramProcessor = $this->newMockParamProcessor( $expectedParams, $processedParams );
119
120
		$hookHandler = $this->newMockHookHandler( $processedParams );
121
122
		return new HookRunner(
123
			$definition,
124
			$hookHandler,
125
			$this->options,
126
			$paramProcessor
127
		);
128
	}
129
130
	protected function newMockParser() {
131
		$parser = $this->getMock( 'Parser' );
132
133
		if ( $this->options[HookRunner::OPT_DO_PARSE] ) {
134
			$parser->expects( $this->once() )
135
				->method( 'recursiveTagParse' )
136
				->with(
137
					$this->equalTo( self::HOOK_HANDLER_RESULT ),
138
					$this->equalTo( $this->frame )
139
				)
140
				->will( $this->returnValue( self::PARSE_RESULT ) );
141
		}
142
		else {
143
			$parser->expects( $this->never() )
144
				->method( 'recursiveTagParse' );
145
		}
146
147
		return $parser;
148
	}
149
150
	protected function newMockHookHandler( $expectedProcessor ) {
151
		$hookHandler = $this->getMock( 'ParserHooks\HookHandler' );
152
153
		$hookHandler->expects( $this->once() )
154
			->method( 'handle' )
155
			->with(
156
				$this->equalTo( $this->parser ),
157
				$this->equalTo( $expectedProcessor )
158
			)
159
			->will( $this->returnValue( self::HOOK_HANDLER_RESULT ) );
160
161
		return $hookHandler;
162
	}
163
164
	protected function newMockParamProcessor( $expandedParams, $processedParams ) {
165
		$paramProcessor = $this->getMockBuilder( 'ParamProcessor\Processor' )
166
			->disableOriginalConstructor()->getMock();
167
168
		$paramProcessor->expects( $this->once() )
169
			->method( 'setParameters' )
170
			->with( $this->equalTo( $expandedParams ) );
171
172
		$paramProcessor->expects( $this->once() )
173
			->method( 'processParameters' )
174
			->will( $this->returnValue( $processedParams ) );
175
176
		return $paramProcessor;
177
	}
178
179
}
180