From 0a803737fd1196109d94012f30d37ecb66cf92b9 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:01:24 +0100 Subject: [PATCH 1/2] add code coverage github action --- .github/workflows/code-coverage.yaml | 44 +++++++++++++++++++++++ composer.json | 3 +- phpunit-threshold.php | 53 ++++++++++++++++++++++++++++ phpunit.ci.xml | 9 ++++- 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/code-coverage.yaml create mode 100644 phpunit-threshold.php diff --git a/.github/workflows/code-coverage.yaml b/.github/workflows/code-coverage.yaml new file mode 100644 index 00000000..e42a2cb0 --- /dev/null +++ b/.github/workflows/code-coverage.yaml @@ -0,0 +1,44 @@ +name: PHPUnit Coverage Report + +on: + push: + branches: [develop, master] + pull_request: + branches: [develop, master] + workflow_dispatch: ~ + +permissions: + contents: write + +jobs: + ci: + runs-on: ubuntu-20.04 + defaults: + run: + working-directory: ./ + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 5.6 + tools: composer:v1 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Ensure source code is linted + run: ./vendor/bin/phpcs src + + - name: PHPUnit + run: ./vendor/bin/phpunit -c phpunit.ci.xml --coverage-xml ./.coverage + env: + XDEBUG_MODE: coverage + + - name: PHPUnit threshold + run: php ./phpunit-threshold.php diff --git a/composer.json b/composer.json index eb60eff3..ab398b8c 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2", "psr/log": "^1 || ^2 || ^3", "ext-curl": "*", - "ext-json": "*" + "ext-json": "*", + "ext-simplexml": "*" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", diff --git a/phpunit-threshold.php b/phpunit-threshold.php new file mode 100644 index 00000000..09aa715f --- /dev/null +++ b/phpunit-threshold.php @@ -0,0 +1,53 @@ + "Lines", + 'threshold' => 51, + 'ratio' => (double)$coverage->project->directory->totals->lines["percent"] + ], + [ + 'name' => "Methods", + 'threshold' => 47, + 'ratio' => (double)$coverage->project->directory->totals->methods["percent"] + ], + [ + 'name' => "Functions", + 'threshold' => 0, + 'ratio' => (double)$coverage->project->directory->totals->functions["percent"] + ], + [ + 'name' => "Classes", + 'threshold' => 22, + 'ratio' => (double)$coverage->project->directory->totals->classes["percent"] + ], + [ + 'name' => "Traits", + 'threshold' => 0, + 'ratio' => (double)$coverage->project->directory->totals->traits["percent"] + ] +]; + +foreach ($totals as $total) { + if ($total['ratio'] < $total['threshold']) { + echo "{$total['name']} coverage failed! \r\n"; + echo "{$total['name']} coverage: {$total['ratio']}% \r\n"; + echo "Threshold: {$total['threshold']}% \r\n"; + $coverageIsOk = false; + $errorMessage .= "{$total['name']} coverage failed! \r\n"; + } else { + echo "{$total['name']} coverage success: {$total['ratio']}% \r\n"; + } +} + +if (!$coverageIsOk) { + echo "Coverage failed! \r\n"; + echo $errorMessage; + exit(1); +} else { + echo "Coverage success! \r\n"; +} diff --git a/phpunit.ci.xml b/phpunit.ci.xml index 16e49caa..9e3d4842 100644 --- a/phpunit.ci.xml +++ b/phpunit.ci.xml @@ -15,5 +15,12 @@ tests/Unit/Legacy - + + + ./src/* + + ./src/vendor/* + + + From a26dabddc3c56aa37d4df1c1f12814a8f3e29519 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:10:11 +0100 Subject: [PATCH 2/2] code coverage remove permission and change ubuntu version --- .github/workflows/code-coverage.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/code-coverage.yaml b/.github/workflows/code-coverage.yaml index e42a2cb0..3b6466ff 100644 --- a/.github/workflows/code-coverage.yaml +++ b/.github/workflows/code-coverage.yaml @@ -7,12 +7,9 @@ on: branches: [develop, master] workflow_dispatch: ~ -permissions: - contents: write - jobs: ci: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 defaults: run: working-directory: ./