4
4
5
5
namespace SzepeViktor \PHPStan \WordPress \Tests ;
6
6
7
+ use PHPStan \Reflection \ReflectionProvider ;
7
8
use SzepeViktor \PHPStan \WordPress \HookCallbackRule ;
8
9
10
+ use const PHP_VERSION_ID ;
11
+
9
12
/**
10
13
* @extends \PHPStan\Testing\RuleTestCase<\SzepeViktor\PHPStan\WordPress\HookCallbackRule>
11
14
*/
12
15
class HookCallbackRuleTest extends \PHPStan \Testing \RuleTestCase
13
16
{
17
+ private const EXPECTED_ERRORS = [
18
+ ['Filter callback return statement is missing. ' , 17 ],
19
+ ['Filter callback return statement is missing. ' , 18 ],
20
+ ['Callback expects 1 parameter, $accepted_args is set to 0. ' , 21 ],
21
+ ['Callback expects 1 parameter, $accepted_args is set to 2. ' , 26 ],
22
+ ['Callback expects 0-1 parameters, $accepted_args is set to 2. ' , 31 ],
23
+ ['Callback expects 2 parameters, $accepted_args is set to 1. ' , 36 ],
24
+ ['Callback expects 2-4 parameters, $accepted_args is set to 1. ' , 41 ],
25
+ ['Callback expects 2-3 parameters, $accepted_args is set to 4. ' , 46 ],
26
+ ['Action callback returns true but should not return anything. ' , 51 ],
27
+ ['Action callback returns true but should not return anything. ' , 54 ],
28
+ ['Filter callback return statement is missing. ' , 61 ],
29
+ ['Action callback returns false but should not return anything. ' , 64 ],
30
+ ['Action callback returns int but should not return anything. ' , 67 ],
31
+ ['Callback expects at least 1 parameter, $accepted_args is set to 0. ' , 70 ],
32
+ ['Callback expects at least 1 parameter, $accepted_args is set to 0. ' , 73 ],
33
+ ['Callback expects 0-3 parameters, $accepted_args is set to 4. ' , 78 ],
34
+ ['Action callback returns int but should not return anything. ' , 83 ],
35
+ ['Callback expects 0 parameters, $accepted_args is set to 2. ' , 86 ],
36
+ ['Action callback returns false but should not return anything. ' , 89 ],
37
+ ['Action callback returns mixed but should not return anything. ' , 92 ],
38
+ ['Action callback returns null but should not return anything. ' , 95 ],
39
+ ];
40
+
14
41
protected function getRule (): \PHPStan \Rules \Rule
15
42
{
43
+ $ reflectionProvider = self ::getContainer ()->getByType (ReflectionProvider::class);
44
+
16
45
// getRule() method needs to return an instance of the tested rule
17
- return new HookCallbackRule ();
46
+ return new HookCallbackRule ($ reflectionProvider );
18
47
}
19
48
20
49
public function testRule (): void
@@ -26,96 +55,21 @@ public function testRule(): void
26
55
[
27
56
__DIR__ . '/data/hook-callback.php ' ,
28
57
],
58
+ self ::EXPECTED_ERRORS
59
+ );
60
+ }
61
+
62
+ public function testRuleWithNamedArguments (): void
63
+ {
64
+ if (PHP_VERSION_ID < 80000 ) {
65
+ $ this ::markTestSkipped ('Named arguments are only supported in PHP 8.0 and later. ' );
66
+ }
67
+
68
+ $ this ->analyse (
29
69
[
30
- [
31
- 'Filter callback return statement is missing. ' ,
32
- 17 ,
33
- ],
34
- [
35
- 'Filter callback return statement is missing. ' ,
36
- 20 ,
37
- ],
38
- [
39
- 'Filter callback return statement is missing. ' ,
40
- 23 ,
41
- ],
42
- [
43
- 'Callback expects 1 parameter, $accepted_args is set to 0. ' ,
44
- 26 ,
45
- ],
46
- [
47
- 'Callback expects 1 parameter, $accepted_args is set to 2. ' ,
48
- 31 ,
49
- ],
50
- [
51
- 'Callback expects 0-1 parameters, $accepted_args is set to 2. ' ,
52
- 36 ,
53
- ],
54
- [
55
- 'Callback expects 2 parameters, $accepted_args is set to 1. ' ,
56
- 41 ,
57
- ],
58
- [
59
- 'Callback expects 2-4 parameters, $accepted_args is set to 1. ' ,
60
- 46 ,
61
- ],
62
- [
63
- 'Callback expects 2-3 parameters, $accepted_args is set to 4. ' ,
64
- 51 ,
65
- ],
66
- [
67
- 'Action callback returns true but should not return anything. ' ,
68
- 56 ,
69
- ],
70
- [
71
- 'Action callback returns true but should not return anything. ' ,
72
- 61 ,
73
- ],
74
- [
75
- 'Filter callback return statement is missing. ' ,
76
- 68 ,
77
- ],
78
- [
79
- 'Action callback returns false but should not return anything. ' ,
80
- 71 ,
81
- ],
82
- [
83
- 'Action callback returns int but should not return anything. ' ,
84
- 74 ,
85
- ],
86
- [
87
- 'Callback expects at least 1 parameter, $accepted_args is set to 0. ' ,
88
- 77 ,
89
- ],
90
- [
91
- 'Callback expects at least 1 parameter, $accepted_args is set to 0. ' ,
92
- 80 ,
93
- ],
94
- [
95
- 'Callback expects 0-3 parameters, $accepted_args is set to 4. ' ,
96
- 85 ,
97
- ],
98
- [
99
- 'Action callback returns int but should not return anything. ' ,
100
- 90 ,
101
- ],
102
- [
103
- 'Callback expects 0 parameters, $accepted_args is set to 2. ' ,
104
- 93 ,
105
- ],
106
- [
107
- 'Action callback returns false but should not return anything. ' ,
108
- 96 ,
109
- ],
110
- [
111
- 'Action callback returns mixed but should not return anything. ' ,
112
- 99 ,
113
- ],
114
- [
115
- 'Action callback returns null but should not return anything. ' ,
116
- 102 ,
117
- ],
118
- ]
70
+ __DIR__ . '/data/hook-callback-named-args.php ' ,
71
+ ],
72
+ self ::EXPECTED_ERRORS
119
73
);
120
74
}
121
75
0 commit comments