1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParserHooks\Tests; |
4
|
|
|
|
5
|
|
|
use ParserHooks\HookDefinition; |
6
|
|
|
use ParserHooks\HookRegistrant; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers ParserHooks\HookRegistrant |
11
|
|
|
* |
12
|
|
|
* @group ParserHooks |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class HookRegistrantTest extends TestCase { |
18
|
|
|
|
19
|
|
|
public function namesProvider() { |
20
|
|
|
return [ |
21
|
|
|
[ [ 'foo' ] ], |
22
|
|
|
[ [ 'foo', 'bar' ] ], |
23
|
|
|
[ [ 'foo', 'bar', 'baz', 'bah' ] ], |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @dataProvider namesProvider |
29
|
|
|
*/ |
30
|
|
|
public function testRegisterFunction( array $names ) { |
31
|
|
|
$parser = $this->newMockParserForFunction( $names ); |
32
|
|
|
$registrant = new HookRegistrant( $parser ); |
33
|
|
|
|
34
|
|
|
$registrant->registerFunction( $this->newMockRunner( $names, 'ParserHooks\FunctionRunner' ) ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider namesProvider |
39
|
|
|
*/ |
40
|
|
|
public function testRegisterHook( array $names ) { |
41
|
|
|
$parser = $this->newMockParserForHook( $names ); |
42
|
|
|
$registrant = new HookRegistrant( $parser ); |
43
|
|
|
|
44
|
|
|
$registrant->registerHook( $this->newMockRunner( $names, 'ParserHooks\HookRunner' ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider namesProvider |
49
|
|
|
*/ |
50
|
|
|
public function testRegisterFunctionHandler( array $names ) { |
51
|
|
|
$parser = $this->newMockParserForFunction( $names ); |
52
|
|
|
$registrant = new HookRegistrant( $parser ); |
53
|
|
|
|
54
|
|
|
$registrant->registerFunctionHandler( |
55
|
|
|
new HookDefinition( $names ), |
56
|
|
|
$this->createMock( 'ParserHooks\HookHandler' ) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider namesProvider |
62
|
|
|
*/ |
63
|
|
|
public function testRegisterHookHandler( array $names ) { |
64
|
|
|
$parser = $this->newMockParserForHook( $names ); |
65
|
|
|
$registrant = new HookRegistrant( $parser ); |
66
|
|
|
|
67
|
|
|
$registrant->registerHookHandler( |
68
|
|
|
new HookDefinition( $names ), |
69
|
|
|
$this->createMock( 'ParserHooks\HookHandler' ) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function newMockParserForFunction( array $names ) { |
74
|
|
|
return $this->newMockParser( $names, 'setFunctionHook' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function newMockParserForHook( array $names ) { |
78
|
|
|
return $this->newMockParser( $names, 'setHook' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function newMockParser( array $names, $expectedMethod ) { |
82
|
|
|
$parser = $this->createMock( 'Parser' ); |
83
|
|
|
|
84
|
|
|
foreach ( $names as $index => $name ) { |
85
|
|
|
$parser->expects( $this->at( $index ) ) |
86
|
|
|
->method( $expectedMethod ) |
87
|
|
|
->with( |
88
|
|
|
$this->equalTo( $name ), |
89
|
|
|
$this->isType( 'callable' ) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $parser; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function newMockRunner( array $names, $runnerClass ) { |
97
|
|
|
$definition = new HookDefinition( $names ); |
98
|
|
|
|
99
|
|
|
$runner = $this->createMock( $runnerClass ); |
100
|
|
|
|
101
|
|
|
$runner->expects( $this->once() ) |
102
|
|
|
->method( 'getDefinition' ) |
103
|
|
|
->will( $this->returnValue( $definition ) ); |
104
|
|
|
|
105
|
|
|
return $runner; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|