Skip to content

Commit 8ff6a33

Browse files
add tests on the ComponentMapper for a future refacto
1 parent bf082e0 commit 8ff6a33

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

app/Presenter/Analyze/Component/Shared/Component.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function countClasses(): int
2525
return $this->countClasses;
2626
}
2727

28-
public function countAbstractions(): float
28+
public function countAbstractions(): int
2929
{
3030
return $this->countAbstractions;
3131
}

app/Presenter/Analyze/Component/Shared/ComponentMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function from(array $metrics): array
4949
$component,
5050
count($componentMetrics),
5151
$abstract,
52-
$abstract / count($componentMetrics),
53-
$instability / count($componentMetrics),
52+
$abstract / (count($componentMetrics) ?: 1),
53+
$instability / (count($componentMetrics) ?: 1),
5454
$innerDependencies,
5555
);
5656
}

tests/Builders/AnalyzeMetricBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public function withName(string $value): self
2222
return $this;
2323
}
2424

25+
public function isAbstract(): self
26+
{
27+
$this->abstract = true;
28+
29+
return $this;
30+
}
31+
2532
public function withDependencies(array $value): self
2633
{
2734
$this->dependencies = $value;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
use App\Presenter\Analyze\Component\Shared\Component;
4+
use App\Presenter\Analyze\Component\Shared\ComponentMapper;
5+
6+
it('should map metrics to components', function () {
7+
8+
$mapper = new ComponentMapper();
9+
10+
$metrics = [
11+
'A' => [
12+
$this->oneAnalyzeMetric()->withName('A\Class1')->build(),
13+
$this->oneAnalyzeMetric()->withName('A\Class2')->build(),
14+
$this->oneAnalyzeMetric()->withName('A\Class3')->build(),
15+
]
16+
];
17+
18+
$components = $mapper->from($metrics);
19+
20+
expect($components)->toHaveCount(1);
21+
expect($components[0])->toBeInstanceOf(Component::class);
22+
expect($components[0]->name())->toBe('A');
23+
});
24+
25+
it('should map metrics to components with dependencies', function () {
26+
27+
$mapper = new ComponentMapper();
28+
29+
$metrics = [
30+
'A' => [
31+
$this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['B\Class2'])->build(),
32+
],
33+
'B' => [
34+
//
35+
]
36+
];
37+
38+
$components = $mapper->from($metrics);
39+
40+
expect($components)->toHaveCount(2);
41+
expect($components[0]->dependencies())->toBe(['B']);
42+
43+
});
44+
45+
it('should not keep dependencies from unwanted namespaces', function () {
46+
47+
$mapper = new ComponentMapper();
48+
49+
$metrics = [
50+
'A' => [
51+
/**
52+
* This dependency is in an unwanted namespace C
53+
*/
54+
$this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['C\Class2'])->build(),
55+
],
56+
'B' => [
57+
//
58+
]
59+
];
60+
61+
$components = $mapper->from($metrics);
62+
63+
expect($components)->toHaveCount(2);
64+
expect($components[0]->dependencies())->toBe([]);
65+
});
66+
67+
it('should calculate the average abstractness', function () {
68+
69+
$mapper = new ComponentMapper();
70+
71+
$metrics = [
72+
'A' => [
73+
$this->oneAnalyzeMetric()->build(),
74+
$this->oneAnalyzeMetric()->isAbstract()->build(),
75+
$this->oneAnalyzeMetric()->isAbstract()->build(),
76+
$this->oneAnalyzeMetric()->isAbstract()->build(),
77+
],
78+
];
79+
80+
$components = $mapper->from($metrics);
81+
82+
expect($components[0]->countClasses())->toBe(4);
83+
expect($components[0]->countAbstractions())->toBe(3);
84+
expect($components[0]->abstractness())->toBe(0.75);
85+
});
86+
87+
it('should calculate the average instability', function () {
88+
89+
$mapper = new ComponentMapper();
90+
91+
$metrics = [
92+
'A' => [
93+
$this->oneAnalyzeMetric()->withInstability(0.3)->build(),
94+
$this->oneAnalyzeMetric()->withInstability(0.7)->build(),
95+
$this->oneAnalyzeMetric()->withInstability(1)->build(),
96+
],
97+
];
98+
99+
$components = $mapper->from($metrics);
100+
101+
expect($components[0]->instability())->toBe(0.67);
102+
});

0 commit comments

Comments
 (0)