From a1b07b6ef3a4feb36834ae898234c9e8daa5bccd Mon Sep 17 00:00:00 2001 From: james Date: Fri, 30 May 2025 21:00:15 +0800 Subject: [PATCH] new src --- .gitea/workflows/test.yml | 56 ++++++++++++++++++++++++++++++++++++--- src/Calculator.php | 8 ++++-- tests/CalculatorTest.php | 11 +++++++- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 1f8b49b..4a9b7b3 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -10,17 +10,65 @@ on: jobs: test: runs-on: phpunit_test - container: php:8.1-cli-alpine + container: php8.1-node-gitea-runner + steps: - name: Checkout code uses: actions/checkout@v4 - name: Install dependencies run: | - apk add --no-cache git unzip - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + # 安装依赖 composer install --no-interaction --prefer-dist - name: Run PHPUnit tests run: | - vendor/bin/phpunit --configuration phpunit.xml \ No newline at end of file + XDEBUG_MODE=coverage vendor/bin/phpunit --configuration phpunit.xml --verbose + + - name: Show coverage.xml + run: | + cat coverage.xml + + - name: Check coverage threshold (100% required) + run: | + # 提取项目级别的覆盖率数据 + total_statements=$(xmlstarlet sel -t -v "//project/metrics/@statements" coverage.xml) + covered_statements=$(xmlstarlet sel -t -v "//project/metrics/@coveredstatements" coverage.xml) + + total_methods=$(xmlstarlet sel -t -v "//project/metrics/@methods" coverage.xml) + covered_methods=$(xmlstarlet sel -t -v "//project/metrics/@coveredmethods" coverage.xml) + + if [ -z "$total_statements" ] || [ -z "$covered_statements" ] || \ + [ -z "$total_methods" ] || [ -z "$covered_methods" ]; then + echo "❌ 无法提取覆盖率数据,请检查 coverage.xml 格式" + exit 1 + fi + + # 计算覆盖率百分比 + statement_coverage=$(echo "scale=2; $covered_statements / $total_statements * 100" | bc) + method_coverage=$(echo "scale=2; $covered_methods / $total_methods * 100" | bc) + + echo "📈 行覆盖率: $statement_coverage%" + echo "📉 函数覆盖率: $method_coverage%" + + # 提取未覆盖的行号 statement level + uncovered_lines=$(xmlstarlet sel -t -m "//file/line[@type='stmt' and @count=0]" -v "concat(../@name, '()', ':', @num)" coverage.xml | sort -u) + + # 输出未覆盖的行 + if [ -n "$uncovered_lines" ]; then + echo -e "❌ 以下行未被覆盖:\n$uncovered_lines" + fi + + + # 检查是否达标 + if [ "$(echo "$statement_coverage < 99.9" | bc)" -eq 1 ]; then + echo "❌ 行覆盖率未达到 100%" + exit 1 + fi + + if [ "$(echo "$method_coverage < 99.9" | bc)" -eq 1 ]; then + echo "❌ 函数覆盖率未达到 100%" + exit 1 + fi + + echo "✅ 行覆盖率和函数覆盖率均达标" \ No newline at end of file diff --git a/src/Calculator.php b/src/Calculator.php index 164ba7d..143aee2 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -11,7 +11,11 @@ class Calculator public function subtract($a, $b) { - $a = 1111; - return $a - $b; + if($a>99){ + return $a - $b; + }else{ + return $a - 2*$b; + } + } } diff --git a/tests/CalculatorTest.php b/tests/CalculatorTest.php index 5b28347..7e3a9d1 100644 --- a/tests/CalculatorTest.php +++ b/tests/CalculatorTest.php @@ -10,4 +10,13 @@ class CalculatorTest extends TestCase $calc = new Calculator(); $this->assertEquals(5, $calc->add(2, 3)); } -} + + public function testSubtract() + { + $calc = new Calculator(); + $this->assertEquals(1108, $calc->subtract(1111, 3)); // 1111 - 3 = 1108 + + $calc2 = new Calculator(); + $this->assertEquals(44, $calc2->subtract(50, 3)); // 50 - 3*2 = 44 + } +} \ No newline at end of file