From 636f836c18bb4edf79fa4aa9ed1e2444ff4ce0c8 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 21:59:31 +0300 Subject: [PATCH 1/8] Fix code coverage report --- src/Model/ComparingRow.php | 15 ++++-- .../Command/CloverCompareCommandTest.php | 34 ++++++++++++ tests/Functional/FunctionalTestCase.php | 12 +++++ tests/Integration/ComparatorTest.php | 53 +++++++++++++++++++ tests/Integration/IntegrationTestCase.php | 12 +++++ tests/Unit/Model/ComparingRowTest.php | 16 +++--- tests/Unit/Reader/BaselineReaderTest.php | 16 +++--- tests/fixtures/baseline/baseline.json | 16 +++--- 8 files changed, 145 insertions(+), 29 deletions(-) create mode 100644 tests/Functional/Command/CloverCompareCommandTest.php create mode 100644 tests/Functional/FunctionalTestCase.php create mode 100644 tests/Integration/ComparatorTest.php create mode 100644 tests/Integration/IntegrationTestCase.php diff --git a/src/Model/ComparingRow.php b/src/Model/ComparingRow.php index 0dded02..0f93b44 100644 --- a/src/Model/ComparingRow.php +++ b/src/Model/ComparingRow.php @@ -29,8 +29,8 @@ final class ComparingRow public function __construct(string $name, float $old, float $new) { $this->name = $name; - $this->old = round($old * 100, 2); - $this->new = round($new * 100, 2); + $this->old = round($old, 2); + $this->new = round($new, 2); $this->progress = $this->new - $this->old; } @@ -58,9 +58,14 @@ public function getValues(): array return [ 'name' => $this->name, - 'old' => str_pad(number_format($this->old, 2), 6, ' ', \STR_PAD_LEFT) . ' %', - 'new' => str_pad(number_format($this->new, 2), 6, ' ', \STR_PAD_LEFT) . ' %', - 'progress' => str_pad($progressPrefix . number_format($this->progress, 2), 7, ' ', \STR_PAD_LEFT) . ' %', + 'old' => $this->formatPercentage($this->old, 6), + 'new' => $this->formatPercentage($this->new, 6), + 'progress' => $this->formatPercentage($this->progress, 7, $progressPrefix), ]; } + + private function formatPercentage(float $value, int $length, string $prefix = ''): string + { + return str_pad($prefix . number_format($value, 2, '.', ''), $length, ' ', \STR_PAD_LEFT) . ' %'; + } } diff --git a/tests/Functional/Command/CloverCompareCommandTest.php b/tests/Functional/Command/CloverCompareCommandTest.php new file mode 100644 index 0000000..d980c2e --- /dev/null +++ b/tests/Functional/Command/CloverCompareCommandTest.php @@ -0,0 +1,34 @@ + 'methods', + 'old' => ' 1.00 %', + 'new' => ' 50.00 %', + 'progress' => ' +49.00 %', + ], + [ + 'name' => 'conditionals', + 'old' => ' 30.00 %', + 'new' => ' 75.00 %', + 'progress' => ' +45.00 %', + ], + [ + 'name' => 'statements', + 'old' => ' 50.00 %', + 'new' => ' 83.33 %', + 'progress' => ' +33.33 %', + ], + [ + 'name' => 'elements', + 'old' => ' 70.00 %', + 'new' => ' 87.50 %', + 'progress' => ' +17.50 %', + ], + ]; + $baselinePath = __DIR__ . '/../fixtures/baseline/baseline_v2.json'; + $baselineReader = (new BaselineReaderFactory())->createReader($baselinePath); + $cloverPath = __DIR__ . '/../fixtures/clover/clover.xml'; + $cloverReader = new CloverReader($cloverPath); + $results = (new Comparator($baselineReader, $cloverReader))->compare(); + $actual = array_map(static function (ComparingRow $row): array { + return $row->getValues(); + }, $results->getRows()); + + self::assertEquals($expected, $actual); + } +} diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php new file mode 100644 index 0000000..376fbbc --- /dev/null +++ b/tests/Integration/IntegrationTestCase.php @@ -0,0 +1,12 @@ + [ [ 'name' => 'any string', - 'old' => ' 10.00 %', - 'new' => ' 10.00 %', + 'old' => ' 0.10 %', + 'new' => ' 0.10 %', 'progress' => ' 0.00 %', ], 0.1, @@ -68,7 +68,7 @@ public function getDataForTestNumbersFormattingOnJsonSerialise(): iterable 'progress' => '+100.00 %', ], 0.0, - 1.0, + 100.0, ]; yield '-100 %' => [ @@ -78,7 +78,7 @@ public function getDataForTestNumbersFormattingOnJsonSerialise(): iterable 'new' => ' 0.00 %', 'progress' => '-100.00 %', ], - 1.0, + 100.0, 0.0, ]; @@ -89,8 +89,8 @@ public function getDataForTestNumbersFormattingOnJsonSerialise(): iterable 'new' => ' 0.10 %', 'progress' => ' +0.09 %', ], - 0.0001, - 0.001, + 0.01, + 0.1, ]; yield 'not showable difference' => [ @@ -100,8 +100,8 @@ public function getDataForTestNumbersFormattingOnJsonSerialise(): iterable 'new' => ' 0.01 %', 'progress' => ' +0.01 %', ], - 0.00001, - 0.0001, + 0.001, + 0.01, ]; } } diff --git a/tests/Unit/Reader/BaselineReaderTest.php b/tests/Unit/Reader/BaselineReaderTest.php index a915393..c7eda1f 100644 --- a/tests/Unit/Reader/BaselineReaderTest.php +++ b/tests/Unit/Reader/BaselineReaderTest.php @@ -59,14 +59,14 @@ public function getDataForTestPositiveFlow(): iterable { yield [ [ - 'methods' => 1, - 'conditionals' => 3, - 'statements' => 5, - 'elements' => 7, - 'coveredmethods' => 2, - 'coveredconditionals' => 4, - 'coveredstatements' => 6, - 'coveredelements' => 8, + 'methods' => 2, + 'conditionals' => 4, + 'statements' => 6, + 'elements' => 8, + 'coveredmethods' => 1, + 'coveredconditionals' => 3, + 'coveredstatements' => 5, + 'coveredelements' => 7, ], __DIR__ . '/../../fixtures/baseline/baseline.json', ]; diff --git a/tests/fixtures/baseline/baseline.json b/tests/fixtures/baseline/baseline.json index 6b200a7..74cac03 100644 --- a/tests/fixtures/baseline/baseline.json +++ b/tests/fixtures/baseline/baseline.json @@ -1,10 +1,10 @@ { - "methods": 1, - "coveredmethods": 2, - "conditionals": 3, - "coveredconditionals": 4, - "statements": 5, - "coveredstatements": 6, - "elements": 7, - "coveredelements": 8 + "methods": 2, + "coveredmethods": 1, + "conditionals": 4, + "coveredconditionals": 3, + "statements": 6, + "coveredstatements": 5, + "elements": 8, + "coveredelements": 7 } From 1f100c1d27e4d680f31d438465a8b2ee12287e47 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:07:44 +0300 Subject: [PATCH 2/8] Add CHANGELOG.md --- CHANGELOG.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6bb1dc8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,44 @@ +CHANGELOG +========= + +2.0.1 +----- +* Bug fixes: + * Fix comparing and displaying of data used for comparing of Clover code coverage report with its baseline. +* Minors: + * Configure matrix for better dependencies testing. + +2.0.0 +----- +* Feature: + * Changed format of Clover code coverage baseline to improve its readability. + * Added support for old version of Clover code coverage baseline. +* Backward compatibility breaks: + * Used package `symfony/console` instead of custom console calls handling. +* Deprecations: + * Scripts `bin/pccb_clover_build_baseline` and `bin/pccb_clover_compare` are deprecated. + Single script `bin/pccb` should be used for all commands calls. +* Minors: + * Configured Docker for dev purposes. + +1.1.0 +----- +* Features: + * Beautified report of clover baseline comparing results when used option "verbose". + * Added compatibility with PHP 7.1. +* Minors: + * Added check by PHPStan. + +1.0.0 +----- +* Features: + * Implemented baseline builder. +* Minors: + * Code refactored in OOP approach. + * Removed messages about fallback to the default options values. + * Configured running of automated tests on GitHub. + +0.1 +--- +* Features: + * Initial implementation of baseline comparing. From 8856d8c904124ab58c5b02c6268d9fe548b846c1 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:12:01 +0300 Subject: [PATCH 3/8] Update PHPStan baseline --- phpstan-baseline.neon | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fd09b69..72b34d4 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,11 +15,21 @@ parameters: count: 2 path: tests/Unit/ComparatorTest.php + - + message: "#^Trying to mock an undefined method read\\(\\) on class \\.$#" + count: 2 + path: tests/Unit/ComparatorTest.php + - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 1 path: tests/Unit/Model/ConsoleTableTest.php + - + message: "#^Trying to mock an undefined method getValues\\(\\) on class \\.$#" + count: 1 + path: tests/Unit/Model/ConsoleTableTest.php + - message: "#^Expression \"\\$coverage\\['any_key'\\]\" on a separate line does not do anything\\.$#" count: 2 From a6840dd804613359c42dd0d3202c161ecc02a124 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:16:43 +0300 Subject: [PATCH 4/8] Update composer scripts --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 1 + composer.json | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46d3826..54ac85d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: uses: ramsey/composer-install@v2 - name: Run test suite - run: composer run-script test + run: composer run-script phpunit phpstan: runs-on: ubuntu-latest @@ -82,4 +82,4 @@ jobs: uses: ramsey/composer-install@v2 - name: PHPStan analyse - run: vendor/bin/phpstan analyse + run: composer run-script phpstan-analise diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb1dc8..b27b73a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Fix comparing and displaying of data used for comparing of Clover code coverage report with its baseline. * Minors: * Configure matrix for better dependencies testing. + * Update scripts in composer config and running of them on GitGub Actions. 2.0.0 ----- diff --git a/composer.json b/composer.json index 205a4c5..2d59ced 100644 --- a/composer.json +++ b/composer.json @@ -44,8 +44,13 @@ "phpunit/phpunit": "^7.5|^9.5" }, "scripts": { - "test": "vendor/bin/phpunit", - "phpstan-analise": "vendor/bin/phpstan analyse" + "test": [ + "@phpunit", + "@phpstan-analise" + ], + "phpunit": "vendor/bin/phpunit", + "phpstan-analise": "vendor/bin/phpstan analyse", + "phpstan-update": "vendor/bin/phpstan analyse --generate-baseline phpstan-baseline.neon" }, "suggest": { "phpunit/phpunit": "PHPUnit must be used to generate code coverage report." From 2457ae980695f7893d6c0de47bfcf5dc0af9e200 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:50:14 +0300 Subject: [PATCH 5/8] Add script for replacing of packages versions --- .github/workflows/ci.yml | 8 +++----- bin/dev/set_composer_versions | 9 +++++++++ composer.json | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 bin/dev/set_composer_versions diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54ac85d..2ff0cee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,14 +37,12 @@ jobs: - uses: actions/checkout@v3 - - name: Set versions - run: | - sed -i 's/\^3.4|\^4.0|\^5.0|\^6.0/${{ matrix.symfony-version }}/g' composer.json - sed -i 's/\^7.5|\^9.5/${{ matrix.phpunit-version }}/g' composer.json - - name: Validate composer.json and composer.lock run: composer validate --strict + - name: Set versions + run: composer run-script set_versions ${{ matrix.symfony-version }} ${{ matrix.phpunit-version }} + - name: Cache Composer packages id: composer-cache uses: actions/cache@v3 diff --git a/bin/dev/set_composer_versions b/bin/dev/set_composer_versions new file mode 100644 index 0000000..4bdf346 --- /dev/null +++ b/bin/dev/set_composer_versions @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +SYMFONY_NEW_VERSION=$1 +PHPUNIT_NEW_VERSION=$2 + +sed -i "s/\^3.4|\^4.0|\^5.0|\^6.0/${SYMFONY_NEW_VERSION}/g" composer.json +sed -i "s/\^7.5|\^9.5/${PHPUNIT_NEW_VERSION}/g" composer.json diff --git a/composer.json b/composer.json index 2d59ced..e6bbd65 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ ], "phpunit": "vendor/bin/phpunit", "phpstan-analise": "vendor/bin/phpstan analyse", - "phpstan-update": "vendor/bin/phpstan analyse --generate-baseline phpstan-baseline.neon" + "phpstan-update": "vendor/bin/phpstan analyse --generate-baseline phpstan-baseline.neon", + "set_versions": "sh bin/dev/set_composer_versions" }, "suggest": { "phpunit/phpunit": "PHPUnit must be used to generate code coverage report." From 81eb161e5efb3068e029b5e6bdfacd45bde77bb3 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:53:00 +0300 Subject: [PATCH 6/8] Fix test for CI compatibility --- tests/Functional/Command/CloverCompareCommandTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Functional/Command/CloverCompareCommandTest.php b/tests/Functional/Command/CloverCompareCommandTest.php index d980c2e..60946b3 100644 --- a/tests/Functional/Command/CloverCompareCommandTest.php +++ b/tests/Functional/Command/CloverCompareCommandTest.php @@ -10,7 +10,8 @@ final class CloverCompareCommandTest extends FunctionalTestCase { public function testPositiveFlow(): void { - $command = 'bin/pccb pccb:clover:compare -vv' + $command = 'php ' . __DIR__ . '/../../../bin/pccb' + . ' pccb:clover:compare -vv' . ' -b tests/fixtures/baseline/baseline_v2.json' . ' -c tests/fixtures/clover/clover.xml'; exec($command, $output, $resultCode); From 09fe92a08649ab30fe0289e2e980175543fef39a Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:55:12 +0300 Subject: [PATCH 7/8] Remove excess composer caching on CI --- .github/workflows/ci.yml | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2ff0cee..438a34b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,15 +43,6 @@ jobs: - name: Set versions run: composer run-script set_versions ${{ matrix.symfony-version }} ${{ matrix.phpunit-version }} - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v3 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - name: Install dependencies uses: ramsey/composer-install@v2 @@ -67,15 +58,6 @@ jobs: - uses: actions/checkout@v3 - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v3 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - name: Install dependencies uses: ramsey/composer-install@v2 From 5cd9dc708f8fc6252bc91e56b980f00de50bc6ea Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikau Date: Wed, 1 Mar 2023 23:59:50 +0300 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b27b73a..5cfb08e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ CHANGELOG * Fix comparing and displaying of data used for comparing of Clover code coverage report with its baseline. * Minors: * Configure matrix for better dependencies testing. - * Update scripts in composer config and running of them on GitGub Actions. + * Update scripts in composer config and running of them on GitGub Actions. + * Added script to replace required versions of packages. 2.0.0 -----