Skip to content

Commit 5dc3405

Browse files
authored
Tests
1 parent edacb66 commit 5dc3405

File tree

11 files changed

+139
-11
lines changed

11 files changed

+139
-11
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"test:unit": "phpunit --testsuite=unit",
3737
"test:integration": "phpunit --testsuite=integration",
3838
"test:watch": "phpunit --watch",
39-
"coverage:check": "phpunit --coverage-clover=coverage.xml --colors=never && php scripts/coverage-check.php 80"
39+
"coverage:check": "phpunit --coverage-clover=coverage.xml --colors=never && php scripts/coverage-check.php 80",
40+
"phpcs": "phpcs",
41+
"phpcs:tests": "phpcs --standard=tests/phpcs.xml tests/",
42+
"phpcbf": "phpcbf",
43+
"phpstan": "phpstan analyse"
4044
}
4145
}

scripts/coverage-check.php

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@
33
* Coverage Threshold Checker
44
*
55
* Simple script to check if code coverage meets the minimum threshold.
6+
* This is a CLI script and does not require WordPress escaping functions.
67
*
78
* @package Simple_WP_Optimizer
89
* @subpackage Tests
910
* @since 1.5.12
1011
*/
1112

13+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
15+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
16+
17+
<?php
18+
/**
19+
* Coverage Threshold Checker
20+
*
21+
* Simple script to check if code coverage meets the minimum threshold.
22+
* This is a CLI script and does not require WordPress escaping functions.
23+
*
24+
* @package Simple_WP_Optimizer
25+
* @subpackage Tests
26+
* @since 1.5.12
27+
*/
28+
29+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
30+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
31+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
32+
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
33+
1234
/**
1335
* Check coverage threshold from PHPUnit clover XML output
1436
*
@@ -18,43 +40,53 @@
1840
*/
1941
function check_coverage_threshold( $clover_file = 'coverage.xml', $threshold = 80.0 ) {
2042
if ( ! file_exists( $clover_file ) ) {
21-
echo "Coverage file not found: {$clover_file}\n";
43+
fwrite( STDERR, sprintf( "Coverage file not found: %s\n", $clover_file ) );
2244
return false;
2345
}
2446

2547
$xml = simplexml_load_file( $clover_file );
2648
if ( ! $xml ) {
27-
echo "Failed to parse coverage XML file\n";
49+
fwrite( STDERR, "Failed to parse coverage XML file\n" );
2850
return false;
2951
}
3052

3153
$metrics = $xml->project->metrics;
3254
if ( ! $metrics ) {
33-
echo "No metrics found in coverage file\n";
55+
fwrite( STDERR, "No metrics found in coverage file\n" );
3456
return false;
3557
}
3658

3759
$covered_statements = (float) $metrics['coveredstatements'];
3860
$total_statements = (float) $metrics['statements'];
3961

4062
if ( $total_statements === 0.0 ) {
41-
echo "No statements found to check coverage\n";
63+
fwrite( STDERR, "No statements found to check coverage\n" );
4264
return false;
4365
}
4466

4567
$coverage_percentage = ( $covered_statements / $total_statements ) * 100;
4668

47-
printf( "Code Coverage: %.2f%% (%d/%d statements)\n",
48-
$coverage_percentage,
49-
(int) $covered_statements,
50-
(int) $total_statements
69+
fwrite(
70+
STDOUT,
71+
sprintf(
72+
"Code Coverage: %.2f%% (%d/%d statements)\n",
73+
$coverage_percentage,
74+
(int) $covered_statements,
75+
(int) $total_statements
76+
)
5177
);
5278

5379
if ( $coverage_percentage >= $threshold ) {
54-
printf( "✅ Coverage meets threshold (%.2f%% >= %.2f%%)\n", $coverage_percentage, $threshold );
80+
fwrite(
81+
STDOUT,
82+
sprintf( "✅ Coverage meets threshold (%.2f%% >= %.2f%%)\n", $coverage_percentage, $threshold )
83+
);
5584
return true;
5685
} else {
57-
printf( "❌ Coverage below threshold (%.2f%% < %.2f%%)\n", $coverage_percentage, $threshold );
86+
fwrite(
87+
STDERR,
88+
sprintf( "❌ Coverage below threshold (%.2f%% < %.2f%%)\n", $coverage_percentage, $threshold )
89+
);
5890
return false;
5991
}
6092
}

tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
// Define test constants
1317
if ( ! defined( 'SIMPLE_WP_OPTIMIZER_TESTING' ) ) {
1418
define( 'SIMPLE_WP_OPTIMIZER_TESTING', true );

tests/helpers/TestHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
/**
1317
* Test Helper Class
1418
*

tests/integration/IntegrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
use WP_Mock\Tools\TestCase;
1317

1418
/**

tests/phpcs.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Test Files Coding Standards">
3+
<description>PHP coding standards for test files</description>
4+
5+
<!-- Files to check -->
6+
<file>.</file>
7+
8+
<!-- Use PSR-12 coding standards for test files -->
9+
<rule ref="PSR12">
10+
<!-- Allow longer lines for test assertions -->
11+
<exclude name="Generic.Files.LineLength"/>
12+
</rule>
13+
14+
<!-- Allow direct use of parse_url, strip_tags, etc. in tests -->
15+
<rule ref="WordPress.WP.AlternativeFunctions">
16+
<exclude name="WordPress.WP.AlternativeFunctions"/>
17+
</rule>
18+
19+
<!-- Allow output without escaping in test files -->
20+
<rule ref="WordPress.Security.EscapeOutput">
21+
<exclude name="WordPress.Security.EscapeOutput"/>
22+
</rule>
23+
24+
<!-- Allow localhost and URLs in test files -->
25+
<rule ref="WordPress.WP.EnqueuedResources">
26+
<exclude name="WordPress.WP.EnqueuedResources"/>
27+
</rule>
28+
29+
<!-- Allow direct echo/print in test output -->
30+
<rule ref="WordPress.Security.EscapeOutput.OutputNotEscaped">
31+
<exclude name="WordPress.Security.EscapeOutput.OutputNotEscaped"/>
32+
</rule>
33+
34+
<!-- ENFORCE SPACES INSTEAD OF TABS -->
35+
<rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
36+
<rule ref="Generic.WhiteSpace.ScopeIndent">
37+
<properties>
38+
<property name="indent" value="4"/>
39+
<property name="tabIndent" value="false"/>
40+
</properties>
41+
</rule>
42+
43+
<!-- File formatting -->
44+
<rule ref="Generic.Files.ByteOrderMark"/>
45+
<rule ref="Generic.Files.LineEndings">
46+
<properties>
47+
<property name="eolChar" value="\n"/>
48+
</properties>
49+
</rule>
50+
51+
<!-- Show progress -->
52+
<arg value="p"/>
53+
54+
<!-- Show sniff codes in all reports -->
55+
<arg value="s"/>
56+
57+
<!-- Use colors in output -->
58+
<arg name="colors"/>
59+
60+
</ruleset>

tests/unit/HeaderCleanupTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
use WP_Mock\Tools\TestCase;
1317

1418
/**

tests/unit/OptionsTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
use WP_Mock\Tools\TestCase;
1317

1418
/**

tests/unit/PerformanceTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
use WP_Mock\Tools\TestCase;
1317

1418
/**

tests/unit/RenderingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @since 1.5.12
1010
*/
1111

12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url
14+
// phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
15+
1216
use WP_Mock\Tools\TestCase;
1317

1418
/**

0 commit comments

Comments
 (0)