Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orbeji/phpunit-pr-coverage-check
php package to analyze code coverage of pull request
https://github.com/orbeji/phpunit-pr-coverage-check
coverage php pr pull-request
Last synced: 1 day ago
JSON representation
php package to analyze code coverage of pull request
- Host: GitHub
- URL: https://github.com/orbeji/phpunit-pr-coverage-check
- Owner: orbeji
- License: mit
- Created: 2024-03-01T18:21:58.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-11-12T20:28:44.000Z (about 1 month ago)
- Last Synced: 2024-12-17T05:57:15.069Z (5 days ago)
- Topics: coverage, php, pr, pull-request
- Language: PHP
- Homepage:
- Size: 72.3 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Tests](https://github.com/orbeji/phpunit-pr-coverage-check/actions/workflows/tests.yml/badge.svg)
![Static analysis](https://github.com/orbeji/phpunit-pr-coverage-check/actions/workflows/static.yml/badge.svg)
![Symfony & php compatibility](https://github.com/orbeji/phpunit-pr-coverage-check/actions/workflows/compatibility.yml/badge.svg)## About The Project
Inspired by https://github.com/richardregeer/phpunit-coverage-check
This project is a console command that filters the coverage report of your Phpunit tests to only take into account the
modifications of a PullRequest, so you can be sure that the PR has enough tests.## Getting Started
### Installation
Make sure Composer is installed globally, as explained in the
[installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.Open a command console, enter your project directory and execute:
```console
$ composer require --dev orbeji/phpunit-pr-coverage-check
```## Usage
There are two ways of using this command:
* Passing a diff file
* Passing git repo informationWith the first option you need to generate the appropiate diff file to pass to the command.
The diff file must only contain the modified files/lines of the pr. To do that you can call
`git diff DESTINATION_BRANCH...PR_BRANCH > diff.txt` with
[Bitbucket](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) and
[GitHub](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables)
you have environment variables to know
the current and destination branches.Diff file example:
Bitbucket
```bash
vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'
git diff origin/${BITBUCKET_PR_DESTINATION_BRANCH}...origin/${BITBUCKET_BRANCH} > diff.txt
vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi
```
GitHub
```bash
vendor/bin/phpunit --coverage-clover clover.xml
git diff ${GITHUB_BASE_REF}...${GITHUB_REF_NAME} > diff.txt
vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt
```
Git repo information
```bash
vendor/bin/pr-coverage-check check clover.xml 100 --pullrequest-id=1 --provider=Github --workspace=orbeji --repository=test --api_token=API_TOKEN
```
The API token must have permissions to the PullRequest scope.For GitHub in the workspace option put the owner of the repo.
In any case if the coverage is not met the command will return the covered percentage
```bash
Coverage 40%
```### Reports
When executing the command line you can pass the option `--report` to generate a table with the uncovered lines of the PRThis option accepts 3 values:
* ansi: Coverage table in console output
* comment: Coverage table in PR comment
* report: Bitbucket report#### Examples:
ansi:
```
root@bbfb2d246e64:/app# bin/pr-coverage-check check tests/clover.xml 90 --diff=tests/diff.txt --report=ansi
Coverage: 40%
--------------- ------------
File Uncovered Lines
--------------- ------------
src/Dummy.php 19, 20, 26
--------------- ------------
```
comment:
![](docs/screenshots/github_comment.png)
![](docs/screenshots/bitbucket_comment.png)
report:### CI Integration
#### Bitbucket Pipelines
```yaml
pipelines:
pull-requests:
feature/*:
- step:
name: phpunit coverage check
image:
name: orbeji/base:7.2-xdebug-cli
script:
- composer install
- vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'
- git diff origin/${BITBUCKET_PR_DESTINATION_BRANCH}...origin/${BITBUCKET_BRANCH} > diff.txt
- vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi
caches:
- composer
```
#### GitHub Actions
```yaml
name: PHPUnit and Coverage Checkon: [pull_request]
jobs:
test:
runs-on: ubuntu-22.04steps:
- name: Checkout code
uses: actions/checkout@v4- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'- name: Install dependencies
run: composer install- name: Run PHPUnit with coverage
run: vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'- name: Git diff
run: git fetch && git diff origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF} > diff.txt- name: PR Coverage Check
run: vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi```
## Roadmap
- [ ] Add Bitbucket report example screenshot
- [x] Add examples of CI for Bitbucket and GitHubSee the [open issues](https://github.com/orbeji/phpunit-pr-coverage-check/issues) for a full list of proposed features (and known issues).
## Contributing
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.Please make sure to update tests as appropriate and that all the GitHub Actions are passing.
## License
Distributed under the [MIT](https://choosealicense.com/licenses/mit/) License.
## Contact
Project Link: [https://github.com/orbeji/phpunit-pr-coverage-check](https://github.com/orbeji/phpunit-pr-coverage-check)