Skip to content

Commit 2ae6705

Browse files
Update documentation
1 parent d827770 commit 2ae6705

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

README.md

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,108 @@ $ ./tools/phpcov --version
3030

3131
## Usage
3232

33-
### Merging exported php-code-coverage objects stored in *.cov files
33+
### Merging Serialized Code Coverage Data
34+
35+
When you run PHPUnit with `--coverage-php`, it writes serialized code coverage data to a `.cov` file.
36+
The `phpcov merge` command merges multiple `.cov` files into a single code coverage report.
37+
38+
This is useful when you run test suites in parallel or across separate processes and want to combine the results into one report.
39+
40+
#### Example
41+
42+
Run your test suites separately, each writing their own `.cov` file:
3443

3544
```
36-
$ parallel --gnu ::: \
37-
'phpunit --coverage-php /tmp/coverage/FooTest.cov tests/FooTest' \
38-
'phpunit --coverage-php /tmp/coverage/BarTest.cov tests/BarTest'
45+
$ phpunit --coverage-php /tmp/coverage/FooTest.cov tests/FooTest
46+
$ phpunit --coverage-php /tmp/coverage/BarTest.cov tests/BarTest
3947
```
4048

49+
Then merge the `.cov` files and generate a report:
50+
4151
```
42-
$ phpcov merge --clover /tmp/clover.xml /tmp/coverage
43-
phpcov 8.1.0 by Sebastian Bergmann.
52+
$ phpcov merge --html /tmp/coverage-html /tmp/coverage
53+
```
54+
55+
All `.cov` files in the specified directory will be merged.
56+
57+
You can generate reports in multiple formats at once:
4458

45-
Generating code coverage report in Clover XML format ... done
59+
```
60+
$ phpcov merge --html /tmp/coverage-html --openclover /tmp/coverage.xml /tmp/coverage
4661
```
4762

48-
### Patch Coverage
63+
#### Exporting merged data
64+
65+
Use `--php` to write the merged coverage data back to a `.cov` file:
4966

5067
```
51-
$ git diff HEAD^1 > /tmp/patch.txt
68+
$ phpcov merge --php /tmp/merged.cov /tmp/coverage
5269
```
5370

71+
#### Merging on a different machine
72+
73+
The serialized `.cov` files store file paths relative to a base path (the common path prefix of all covered files).
74+
When generating reports, the source files must be readable so that they can be analysed.
75+
76+
If you are merging `.cov` files on a different machine than where the tests were run, and the source code is located at a different path, use `--source` to specify where the source code is on the current machine:
77+
5478
```
55-
$ phpunit --coverage-php /tmp/coverage.cov
79+
$ phpcov merge --source /home/ci/project --html /tmp/coverage-html /tmp/coverage
5680
```
5781

82+
#### Available Report Formats
83+
84+
| Option | Format |
85+
|-----------------------|-------------------------|
86+
| `--clover <file>` | Clover XML |
87+
| `--openclover <file>` | OpenClover XML |
88+
| `--cobertura <file>` | Cobertura XML |
89+
| `--crap4j <file>` | Crap4J XML |
90+
| `--html <directory>` | HTML |
91+
| `--php <file>` | Serialized PHP (`.cov`) |
92+
| `--text <file>` | Plain text |
93+
| `--xml <directory>` | PHPUnit XML |
94+
95+
### Patch Coverage
96+
97+
The `phpcov patch-coverage` command calculates code coverage for the lines changed in a patch (unified diff).
98+
It reports how many of the changed executable lines are covered by tests.
99+
100+
#### Example
101+
102+
Generate a unified diff and collect code coverage:
103+
104+
```
105+
$ git diff HEAD^1 > /tmp/patch.txt
106+
$ phpunit --coverage-php /tmp/coverage.cov
58107
```
59-
$ phpcov patch-coverage --path-prefix /path/to/project /tmp/coverage.cov /tmp/patch.txt
60-
phpcov 8.1.0 by Sebastian Bergmann.
61108

109+
Then calculate patch coverage:
110+
111+
```
112+
$ phpcov patch-coverage /tmp/coverage.cov /tmp/patch.txt
62113
1 / 2 changed executable lines covered (50.00%)
63114
64115
Changed executable lines that are not covered:
65116
66117
Example.php:11
67118
```
119+
120+
The command exits with code `0` when all changed executable lines are covered.
121+
It exits with `1` when some changed executable lines are not covered.
122+
It exits with `2` when no changed executable lines could be detected. This usually indicates a path mismatch.
123+
124+
#### The `--path-prefix` option
125+
126+
The `--path-prefix` option is needed when the file paths in the patch do not match the file paths in the coverage data.
127+
128+
The serialized `.cov` file stores file paths relative to a base path.
129+
The patch file contains paths relative to wherever `git diff` (or equivalent) was run.
130+
When both are relative to the same project root directory, which is the common case, they match and `--path-prefix` is not needed.
131+
132+
When the paths do not match, `--path-prefix` specifies the directory to prepend to the paths in the patch so that they can be resolved against the coverage data.
133+
For example, if the diff was generated from a parent directory and contains paths like `project/src/Foo.php`, but the coverage data has paths relative to the project root (`src/Foo.php`), you would use:
134+
135+
```
136+
$ phpcov patch-coverage --path-prefix /path/to/project /tmp/coverage.cov /tmp/patch.txt
137+
```

0 commit comments

Comments
 (0)