Skip to content

Commit a382291

Browse files
committed
Add some tests for Signature comparator
1 parent 4f565b6 commit a382291

File tree

4 files changed

+123
-16
lines changed

4 files changed

+123
-16
lines changed

src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter)
8787

8888
if (!$this->areMethodsEqual($methodBefore, $methodAfter)) {
8989

90-
$paramsBefore = $methodBefore->params;
91-
$paramsAfter = $methodAfter->params;
92-
93-
$signatureResult = Signature::analyze($paramsBefore, $paramsAfter);
90+
$signatureResult = Signature::analyze($methodBefore, $methodAfter);
9491

9592
$changes = [
9693
'parameter_added' => ClassMethodParameterAdded::class,

src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
5555

5656
// Leave non-strict comparison here
5757
if ($functionBefore != $functionAfter) {
58-
$paramsBefore = $functionBefore->params;
59-
$paramsAfter = $functionAfter->params;
6058

61-
$signatureResult = Signature::analyze($paramsBefore, $paramsAfter);
59+
$signatureResult = Signature::analyze($functionBefore, $functionAfter);
6260

6361
$changes = [
6462
'parameter_added' => FunctionParameterAdded::class,

src/PHPSemVerChecker/Comparator/Signature.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace PHPSemVerChecker\Comparator;
44

5-
class Signature {
6-
/**
7-
* @param array $parametersA
8-
* @param array $parametersB
9-
* @return array
10-
*/
11-
public static function analyze(array $parametersA, array $parametersB)
5+
use PhpParser\Node\FunctionLike;
6+
7+
class Signature
8+
{
9+
10+
public static function analyze(FunctionLike $functionA, FunctionLike $functionB)
1211
{
1312
$changes = [
13+
'function_renamed' => false,
14+
'function_renamed_case_only' => false,
1415
'parameter_added' => false,
1516
'parameter_removed' => false,
1617
'parameter_renamed' => false,
@@ -20,6 +21,17 @@ public static function analyze(array $parametersA, array $parametersB)
2021
'parameter_default_removed' => false,
2122
'parameter_default_value_changed' => false,
2223
];
24+
25+
$changes = self::detectParameterChanges($changes, $functionA, $functionB);
26+
27+
return $changes;
28+
}
29+
30+
private static function detectParameterChanges($changes, FunctionLike $functionA, FunctionLike $functionB)
31+
{
32+
$parametersA = $functionA->getParams();
33+
$parametersB = $functionB->getParams();
34+
2335
$lengthA = count($parametersA);
2436
$lengthB = count($parametersB);
2537

@@ -56,8 +68,8 @@ public static function analyze(array $parametersA, array $parametersB)
5668
$changes['parameter_default_removed'] = true;
5769
} elseif ($parametersA[$i]->default === null && $parametersB[$i]->default !== null) {
5870
$changes['parameter_default_added'] = true;
59-
// TODO(tom@tomrochette.com): Not all nodes have a value property
60-
} elseif ( ! Node::isEqual($parametersA[$i]->default, $parametersB[$i]->default)) {
71+
// TODO(tom@tomrochette.com): Not all nodes have a value property
72+
} elseif (!Node::isEqual($parametersA[$i]->default, $parametersB[$i]->default)) {
6173
$changes['parameter_default_value_changed'] = true;
6274
}
6375
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Test\Comparator;
4+
5+
use PhpParser\Node\Param;
6+
use PhpParser\Node\Stmt\ClassMethod;
7+
use PHPSemVerChecker\Comparator\Signature;
8+
9+
class SignatureTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testIdenticalSignaturesNoParameters()
12+
{
13+
$signature1 = new ClassMethod('testMethod1');
14+
$signature2 = new ClassMethod('testMethod1');
15+
16+
$result = Signature::analyze($signature1, $signature2);
17+
18+
$expectedResult = [
19+
'function_renamed' => false,
20+
'function_renamed_case_only' => false,
21+
'parameter_added' => false,
22+
'parameter_removed' => false,
23+
'parameter_renamed' => false,
24+
'parameter_typing_added' => false,
25+
'parameter_typing_removed' => false,
26+
'parameter_default_added' => false,
27+
'parameter_default_removed' => false,
28+
'parameter_default_value_changed' => false,
29+
];
30+
31+
$this->assertEquals($expectedResult, $result);
32+
}
33+
34+
public function testRenamedParameters()
35+
{
36+
$signature1 = new ClassMethod('testMethod1', [
37+
'params' => [
38+
new Param('testParameter')
39+
],
40+
]);
41+
42+
43+
$signature2 = new ClassMethod('testMethod1', [
44+
'params' => [
45+
new Param('testRenamedParameter')
46+
],
47+
]);
48+
49+
$result = Signature::analyze($signature1, $signature2);
50+
51+
$expectedResult = [
52+
'function_renamed' => false,
53+
'function_renamed_case_only' => false,
54+
'parameter_added' => false,
55+
'parameter_removed' => false,
56+
'parameter_renamed' => true,
57+
'parameter_typing_added' => false,
58+
'parameter_typing_removed' => false,
59+
'parameter_default_added' => false,
60+
'parameter_default_removed' => false,
61+
'parameter_default_value_changed' => false,
62+
];
63+
64+
$this->assertEquals($expectedResult, $result);
65+
}
66+
67+
public function testAddedParameters()
68+
{
69+
$signature1 = new ClassMethod('testMethod1', [
70+
'params' => [
71+
new Param('testParameter'),
72+
],
73+
]);
74+
75+
76+
$signature2 = new ClassMethod('testMethod1', [
77+
'params' => [
78+
new Param('testParameter'),
79+
new Param('testAddedParameter'),
80+
],
81+
]);
82+
83+
$result = Signature::analyze($signature1, $signature2);
84+
85+
$expectedResult = [
86+
'function_renamed' => false,
87+
'function_renamed_case_only' => false,
88+
'parameter_added' => true,
89+
'parameter_removed' => false,
90+
'parameter_renamed' => false,
91+
'parameter_typing_added' => false,
92+
'parameter_typing_removed' => false,
93+
'parameter_default_added' => false,
94+
'parameter_default_removed' => false,
95+
'parameter_default_value_changed' => false,
96+
];
97+
98+
$this->assertEquals($expectedResult, $result);
99+
}
100+
}

0 commit comments

Comments
 (0)