HookRunnerTest::testRun()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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