This package is an implementation to trace code coverage of Twig templates based on xdebug.
The Bootstrapper
class provides static methods to set up your Twig environment to be ready
for coverage testing. Once setup, you can render your templates the known way using the Twig render()
method.
Ensure that you are not using a cache in your environment, otherwise the tracer will end up throwing a "Tracer could not find coverage statistics" exception.
To access render results, use the Tracer::getCoverages()
method. It returns an array of
TemplateCoverageResult
objects that provide the following methods:
getTemplateName(): string
Returns the rendered template namegetCalledLines(): int[]
Returns a list of line numbers that have been executedgetUncalledLines(): int[]
Returns a list of line numbers that have not been executedgetAllLinesByCallStatus() : array
Returns a map with the line numbers as keys and their call status as valuesgetCalledLinesPercentage(): string
Returns a string value between 0 and 100 with a precision of 4
Be aware that
- results of multiple runs are not aggregated; running
$twig->render('foo.twig')
twice will return two coverage results - all line numbers refer to the compiled PHP code, not the template source
Call Status magic numbers are a 1:1 representation of the xdebug code coverage analysis:
1
if a line has been executed-1
if a line has not been executed-2
if a line has no executable code in it
In order to correctly collect all coverage, this library wraps the following tags that are supplied from Twig:
include
The following tags and functions are currently unsupported and will most likely produce unexpected results or even fail when being rendered:
import
from
embed
block
parent()
Additionally, if your implementation provides other extensions that render PHP template code outside of Twig's
doDisplay()
, coverage analysis will most likely fail due to how we collect coverage.