Skip to content

Commit

Permalink
Improve component architecture $because
Browse files Browse the repository at this point in the history
Make it express the intent of the applied rule was,
at the component level.
  • Loading branch information
hgraca committed Oct 1, 2023
1 parent 08ef200 commit fce550e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
23 changes: 19 additions & 4 deletions src/RuleBuilders/Architecture/Architecture.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function mustNotDependOnComponents(string ...$componentNames)
return $this;
}

public function rules(string $because = 'of component architecture'): iterable
public function rules(string $because = null): iterable
{
foreach ($this->componentSelectors as $name => $selector) {
if (isset($this->allowedDependencies[$name])) {
Expand All @@ -118,21 +118,36 @@ public function rules(string $because = 'of component architecture'): iterable
->should($this->createAllowedExpression(
array_merge([$name], $this->allowedDependencies[$name])
))
->because($because);
->because(
$because
?? "$name can only depend on itself"
.(
\count($this->allowedDependencies[$name])
? ' and on '.implode(', ', $this->allowedDependencies[$name])
: ''
)
);
}

if (isset($this->componentDependsOnlyOnTheseComponents[$name])) {
yield Rule::allClasses()
->that(\is_string($selector) ? new ResideInOneOfTheseNamespaces($selector) : $selector)
->should($this->createAllowedExpression($this->componentDependsOnlyOnTheseComponents[$name]))
->because($because);
->because(
$because
?? "$name can only depend on "
.implode(', ', $this->componentDependsOnlyOnTheseComponents[$name])
);
}

if (isset($this->forbiddenDependencies[$name])) {
yield Rule::allClasses()
->that(\is_string($selector) ? new ResideInOneOfTheseNamespaces($selector) : $selector)
->should($this->createForbiddenExpression($this->forbiddenDependencies[$name]))
->because($because);
->because(
$because
?? "$name must not depend on ".implode(', ', $this->forbiddenDependencies[$name])
);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/Architecture/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public function test_layered_architecture(): void
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Domain\*')
))
->because('of component architecture'),
->because('Domain can only depend on itself'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Application\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Application\*', 'App\*\Domain\*')
))
->because('of component architecture'),
->because('Application can only depend on itself and on Domain'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Infrastructure\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Infrastructure\*', 'App\*\Domain\*', 'App\*\Application\*')
))
->because('of component architecture'),
->because('Infrastructure can only depend on itself and on Domain, Application, Infrastructure'),
];

$actualRules = iterator_to_array($rules);
Expand All @@ -69,19 +69,19 @@ public function test_layered_architecture_with_expression(): void
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Domain\*')
))
->because('of component architecture'),
->because('Domain can only depend on itself'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Application\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Application\*', 'App\*\Domain\*')
))
->because('of component architecture'),
->because('Application can only depend on itself and on Domain'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Infrastructure\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Infrastructure\*', 'App\*\Domain\*', 'App\*\Application\*')
))
->because('of component architecture'),
->because('Infrastructure can only depend on itself and on Domain, Application, Infrastructure'),
];

$actualRules = iterator_to_array($rules);
Expand All @@ -107,19 +107,19 @@ public function test_layered_architecture_with_mix_of_namespace_and_expression()
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Domain\*')
))
->because('of component architecture'),
->because('Domain can only depend on itself'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Application\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Application\*', 'App\*\Domain\*')
))
->because('of component architecture'),
->because('Application can only depend on itself and on Domain'),
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Infrastructure\*'))
->should(new DependsOnlyOnTheseExpressions(
new ResideInOneOfTheseNamespaces('App\*\Domain\*', 'App\*\Application\*', 'App\*\Infrastructure\*')
))
->because('of component architecture'),
->because('Infrastructure can only depend on itself and on Domain, Application, Infrastructure'),
];

$actualRules = iterator_to_array($rules);
Expand All @@ -138,7 +138,7 @@ public function test_layered_architecture_with_depends_only_on_components(): voi
Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\*\Domain\*'))
->should(new DependsOnlyOnTheseExpressions(new ResideInOneOfTheseNamespaces('App\*\Domain\*')))
->because('of component architecture'),
->because('Domain can only depend on Domain'),
];

self::assertEquals($expectedRules, iterator_to_array($rules));
Expand Down

0 comments on commit fce550e

Please sign in to comment.