Skip to content

Commit f1c2613

Browse files
author
Florian Krämer
committed
Enhance TestClass with additional methods and corresponding tests for method signature validation
- Added new methods to TestClass to cover various scenarios including max parameters, name mismatches, nullable types, class types, protected visibility, and valid method signatures. - Updated MethodSignatureMustMatchRuleTest to include tests for the new methods, ensuring proper validation of method signatures against defined patterns and visibility requirements. - Improved error reporting for parameter mismatches and added checks for new method scenarios.
1 parent 229a0c0 commit f1c2613

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

data/MethodSignatureMustMatch/TestClass.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
class DummyClass
4+
{
5+
}
6+
37
class TestClass
48
{
59
public function testMethod(int $a)
@@ -13,4 +17,39 @@ public function testMethodNoType($x, string $y)
1317
public function testMethodWithWrongType(int $x, int $y)
1418
{
1519
}
20+
21+
// Test max parameters violation
22+
public function testMaxParams(int $a, string $b, int $c, string $d)
23+
{
24+
}
25+
26+
// Test parameter name pattern mismatch
27+
public function testNameMismatch(int $wrongName, string $anotherWrong)
28+
{
29+
}
30+
31+
// Test nullable types
32+
public function testNullableTypes(?int $nullableInt, ?string $nullableString)
33+
{
34+
}
35+
36+
// Test class types
37+
public function testClassTypes(DummyClass $dummy, string $name)
38+
{
39+
}
40+
41+
// Test protected visibility
42+
protected function testProtectedMethod(int $value)
43+
{
44+
}
45+
46+
// Test method without visibility requirement (should pass)
47+
public function testNoVisibilityReq(int $x)
48+
{
49+
}
50+
51+
// Test valid method matching all criteria
52+
public function testValidMethod(int $alpha, string $beta)
53+
{
54+
}
1655
}

tests/TestCases/Architecture/MethodSignatureMustMatchRuleTest.php

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,71 @@ protected function getRule(): Rule
4646
],
4747
'visibilityScope' => 'public',
4848
],
49+
[
50+
'pattern' => '/^TestClass::testMaxParams$/',
51+
'minParameters' => 1,
52+
'maxParameters' => 2,
53+
'signature' => [],
54+
'visibilityScope' => 'public',
55+
],
56+
[
57+
'pattern' => '/^TestClass::testNameMismatch$/',
58+
'minParameters' => 2,
59+
'maxParameters' => 2,
60+
'signature' => [
61+
['type' => 'int', 'pattern' => '/^param/'], // Name pattern doesn't match
62+
['type' => 'string', 'pattern' => '/^param/'], // Name pattern doesn't match
63+
],
64+
'visibilityScope' => 'public',
65+
],
66+
[
67+
'pattern' => '/^TestClass::testNullableTypes$/',
68+
'minParameters' => 2,
69+
'maxParameters' => 2,
70+
'signature' => [
71+
['type' => '?int', 'pattern' => '/^nullable/'],
72+
['type' => '?string', 'pattern' => '/^nullable/'],
73+
],
74+
'visibilityScope' => 'public',
75+
],
76+
[
77+
'pattern' => '/^TestClass::testClassTypes$/',
78+
'minParameters' => 2,
79+
'maxParameters' => 2,
80+
'signature' => [
81+
['type' => 'DummyClass', 'pattern' => '/^dummy/'],
82+
['type' => 'string', 'pattern' => '/^name/'],
83+
],
84+
'visibilityScope' => 'public',
85+
],
86+
[
87+
'pattern' => '/^TestClass::testProtectedMethod$/',
88+
'minParameters' => 1,
89+
'maxParameters' => 1,
90+
'signature' => [
91+
['type' => 'int', 'pattern' => '/^value/'],
92+
],
93+
'visibilityScope' => 'protected',
94+
],
95+
[
96+
'pattern' => '/^TestClass::testNoVisibilityReq$/',
97+
'minParameters' => 1,
98+
'maxParameters' => 1,
99+
'signature' => [
100+
['type' => 'int', 'pattern' => '/^x/'],
101+
],
102+
// No visibilityScope specified
103+
],
104+
[
105+
'pattern' => '/^TestClass::testValidMethod$/',
106+
'minParameters' => 2,
107+
'maxParameters' => 2,
108+
'signature' => [
109+
['type' => 'int', 'pattern' => '/^alpha/'],
110+
['type' => 'string', 'pattern' => '/^beta/'],
111+
],
112+
'visibilityScope' => 'public',
113+
],
49114
]);
50115
}
51116

@@ -55,15 +120,15 @@ public function testRule(): void
55120
// Errors for testMethod (type checking enabled)
56121
[
57122
'Method TestClass::testMethod has 1 parameters, but at least 2 required.',
58-
5,
123+
9,
59124
],
60125
[
61126
'Method TestClass::testMethod is missing parameter #2 of type string.',
62-
5,
127+
9,
63128
],
64129
[
65130
'Method TestClass::testMethod must be private.',
66-
5,
131+
9,
67132
],
68133
// No errors for testMethodNoType since:
69134
// - First parameter has no type specified, so type checking is skipped
@@ -76,8 +141,34 @@ public function testRule(): void
76141
// - Second parameter has no type specified, so type checking is skipped
77142
[
78143
'Method TestClass::testMethodWithWrongType parameter #1 should be of type string, int given.',
79-
13,
144+
17,
145+
],
146+
147+
// Errors for testMaxParams - exceeds max parameters
148+
[
149+
'Method TestClass::testMaxParams has 4 parameters, but at most 2 allowed.',
150+
22,
151+
],
152+
153+
// Errors for testNameMismatch - parameter names don't match patterns
154+
[
155+
'Method TestClass::testNameMismatch parameter #1 name "wrongName" does not match pattern /^param/.',
156+
27,
157+
],
158+
[
159+
'Method TestClass::testNameMismatch parameter #2 name "anotherWrong" does not match pattern /^param/.',
160+
27,
80161
],
162+
163+
// No errors for testNullableTypes - nullable types should match correctly
164+
165+
// No errors for testClassTypes - class types should match correctly
166+
167+
// No errors for testProtectedMethod - protected visibility matches
168+
169+
// No errors for testNoVisibilityReq - no visibility requirement specified
170+
171+
// No errors for testValidMethod - everything matches correctly
81172
]);
82173
}
83174
}

0 commit comments

Comments
 (0)