From b34ce8758799ee140562370e7249eaae57f8593a Mon Sep 17 00:00:00 2001 From: Fernando Lira Date: Tue, 16 Nov 2021 13:53:16 -0300 Subject: [PATCH] =?UTF-8?q?v3.12.3=20=F0=9F=9A=80=20(#181)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :bug: Fix the PR cover checker when there is no test (#189) This fixes an issue that is workflow failure when the pull request does not contain tests and Added hotfix branch filter to check tests * fix(helper): 🐛 fixed float to int convertion * feat(bin): ✨ new command sync with container * build: 📦 new patch version of mercardo pago for magento 2 v3.12.3 Co-authored-by: Gustavo Brito Co-authored-by: Douglas --- .editorconfig | 3 + CHANGELOG.md | 6 + Makefile | 5 +- README.md | 2 +- bin/pull-request-coverage.sh | 7 +- bin/run-linters.sh | 1 + bin/run-sync-files.sh | 12 ++ composer.json | 2 +- src/MercadoPago/Core/Helper/Round.php | 30 +++-- src/MercadoPago/Core/composer.json | 2 +- src/MercadoPago/Core/etc/module.xml | 2 +- .../Test/Unit/Helper/RoundTest.php | 86 ++++++++++++ .../Test/pull-request-coverage-checker.php | 123 +++++++++++++----- 13 files changed, 228 insertions(+), 53 deletions(-) create mode 100644 bin/run-sync-files.sh create mode 100644 src/MercadoPago/Test/Unit/Helper/RoundTest.php diff --git a/.editorconfig b/.editorconfig index a38c0db9..d8900f85 100644 --- a/.editorconfig +++ b/.editorconfig @@ -19,3 +19,6 @@ indent_size = 2 [{composer, auth}.json] indent_size = 4 + +[{Makefile,**.mk}] +indent_style = tab diff --git a/CHANGELOG.md b/CHANGELOG.md index 55794ff2..8246f258 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.12.3] - 2021-11-16 + +### Fixed + +- Fixed round method for integer currencies + ## [3.12.2] - 2021-11-05 ### Fixed diff --git a/Makefile b/Makefile index 87378e8f..b4826e2d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help bash build linter phpcs phpmd phpstan test +.PHONY: help bash build linter phpcs phpmd phpstan test sync-files help: @grep -E '^[a-zA-Z-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-15s %s\n", $$1, $$2}' @@ -22,3 +22,6 @@ phpstan: ## Run and validate code standards with stan test: ## Run and validate tests with phpunit @sh bin/run-test.sh + +sync-files: ## Sync your local files to Magento 2 Container + @sh bin/run-sync-files.sh diff --git a/README.md b/README.md index 030ea8ac..41772138 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

-# Magento 2 - Mercado Pago Module (v3.12.2) +# Magento 2 - Mercado Pago Module (v3.12.3) The Mercado Pago plugin for Magento 2 allows you to expand the functionalities of your online store and offer a unique payment experience for your customers. diff --git a/bin/pull-request-coverage.sh b/bin/pull-request-coverage.sh index e0e69a72..35824690 100644 --- a/bin/pull-request-coverage.sh +++ b/bin/pull-request-coverage.sh @@ -1,9 +1,14 @@ #!/bin/bash +echo "Getting pull request head branch..." +export PHPUNIT_HEAD_BRANCH=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/PluginAndPartners/cart-magento2/pulls/${PR_NUMBER} \ +| jq ".head.ref" \ +| xargs) + echo "Getting pull request files..." export PHPUNIT_FILES=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/PluginAndPartners/cart-magento2/pulls/${PR_NUMBER}/files \ | jq ".[].filename" \ | grep -E 'php"$' \ | xargs) -php magento2/app/code/MercadoPago/Test/pull-request-coverage-checker.php clover.xml 40 $PHPUNIT_FILES +php magento2/app/code/MercadoPago/Test/pull-request-coverage-checker.php clover.xml 40 $PHPUNIT_HEAD_BRANCH $PHPUNIT_FILES diff --git a/bin/run-linters.sh b/bin/run-linters.sh index de6d2e02..0844f834 100644 --- a/bin/run-linters.sh +++ b/bin/run-linters.sh @@ -1,5 +1,6 @@ #!/bin/bash +sh bin/run-sync-files.sh sh bin/run-phpcs.sh sh bin/run-phpstan.sh sh bin/run-phpmd.sh diff --git a/bin/run-sync-files.sh b/bin/run-sync-files.sh new file mode 100644 index 00000000..7c81dc47 --- /dev/null +++ b/bin/run-sync-files.sh @@ -0,0 +1,12 @@ +#!bin/bash +echo "\n" +echo 🐘🔍 '\033[01;33m RUNNING SYNC FILES TO MAGENTO 2 CONTAINER \033[0m' +echo "\n" + +docker cp src/. magento-php:/var/www/html/magento2/app/code + +if [ $? -eq 0 ]; then + echo ✅ "\033[01;32m SYNC EXECUTED SUCCESSFULLY \n \033[0m" +else + echo 🚫 "\033[01;31m SYNC FAILED \n \033[0m" +fi diff --git a/composer.json b/composer.json index d8efe2da..cce785c5 100755 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ } ], "type": "magento2-module", - "version": "3.12.2", + "version": "3.12.3", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/MercadoPago/Core/Helper/Round.php b/src/MercadoPago/Core/Helper/Round.php index ec4ae3b2..2bef9b6c 100644 --- a/src/MercadoPago/Core/Helper/Round.php +++ b/src/MercadoPago/Core/Helper/Round.php @@ -11,46 +11,52 @@ class Round */ const COUNTRIES_WITH_INTEGER_PRICE = [ 'MLC', - 'MLO', + 'MCO', ]; + /** * Get rounded value with site id * - * @param float $value + * @param float $value * @param string $siteId * @return float|integer */ public static function roundWithSiteId($value, $siteId) { - $round = (float) number_format($value, 2, '.', ''); - if (in_array($siteId, self::COUNTRIES_WITH_INTEGER_PRICE, true)) { - return (int) $round; + return (int) number_format($value, 0, '.', ''); } - return $round; - } + return (float) number_format($value, 2, '.', ''); + + }//end roundWithSiteId() + /** * Get rounded value with site id * - * @param float $value + * @param float $value * @return float */ public static function roundWithoutSiteId($value) { return (float) number_format($value, 2, '.', ''); - } + + }//end roundWithoutSiteId() + /** * Get rounded value with site id * - * @param float $value + * @param float $value * @return integer */ public static function roundInteger($value) { return (int) number_format($value, 0, '.', ''); - } -} + + }//end roundInteger() + + +}//end class diff --git a/src/MercadoPago/Core/composer.json b/src/MercadoPago/Core/composer.json index f74ced0b..c3c77c78 100644 --- a/src/MercadoPago/Core/composer.json +++ b/src/MercadoPago/Core/composer.json @@ -2,7 +2,7 @@ "name": "mercadopago/core", "description": "Mercado Pago Magento 2 Plugin", "type": "magento2-module", - "version": "3.12.2", + "version": "3.12.3", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/MercadoPago/Core/etc/module.xml b/src/MercadoPago/Core/etc/module.xml index cc07878a..719d420c 100644 --- a/src/MercadoPago/Core/etc/module.xml +++ b/src/MercadoPago/Core/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/src/MercadoPago/Test/Unit/Helper/RoundTest.php b/src/MercadoPago/Test/Unit/Helper/RoundTest.php new file mode 100644 index 00000000..60c2cec3 --- /dev/null +++ b/src/MercadoPago/Test/Unit/Helper/RoundTest.php @@ -0,0 +1,86 @@ +assertEquals(2, $result); + + $result = Round::roundInteger(1.5); + $this->assertEquals(2, $result); + } + + public function testRoundIntToDown() + { + $result = Round::roundInteger(1.4); + $this->assertEquals(1, $result); + + $result = Round::roundInteger(1.1); + $this->assertEquals(1, $result); + } + + public function testRoundFloatToUp() + { + $result = Round::roundWithoutSiteId(99.4999); + $this->assertEquals(99.5, $result); + + $result = Round::roundWithoutSiteId(55.999); + $this->assertEquals(56, $result); + } + + public function testRoundFloatToDown() + { + $result = Round::roundWithoutSiteId(66.404444); + $this->assertEquals(66.40, $result); + + $result = Round::roundWithoutSiteId(75.890000); + $this->assertEquals(75.89, $result); + } + + public function testRoundWithSiteFloatToUp() + { + $result = Round::roundWithSiteId(142.87777, 'MLB'); + $this->assertEquals(142.88, $result); + + $result = Round::roundWithSiteId(545.999, 'MLA'); + $this->assertEquals(546, $result); + } + + public function testRoundWithSiteFloatToDown() + { + $result = Round::roundWithSiteId(66.672333, 'MLM'); + $this->assertEquals(66.67, $result); + + $result = Round::roundWithSiteId(755.890000, 'MLU'); + $this->assertEquals(755.89, $result); + } + + public function testRoundWithSiteIntToUp() + { + $result = Round::roundWithSiteId(142.87777, 'MLC'); + $this->assertEquals(143, $result); + + $result = Round::roundWithSiteId(545.999, 'MCO'); + $this->assertEquals(546, $result); + } + + public function testRoundWithSiteIntToDown() + { + $result = Round::roundWithSiteId(66.472333, 'MCO'); + $this->assertEquals(66, $result); + + $result = Round::roundWithSiteId(755.190000, 'MLC'); + $this->assertEquals(755, $result); + } + +}//end class diff --git a/src/MercadoPago/Test/pull-request-coverage-checker.php b/src/MercadoPago/Test/pull-request-coverage-checker.php index fb15a51b..6979a9a0 100644 --- a/src/MercadoPago/Test/pull-request-coverage-checker.php +++ b/src/MercadoPago/Test/pull-request-coverage-checker.php @@ -1,56 +1,109 @@ xpath('//class'); + $totalElements = 0; + $checkedElements = 0; -for ($i = 3; $i < count($argv); $i++) { - $filename = str_replace('src/', '', $argv[$i]); - $filename = str_replace('/', '\\', $filename); - $filename = str_replace('.php', '', $filename); + foreach ($classes as $class) { + if (in_array($class['name'], $pullRequestFiles)) { + $totalElements += (int) $class->metrics['elements']; + $checkedElements += (int) $class->metrics['coveredelements']; + } + } - $pullRequestFiles[] = $filename; + return [ + 'totalElements' => $totalElements, + 'checkedElements' => $checkedElements, + ]; } -if (!file_exists($cloverFile)) { - throw new InvalidArgumentException('Invalid clover file provided'); -} +function parse_pull_request_files($argv) { + $pullRequestFiles = []; + + for ($i = 4; $i < count($argv); $i++) { + $filename = str_replace('src/', '', $argv[$i]); + $filename = str_replace('/', '\\', $filename); + $filename = str_replace('.php', '', $filename); + + if (is_testable($filename)) { + $pullRequestFiles[] = $filename; + print_r($filename . ' is a testable file' . PHP_EOL); + } + } -if (!$percentage) { - throw new InvalidArgumentException('An integer checked percentage must be given as second parameter'); + return $pullRequestFiles; } -if (count($pullRequestFiles) == 0) { - print_r('Pull Request does not contain any php file to check code coverage'); - return; +function is_testable($filename) { + // Add all untestable php files + $whitelist = [ + 'MercadoPago\Test\coverage-checker', + 'MercadoPago\Test\pull-request-coverage-checker', + ]; + + return in_array($filename, $whitelist) ? false : true; } -$xml = new SimpleXMLElement(file_get_contents($cloverFile)); -$classes = $xml->xpath('//class'); -$totalElements = 0; -$checkedElements = 0; +function is_hotfix_branch($branchName) { + return strpos($branchName, 'hotfix'); +} -foreach ($classes as $class) { - if (in_array($class['name'], $pullRequestFiles)) { - $totalElements += (int) $class->metrics['elements']; - $checkedElements += (int) $class->metrics['coveredelements']; +function validate_clover_file($cloverFile) { + if (!file_exists($cloverFile)) { + throw new InvalidArgumentException('Invalid clover file provided'); } } -if ($totalElements == 0 || $checkedElements == 0) { - print_r('Pull Request does not contain tested php files to check code coverage'); - return; +function validate_percentage_param($percentage) { + if (!$percentage) { + throw new InvalidArgumentException('An integer checked percentage must be given as second parameter'); + } } -$coverage = ($checkedElements / $totalElements) * 100; +function validate_pull_request_coverage($totalElements, $checkedElements, $percentage) { + $coverage = ($checkedElements / $totalElements) * 100; + + if ($coverage >= $percentage) { + print_r('Code coverage is ' . $coverage); + print_r(' -> Pull Request OK'); + return; + } -if ($coverage >= $percentage) { - print_r('Code coverage is ' . $coverage); - print_r(' -> Pull Request OK'); - return; + print_r('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); + print_r(' -> Pull Request Rejected'); + + throw new Exception('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); } -print_r('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); -print_r(' -> Pull Request Rejected'); +function execute($argv) { + $branchName = $argv[3]; + $cloverFile = $argv[1]; + $percentage = min(100, max(0, (int) $argv[2])); + $pullRequestFiles = parse_pull_request_files($argv); + + if (!is_hotfix_branch($branchName)) { + validate_clover_file($cloverFile); + validate_percentage_param($percentage); + + $elements = get_elements($cloverFile, $pullRequestFiles); + $totalElements = $elements['totalElements']; + $checkedElements = $elements['checkedElements']; + + if ($totalElements == 0 || $checkedElements == 0) { + if (count($pullRequestFiles) === 0) { + print_r('Pull request does not contain testable files'); + return; + } + + throw new Exception('Pull Request does not contain tested php files to check code coverage'); + } + + validate_pull_request_coverage($totalElements, $checkedElements, $percentage); + + return; + } +} -throw new Exception('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); +execute($argv);