|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Coverage Threshold Checker |
| 4 | + * |
| 5 | + * Simple script to check if code coverage meets the minimum threshold. |
| 6 | + * |
| 7 | + * @package Simple_WP_Optimizer |
| 8 | + * @subpackage Tests |
| 9 | + * @since 1.5.12 |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Check coverage threshold from PHPUnit clover XML output |
| 14 | + * |
| 15 | + * @param string $clover_file Path to clover XML file. |
| 16 | + * @param float $threshold Minimum coverage percentage required. |
| 17 | + * @return bool True if coverage meets threshold. |
| 18 | + */ |
| 19 | +function check_coverage_threshold( $clover_file = 'coverage.xml', $threshold = 80.0 ) { |
| 20 | + if ( ! file_exists( $clover_file ) ) { |
| 21 | + echo "Coverage file not found: {$clover_file}\n"; |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + $xml = simplexml_load_file( $clover_file ); |
| 26 | + if ( ! $xml ) { |
| 27 | + echo "Failed to parse coverage XML file\n"; |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + $metrics = $xml->project->metrics; |
| 32 | + if ( ! $metrics ) { |
| 33 | + echo "No metrics found in coverage file\n"; |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + $covered_statements = (float) $metrics['coveredstatements']; |
| 38 | + $total_statements = (float) $metrics['statements']; |
| 39 | + |
| 40 | + if ( $total_statements === 0.0 ) { |
| 41 | + echo "No statements found to check coverage\n"; |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + $coverage_percentage = ( $covered_statements / $total_statements ) * 100; |
| 46 | + |
| 47 | + printf( "Code Coverage: %.2f%% (%d/%d statements)\n", |
| 48 | + $coverage_percentage, |
| 49 | + (int) $covered_statements, |
| 50 | + (int) $total_statements |
| 51 | + ); |
| 52 | + |
| 53 | + if ( $coverage_percentage >= $threshold ) { |
| 54 | + printf( "✅ Coverage meets threshold (%.2f%% >= %.2f%%)\n", $coverage_percentage, $threshold ); |
| 55 | + return true; |
| 56 | + } else { |
| 57 | + printf( "❌ Coverage below threshold (%.2f%% < %.2f%%)\n", $coverage_percentage, $threshold ); |
| 58 | + return false; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Run the coverage check |
| 63 | +$threshold = isset( $argv[1] ) ? (float) $argv[1] : 80.0; |
| 64 | +$clover_file = isset( $argv[2] ) ? $argv[2] : 'coverage.xml'; |
| 65 | + |
| 66 | +$success = check_coverage_threshold( $clover_file, $threshold ); |
| 67 | +exit( $success ? 0 : 1 ); |
0 commit comments