Skip to content

Commit

Permalink
add tests on the ComponentMapper for a future refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
DeGraciaMathieu committed Nov 14, 2024
1 parent bf082e0 commit 8ff6a33
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Presenter/Analyze/Component/Shared/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function countClasses(): int
return $this->countClasses;
}

public function countAbstractions(): float
public function countAbstractions(): int
{
return $this->countAbstractions;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Presenter/Analyze/Component/Shared/ComponentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function from(array $metrics): array
$component,
count($componentMetrics),
$abstract,
$abstract / count($componentMetrics),
$instability / count($componentMetrics),
$abstract / (count($componentMetrics) ?: 1),
$instability / (count($componentMetrics) ?: 1),
$innerDependencies,
);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Builders/AnalyzeMetricBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public function withName(string $value): self
return $this;
}

public function isAbstract(): self
{
$this->abstract = true;

return $this;
}

public function withDependencies(array $value): self
{
$this->dependencies = $value;
Expand Down
102 changes: 102 additions & 0 deletions tests/Unit/Presenter/ComponentMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use App\Presenter\Analyze\Component\Shared\Component;
use App\Presenter\Analyze\Component\Shared\ComponentMapper;

it('should map metrics to components', function () {

$mapper = new ComponentMapper();

$metrics = [
'A' => [
$this->oneAnalyzeMetric()->withName('A\Class1')->build(),
$this->oneAnalyzeMetric()->withName('A\Class2')->build(),
$this->oneAnalyzeMetric()->withName('A\Class3')->build(),
]
];

$components = $mapper->from($metrics);

expect($components)->toHaveCount(1);
expect($components[0])->toBeInstanceOf(Component::class);
expect($components[0]->name())->toBe('A');
});

it('should map metrics to components with dependencies', function () {

$mapper = new ComponentMapper();

$metrics = [
'A' => [
$this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['B\Class2'])->build(),
],
'B' => [
//
]
];

$components = $mapper->from($metrics);

expect($components)->toHaveCount(2);
expect($components[0]->dependencies())->toBe(['B']);

});

it('should not keep dependencies from unwanted namespaces', function () {

$mapper = new ComponentMapper();

$metrics = [
'A' => [
/**
* This dependency is in an unwanted namespace C
*/
$this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['C\Class2'])->build(),
],
'B' => [
//
]
];

$components = $mapper->from($metrics);

expect($components)->toHaveCount(2);
expect($components[0]->dependencies())->toBe([]);
});

it('should calculate the average abstractness', function () {

$mapper = new ComponentMapper();

$metrics = [
'A' => [
$this->oneAnalyzeMetric()->build(),
$this->oneAnalyzeMetric()->isAbstract()->build(),
$this->oneAnalyzeMetric()->isAbstract()->build(),
$this->oneAnalyzeMetric()->isAbstract()->build(),
],
];

$components = $mapper->from($metrics);

expect($components[0]->countClasses())->toBe(4);
expect($components[0]->countAbstractions())->toBe(3);
expect($components[0]->abstractness())->toBe(0.75);
});

it('should calculate the average instability', function () {

$mapper = new ComponentMapper();

$metrics = [
'A' => [
$this->oneAnalyzeMetric()->withInstability(0.3)->build(),
$this->oneAnalyzeMetric()->withInstability(0.7)->build(),
$this->oneAnalyzeMetric()->withInstability(1)->build(),
],
];

$components = $mapper->from($metrics);

expect($components[0]->instability())->toBe(0.67);
});

0 comments on commit 8ff6a33

Please sign in to comment.