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