Skip to content

Commit b55061c

Browse files
committed
Drop the message argument from assertArchRule()
An ArchRule cannot be built without because(), and the reason it carries is already rendered in the failure message, so the argument was a second place to write the same sentence and a chance for the two to drift apart. PHPUnit's message convention earns its keep on assertTrue() and friends, which have no domain context to report. This constraint has plenty, and with one rule per test method the test name identifies the assertion already. Callers who really need to prepend something can use assertThat() with the constraint, which is the right level for an escape hatch. The test that covered the argument now asserts what replaces it: that the reason passed to because() reaches the failure message. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FE8ESEV7TzGLDwJuTfdq95
1 parent f95ae82 commit b55061c

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ final class ArchitectureTest extends TestCase
4747
}
4848
```
4949

50-
That is the whole API: `assertArchRule(ArchRule $rule, ClassSet $classSet, string $message = '')`.
51-
One rule per test method reads best — PHPUnit names the broken rule and still reports the ones that
52-
pass. `ArchRuleAsserts` is a trait rather than a base test case so it also works when the parent
53-
class is already taken, by `KernelTestCase` or your own.
50+
That is the whole API: `assertArchRule(ArchRule $rule, ClassSet $classSet)`. There is no message
51+
argument, because a rule cannot be built without `because()` — the reason is already in the failure
52+
output. One rule per test method reads best: PHPUnit names the broken rule and still reports the
53+
ones that pass. `ArchRuleAsserts` is a trait rather than a base test case so it also works when the
54+
parent class is already taken, by `KernelTestCase` or your own.
5455

5556
## Writing rules
5657

src/ArchRuleAsserts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ trait ArchRuleAsserts
1919
{
2020
/**
2121
* Asserts that every class in $classSet satisfies $rule.
22+
*
23+
* There is deliberately no $message argument: an ArchRule cannot be built without
24+
* because(), so the reason is already part of the failure message. Anyone who really
25+
* needs to prepend something can call assertThat() with the constraint directly.
2226
*/
23-
public static function assertArchRule(ArchRule $rule, ClassSet $classSet, string $message = ''): void
27+
public static function assertArchRule(ArchRule $rule, ClassSet $classSet): void
2428
{
25-
Assert::assertThat($rule, new ArchRuleCheckerConstraintAdapter($classSet), $message);
29+
Assert::assertThat($rule, new ArchRuleCheckerConstraintAdapter($classSet));
2630
}
2731
}

tests/ArchRuleAssertsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function test_it_fails_on_a_violated_rule(): void
3131
self::assertArchRule(self::violatedRule(), self::mvcClassSet());
3232
}
3333

34-
public function test_it_prepends_the_custom_message_to_the_failure(): void
34+
public function test_it_reports_the_reason_given_to_because(): void
3535
{
3636
$this->expectException(ExpectationFailedException::class);
37-
$this->expectExceptionMessage('controllers must be container aware');
37+
$this->expectExceptionMessage('because i said so');
3838

39-
self::assertArchRule(self::violatedRule(), self::mvcClassSet(), 'controllers must be container aware');
39+
self::assertArchRule(self::violatedRule(), self::mvcClassSet());
4040
}
4141

4242
private static function mvcClassSet(): ClassSet

0 commit comments

Comments
 (0)