I have this very simple code which I am failing to see how to reach a coverage target of 100%.
final class StringValidator
{
public function matches(
string $pattern,
string $string
): bool {
$result = @preg_match($pattern, $string);
if (false === $result) {
throw new InvalidRegexPatternException();
}
return (bool) $result;
}
}
The test file contains :
/**
* @covers StringValidator
*/
final class StringValidatorTest extends TestCase
{
private StringValidator $validator;
protected function setUp(): void
{
$this->validator = new StringValidator();
}
public function testMatchThrowsOnInvalidRegex(): void
{
$this->expectException(InvalidRegexPatternException::class);
$this->validator->matches('/', 'string');
}
public function testMatchReturns0WhenNoMatchIsFound(): void
{
$result = $this->validator->matches('/xyz/', 'abc');
$this->assertFalse($result);
}
public function testMatchReturns1WhenMatchIsFound(): void
{
$result = $this->validator->matches('/abc/', 'abc');
$this->assertTrue($result);
}
}
The coverage reports indicates this :
StringValidator
Methods: 0.00% ( 0/ 1) Paths: 50.00% ( 2/ 4) Branches: 83.33% ( 5/ 6) Lines: 100.00% ( 4/ 4)
I struggle to understand which methods, paths and branches I am missing. There may be tools (or ways to use and understand the tools I have) that I am unaware of, so guidance about how to interpret this result will be appreciated.
I run PHP Unit with :
php -d xdebug.mode=debug vendor/bin/phpunit --configuration config/phpunit.xml --coverage-text
I do delete phpunit's cache quite often to be sure it's not a cache issue.
If the configuration can be relevant, here's my phpunit.xml :
../sources
../sources/**/**/**/Entity
../sources/**/**/Entity
../sources/**/**/**/ValueObject
../sources/**/**/ValueObject
../sources/**/**/**/Enum
../sources/**/**/Enum
../sources/**/**/**/Repository
../sources/**/**/Repository
../sources/**/**/**/Exception
../sources/**/**/Exception
../sources/**/**/**/Event
../sources/**/**/Event
../sources/**/**/**/Command
../sources/**/**/Command
../sources/**/**/**/Query
../sources/**/**/Query
../sources/**/**/**/DTO
../sources/**/**/DTO
../sources/**/**/Test
../sources/**/Test
../sources/**/**/Config
../sources/**/Config
../tests/Unit
../sources/**/Test
../sources/SubDomains/**/Test
I use PHP Unit 9.6.34, and php --version outputs :
PHP 8.4.12 (cli) (built: Sep 8 2025 21:18:18) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.12, Copyright (c) Zend Technologies
with Zend OPcache v8.4.12, Copyright (c), by Zend Technologies
with Xdebug v3.5.1, Copyright (c) 2002-2026, by Derick Rethans