Skip to content

Commit edacb66

Browse files
authored
Tests
1 parent 31afc89 commit edacb66

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"phpstan/phpstan": "^1.0",
2424
"10up/wp_mock": "^1.0",
2525
"mockery/mockery": "^1.5",
26-
"phpunit/php-code-coverage": "^9.2",
27-
"sebastianbergmann/phpcov": "^8.2"
26+
"phpunit/php-code-coverage": "^9.2"
2827
},
2928
"config": {
3029
"allow-plugins": {
@@ -33,10 +32,10 @@
3332
},
3433
"scripts": {
3534
"test": "phpunit",
36-
"test:coverage": "phpunit --coverage-html coverage --coverage-clover coverage.xml",
35+
"test:coverage": "phpunit --coverage-html coverage --coverage-clover coverage.xml --coverage-text",
3736
"test:unit": "phpunit --testsuite=unit",
3837
"test:integration": "phpunit --testsuite=integration",
3938
"test:watch": "phpunit --watch",
40-
"coverage:check": "php scripts/check-coverage.php"
39+
"coverage:check": "phpunit --coverage-clover=coverage.xml --colors=never && php scripts/coverage-check.php 80"
4140
}
4241
}

scripts/check-coverage.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Simple coverage check script for Simple WP Optimizer
5+
*
6+
* Checks that code coverage meets the minimum threshold of 80%
7+
*
8+
* @package Simple_WP_Optimizer
9+
* @subpackage Tests
10+
* @since 1.5.12
11+
*/
12+
13+
// Check if coverage.xml exists
14+
if (!file_exists('coverage.xml')) {
15+
echo "❌ Coverage report not found. Run 'composer test:coverage' first.\n";
16+
exit(1);
17+
}
18+
19+
// Parse the clover XML coverage report
20+
$xml = simplexml_load_file('coverage.xml');
21+
22+
if (!$xml) {
23+
echo "❌ Failed to parse coverage report.\n";
24+
exit(1);
25+
}
26+
27+
// Extract coverage metrics
28+
$metrics = $xml->project->metrics;
29+
$covered = (int) $metrics['coveredstatements'];
30+
$statements = (int) $metrics['statements'];
31+
32+
if ($statements === 0) {
33+
echo "❌ No statements found in coverage report.\n";
34+
exit(1);
35+
}
36+
37+
$coverage = ($covered / $statements) * 100;
38+
$threshold = 80.0;
39+
40+
echo "📊 Code Coverage Report\n";
41+
echo "========================\n";
42+
echo sprintf("Total Statements: %d\n", $statements);
43+
echo sprintf("Covered Statements: %d\n", $covered);
44+
echo sprintf("Coverage: %.2f%%\n", $coverage);
45+
echo sprintf("Threshold: %.1f%%\n", $threshold);
46+
echo "\n";
47+
48+
if ($coverage >= $threshold) {
49+
echo sprintf("✅ Coverage of %.2f%% meets the required threshold of %.1f%%\n", $coverage, $threshold);
50+
exit(0);
51+
} else {
52+
echo sprintf("❌ Coverage of %.2f%% is below the required threshold of %.1f%%\n", $coverage, $threshold);
53+
echo sprintf("Need to improve coverage by %.2f%% to meet requirements.\n", $threshold - $coverage);
54+
exit(1);
55+
}

scripts/coverage-check.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)