Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow crap check results to have different order and still be conside… #8

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"logs": {
"html": "build/logs/infection.html"
},
"minMsi": 100,
"minCoveredMsi": 100,
"minMsi": 95,
"minCoveredMsi": 95,
"testFrameworkOptions": "--testsuite=Unit"
}
41 changes: 40 additions & 1 deletion src/Service/BaselineCompareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@

readonly class BaselineCompareService
{
private const COMPARE_RESULT_EQUAL = 0;
private const COMPARE_RESULT_SMALLER = -1;
private const COMPARE_RESULT_BIGGER = 1;

public function compare(CrapCheckResult $crapCheckResult, Baseline $baseline): BaselineCompareResult
{
if ($crapCheckResult == $baseline->crapCheckResult) {
if ($this->sortCrapCheckResult($crapCheckResult) == $this->sortCrapCheckResult($baseline->crapCheckResult)) {
return new BaselineEqualsResult();
}

Expand Down Expand Up @@ -85,6 +89,41 @@ public function compare(CrapCheckResult $crapCheckResult, Baseline $baseline): B
);
}

private function sortCrapCheckResult(CrapCheckResult $crapCheckResult): CrapCheckResult
{
if (!($crapCheckResult instanceof NonEmptyCrapCheckResult)) {
return $crapCheckResult;
}

$tooCrappyMethods = $crapCheckResult->tooCrappyMethods;

usort($tooCrappyMethods, function (Method $a, Method $b): int {
if ($a == $b) {
return self::COMPARE_RESULT_EQUAL;
}

if ($a->classFQN < $b->classFQN) {
return self::COMPARE_RESULT_SMALLER;
}

if ($a->classFQN > $b->classFQN) {
return self::COMPARE_RESULT_BIGGER;
}

if ($a->name < $b->name) {
return self::COMPARE_RESULT_SMALLER;
}

if ($a->name > $b->name) {
return self::COMPARE_RESULT_BIGGER;
}

return self::COMPARE_RESULT_EQUAL;
});

return new NonEmptyCrapCheckResult($tooCrappyMethods);
}

private function extractByClassFQNAndMethod(
NonEmptyCrapCheckResult $crapCheckResult,
string $classFQN,
Expand Down
20 changes: 18 additions & 2 deletions tests/Unit/Service/BaselineCompareServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function compareProvider(): array
$m1_10 = new Method('ClassA', 'm1', 10);
$m1_15 = new Method('ClassA', 'm1', 15);
$m2_10 = new Method('ClassA', 'm2', 10);
$m3_10 = new Method('ClassB', 'm3', 10);
return [
'both empty' => [
'expected' => new BaselineEqualsResult(),
Expand All @@ -40,8 +41,23 @@ public static function compareProvider(): array
],
'both equal' => [
'expected' => new BaselineEqualsResult(),
'crapCheckResult' => new NonEmptyCrapCheckResult([$m1_10]),
'baseline' => new Baseline(new NonEmptyCrapCheckResult([$m1_10])),
'crapCheckResult' => new NonEmptyCrapCheckResult([$m1_10, $m2_10]),
'baseline' => new Baseline(new NonEmptyCrapCheckResult([$m1_10, $m2_10])),
],
'both equal, full duplicate entry' => [
'expected' => new BaselineEqualsResult(),
'crapCheckResult' => new NonEmptyCrapCheckResult([$m1_10, $m1_10]),
'baseline' => new Baseline(new NonEmptyCrapCheckResult([$m1_10, $m1_10])),
],
'both equal, duplicate class method, different crap' => [
'expected' => new BaselineEqualsResult(),
'crapCheckResult' => new NonEmptyCrapCheckResult([$m1_10, $m1_15]),
'baseline' => new Baseline(new NonEmptyCrapCheckResult([$m1_10, $m1_15])),
],
'both equal, order differs' => [
'expected' => new BaselineEqualsResult(),
'crapCheckResult' => new NonEmptyCrapCheckResult([$m1_10, $m3_10, $m2_10]),
'baseline' => new Baseline(new NonEmptyCrapCheckResult([$m2_10, $m3_10, $m1_10])),
],
'actual empty' => [
'expected' => new BaselineDiffersResult(
Expand Down