Skip to content

Commit 9b63946

Browse files
authored
Add basic PHPUnit testing setup (#29)
1 parent da89095 commit 9b63946

File tree

8 files changed

+2359
-91
lines changed

8 files changed

+2359
-91
lines changed

.github/workflows/phpunit.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: PHPUnit Tests
2+
3+
on:
4+
push:
5+
branches: [ trunk ]
6+
pull_request:
7+
branches: [ trunk ]
8+
9+
jobs:
10+
test:
11+
name: PHP ${{ matrix.php }}
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
include:
17+
# Minimum supported PHP
18+
- php: '7.4'
19+
# Latest stable PHP
20+
- php: '8.4'
21+
fail-fast: false
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Setup PHP
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: ${{ matrix.php }}
31+
coverage: none
32+
tools: composer:v2
33+
34+
- name: Install dependencies
35+
run: |
36+
composer install --no-progress --prefer-dist --no-interaction
37+
38+
- name: Run PHPUnit
39+
run: composer test:php

.gitignore

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# Standard gitignore file
2-
31
# Node.js
4-
node_modules/
52
npm-debug.log
63
yarn-error.log
74

@@ -12,6 +9,11 @@ logs/
129

1310
# Dependency directories
1411
jspm_packages/
12+
/vendor/
13+
/node_modules/
14+
15+
# WorDBless
16+
/wordpress/
1517

1618
# Optional npm cache directory
1719
.npm
@@ -42,4 +44,6 @@ jspm_packages/
4244
assets/build/**/*.css
4345
assets/build/**/*.map
4446
assets/build/**/*.js
45-
/vendor/
47+
48+
# PHPUnit result cache
49+
.phpunit.result.cache

.phpcs.xml.dist

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
11
<?xml version="1.0"?>
2-
<ruleset>
3-
<!-- Check all PHP files in directory tree by default. -->
4-
<arg name="extensions" value="php"/>
2+
<ruleset name="Secure Custom Fields">
53
<file>.</file>
64

75
<!-- Exclude package files -->
8-
<exclude-pattern>/vendor/*</exclude-pattern>
9-
<exclude-pattern>/node_modules/*</exclude-pattern>
6+
<exclude-pattern>/vendor/</exclude-pattern>
7+
<exclude-pattern>/node_modules/</exclude-pattern>
108
<exclude-pattern>/lang/*</exclude-pattern>
119

12-
<!-- Confirm PHP compat. -->
13-
<config name="testVersion" value="7.4-"/>
14-
<rule ref="PHPCompatibilityWP" />
15-
16-
<!-- Display sniff code -->
17-
<arg value="s"/>
10+
<!-- How to scan -->
11+
<arg value="sp"/> <!-- Show sniff and progress -->
12+
<arg name="colors"/>
13+
<arg name="extensions" value="php"/>
1814

15+
<!-- Rules: WordPress Coding Standards -->
16+
<config name="minimum_supported_wp_version" value="6.0"/>
1917
<rule ref="WordPress">
2018
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" /> <!-- 'acf/hookname' is used throughout. -->
2119
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" /> <!-- This is trivial and not really useful today. -->
2220
<exclude name="WordPress.Files.FileName.InvalidClassFileName" /> <!-- Refactoring of this scale is not in scope yet.-->
2321
</rule>
22+
2423
<rule ref="WordPress.Security.EscapeOutput">
2524
<properties>
26-
<property name="customEscapingFunctions" type="array">
27-
<element value="acf_esc_attrs" /> <!-- This function takes an associated array and escapes both the attr title and attr value. -->
25+
<property name="customEscapingFunctions" type="array">
26+
<element value="acf_esc_attrs" /> <!-- This function takes an associated array and escapes both the attr title and attr value. -->
2827
<element value="acf_esc_html" />
29-
</property>
28+
</property>
3029
</properties>
3130
</rule>
31+
3232
<rule ref="WordPress.Security.ValidatedSanitizedInput">
3333
<properties>
3434
<property name="customSanitizingFunctions" type="array">
3535
<element value="acf_sanitize_request_args" />
3636
</property>
3737
</properties>
3838
</rule>
39+
3940
<rule ref="WordPress.Security.NonceVerification">
4041
<properties>
4142
<property name="customNonceVerificationFunctions" type="array">
4243
<element value="acf_verify_ajax" />
4344
</property>
4445
</properties>
4546
</rule>
47+
48+
<!-- Rules: PHPCompatibility -->
49+
<config name="testVersion" value="7.4-"/>
50+
<rule ref="PHPCompatibilityWP"/>
51+
52+
<!-- Rules: Text Domain -->
4653
<rule ref="WordPress.WP.I18n">
4754
<properties>
4855
<property name="text_domain" type="array">

composer.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wordpress/secure-custom-fields",
33
"description": "Secure Custom Fields",
4-
"type": "project",
4+
"type": "wordpress-plugin",
55
"license": "GPL-2.0-or-later",
66
"authors": [
77
{
@@ -10,15 +10,28 @@
1010
],
1111
"config": {
1212
"allow-plugins": {
13-
"dealerdirect/phpcodesniffer-composer-installer": true
13+
"dealerdirect/phpcodesniffer-composer-installer": true,
14+
"roots/wordpress-core-installer": true
15+
},
16+
"platform": {
17+
"php": "7.4"
1418
}
1519
},
20+
"require": {
21+
"php": ">=7.4"
22+
},
1623
"require-dev": {
24+
"yoast/phpunit-polyfills": "^1.1.0",
25+
"phpunit/phpunit": "^9.6",
1726
"wp-coding-standards/wpcs": "^3.0",
1827
"phpcompatibility/phpcompatibility-wp": "^2.1",
1928
"sirbrillig/phpcs-changed": "^2.11",
2029
"nikic/php-parser": "^4.0",
21-
"symfony/finder": "^5.0|^6.0"
30+
"symfony/finder": "^5.0|^6.0",
31+
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
32+
"phpcompatibility/phpcompatibility-wp": "^2.1.3",
33+
"sirbrillig/phpcs-changed": "^2.11",
34+
"automattic/wordbless": "^0.5.0"
2235
},
2336
"scripts": {
2437
"docs:manifest": "php docs/bin/generate-manifest.php",
@@ -32,6 +45,14 @@
3245
"@docs:fix",
3346
"@docs:lint",
3447
"@docs:manifest"
35-
]
48+
],
49+
"test": "@test:php",
50+
"test:php": "phpunit",
51+
"lint": "@lint:php",
52+
"lint:php": "phpcs",
53+
"format": "@format:php",
54+
"format:php": "phpcbf",
55+
"post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy",
56+
"post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy"
3657
}
3758
}

0 commit comments

Comments
 (0)