Code Coverage In AI era
Jan Nedbal
Principal Engineer at ShipMonk
function refund(Order $order): void
{
if ($order->isPaid()) {
$this->bank->refund($order);
}
$order->markRefunded();
}
…and I know why
function refund(Order $order): void
{
if ($order->isPaid()) {
$this->bank->refund($order);
}
$order->markRefunded();
}
<file name="src/Order/OrderService.php"> <line num="1" type="method" name="refund" count="1"/> <line num="3" type="stmt" count="1"/> <line num="4" type="stmt" count="0"/> <line num="6" type="stmt" count="1"/> <metrics statements="3" coveredstatements="2"/> </file>
Almost plain PHP speed
Fail CI on your rules
…we need to know how coverage works
PHP runs opcodes, not lines — Xdebug & PCOV hook each one
require 'fee.php'; fee(3);
function fee(int $weight): int
{
if ($weight <= 0) {
throw new LogicException();
}
$fee = $weight * 5;
return $fee;
}
3 | IS_SMALLER_OR_EQUAL | $weight, 0 3 | JMPZ | → MUL 4 | NEW | LogicException 4 | DO_FCALL | 4 | THROW | 6 | MUL | $weight, 5 6 | ASSIGN | $fee 7 | VERIFY_RETURN_TYPE | $fee 7 | RETURN | $fee
With executable lines, we can calculate coverage %
function total(array $items): int
{
$sum = 0;
foreach ($items as $item) {
$sum += $item->price;
}
return $sum;
}
function total(array $items): int
{
$sum = 0;
foreach ($items as $item) {
$sum += $item->price;
}
return $sum;
}
Introducing a new PHP extension
Driver overhead only · Carbon test suite · 6,349 tests · PHP 8.5
fee(3)1st call2nd call
with turbocov, most of the cost is PHPUnit
Carbon test suite · 6,349 tests · PHP 8.5 · clover report
whole codebase
1× per day, to track the metric
changed files only
(whole run: driver + PHPUnit)
Carbon test suite · 2 changed files · clover report
PCOV can filter the report too, but not its hook
Carbon test suite · full run
MR mode · ShipMonk core: CI, ~42k tests, 5 changed files · Carbon: 2 changed files
…but how to make it useful?
See it
public function cancel(Order $order): void
{
if ($order->isShipped()) {
throw new CannotCancelShippedOrder();
}
$order->markCancelled();
$this->refunds->refund($order);
}
.gitlab-ci.yml artifact
coverage_report: coverage_format: cobertura path: cobertura.xml
See it
required → no opcodes → unknown count of executable lines
PHPUnit reads its AST → 5 executable lines
Enforce it
rules, not numbers
Enforce it
public function inspect(
ClassMethodBlock $block,
InspectionContext $context,
): ?CoverageError {
if (
$context->getClassReflection()?->isSubclassOf(Facade::class)
&& $block->getChangePercentage() === 100
&& $block->getCoveragePercentage() === 0
) {
return CoverageError::create('New facade method without tests!');
}
return null;
}
Enforce it
public function ship(Order $order): void
{
if (!$order->isPaid()) {
throw new LogicException('Unreachable');
}
$this->carrier->send($order);
$order->markShipped();
}
$config->addExecutableLineExcluder(
new IgnoreThrowNewExceptionLineExcluder([LogicException::class]),
);
Enforce it
✓ --patch checks only the files & methods changed in the MR
Enforce it
shipmonk/turbocov
github.com/shipmonk-rnd/turbocov
shipmonk/coverage-guard
github.com/shipmonk-rnd/coverage-guard
Questions?