r/PHPhelp • u/Fluent_Press2050 • Apr 12 '26
Code coverage for read only DTO/value object
Since there’s no behavior to test in something that only has a constructor, do we just put @codeCoverageIgnore above the constructor?
readonly class Point
{
public function __construct(
public int $x,
public int $y,
) {}
}
2
Upvotes
2
u/Crafty-Pool7864 Apr 12 '26
If you ignore your DTOs (and I often do) I prefer doing so in your test config once rather than needing to annotate each class.
If you want them I coverage reports, mark them as tested by the tests for whatever they are a dto for.
1
u/exqueezemenow Apr 12 '26
Maybe it's pointless, But I always have a test that creates an object and tests that the values exist.
1
u/barthvonries Apr 12 '26
Code coverage is not a goal, only a metric.
100% code coverage is a bad goal. Test what you feel needs testing. If something does nothing (like the class you provide here), don't try to cover it with your tests. You will only waste resources during pipelines with absolutely no value added.
1
u/gaborj Apr 12 '26
You should be using it in your tests
1
u/mark_b Apr 12 '26
The example you've posted is a very simple one. But what about if it needs validation, for example if $x or $y must not be above or below a certain value?