Skip to content

Commit cd391a8

Browse files
authored
Initial release (#1)
* add composer scripts * quote wp_tests_dir and wp_core_dir * fix linting * require phpcs * add lint workflow * add autotag action * update the up-to-date message * drop closing brace to new line * update readme * add codeowners * add a test that runs the plugin to validate the files were copied * remove --prefer-dist from composer init * use github.head_ref * allow the wpunit-helpers plugin * allow the plugin the right way * use verbose output so we can see what's going on when we require * run a ls to ensure we got a bin/ * remove verbose debug and test for a bin directory * rename job to test * explicitly add exit 1 if any of the files aren't found and remove the -e which we don't need if we're explicitly exiting * subshell the or * remove $
1 parent f540e4b commit cd391a8

File tree

9 files changed

+191
-12
lines changed

9 files changed

+191
-12
lines changed

.github/workflows/autotag.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Tag and Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
permissions:
7+
contents: write
8+
jobs:
9+
tag-release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: pantheon-systems/action-autotag@v0
14+
with:
15+
gh-token: ${{ github.token }}

.github/workflows/lint.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Lint
2+
on: [pull_request]
3+
jobs:
4+
lint:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v3
8+
- name: Install dependencies
9+
run: composer install
10+
- name: Lint
11+
run: composer lint

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Test Plugin
2+
on: [pull_request]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v3
8+
- name: Setup barebones project
9+
run: |
10+
mkdir test_proj && cd test_proj
11+
composer init --name test/test --no-interaction --type=library --stability=dev
12+
composer config repositories.wpunit-helpers '{"type": "vcs", "url": "https://github.com/pantheon-systems/wpunit-helpers.git"}'
13+
composer config allow-plugins.pantheon-systems/wpunit-helpers true
14+
composer require --dev pantheon-systems/wpunit-helpers:dev-${{ github.head_ref }}
15+
- name: Validate that bin files were copied
16+
run: |
17+
cd ${{ github.workspace }}/test_proj
18+
test -d bin || (echo "❌ bin directory not found" >&2 && exit 1)
19+
test -f bin/install-wp-tests.sh || (echo "❌ bin/install-wp-tests.sh not found" >&2 && exit 1)
20+
test -f bin/install-local-tests.sh || (echo "❌ bin/install-local-tests.sh not found" >&2 && exit 1)
21+
test -f bin/phpunit-test.sh || (echo "❌ bin/phpunit-test.sh not found" >&2 && exit 1)
22+
echo "✅ All bin files found"

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @pantheon-systems/cms-platform

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
1-
# wpunit-helpers
1+
# WPUnit Helpers
2+
[![Lint](https://github.com/pantheon-systems/wpunit-helpers/actions/workflows/lint.yml/badge.svg)](https://github.com/pantheon-systems/wpunit-helpers/actions/workflows/lint.yml) ![GitHub](https://img.shields.io/github/license/pantheon-systems/wpunit-helpers) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/pantheon-systems/wpunit-helpers) [![Unofficial Support](https://img.shields.io/badge/Pantheon-Unofficial%20Support-yellow?logo=pantheon&color=FFDC28)](https://docs.pantheon.io/oss-support-levels#unofficial-support)
3+
24
Unified scripts for installing and running automated WP Unit Tests.
5+
6+
## What is this?
7+
8+
This is a set of scripts that can be used in a WordPress plugin or theme repository to run automated tests using the WP Unit Test Framework built on top of PHPUnit. The Composer plugin will install these scripts into the dependent project's `bin` directory, allowing you to add helper scripts to your `composer.json`.
9+
10+
## What's included?
11+
12+
* `install-wp-tests.sh` - The _de facto_ standard script for installing the WP Unit Test Framework.
13+
* `install-local-tests.sh` - A helper script for installing WP Unit Tests locally. See [Local Testing](#local-testing) for more information.
14+
* `phpunit-test.sh` - A helper script for running WP Unit Tests that is intended for use in CI.
15+
16+
## Installation
17+
18+
Use Composer to install this package as a development dependency:
19+
20+
```bash
21+
composer require --dev pantheon-systems/wpunit-helpers
22+
```
23+
24+
On installation, the Composer plugin will copy the scripts into the `bin` directory of the dependent project. You can then add the scripts to your `composer.json`:
25+
26+
```json
27+
{
28+
"scripts": {
29+
"phpunit": "phpunit --do-not-cache-result",
30+
"test": "@phpunit",
31+
"test:install": "bin/install-local-tests.sh --no-db",
32+
"test:install:withdb": "bin/install-local-tests.sh"
33+
}
34+
}
35+
```
36+
37+
## Local Testing
38+
The `install-local-tests.sh` script is highly configurable to allow for a variety of local environment setups. Any parameter that could be passed into `install-wp-tests.sh` is set up as an optional flag in `install-local-tests.sh`. By default, the script with no flags will assume that a new database should be created as `root` with no password.
39+
40+
### Flags
41+
42+
#### `--no-db`
43+
This flag will skip the database creation step. This is useful if you are using a local database that is already set up.
44+
45+
#### `--dbname`
46+
This flag will set the name of the database to be created. The default value is `wordpress_test`.
47+
48+
#### `--dbuser`
49+
This flag will set the username of the database user to be created. The default value is `root`.
50+
51+
#### `--dbpass`
52+
This flag will set the password of the database user to be created. The default value is an empty string.
53+
54+
#### `--dbhost`
55+
This flag will set the host of the database to be created. The default value is `127.0.0.1`.
56+
57+
#### `--wpversion`
58+
This flag will set the version of WordPress to be installed. The default value is `latest`. Using `nightly` here will use the latest nightly build of WordPress.

bin/phpunit-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DIRNAME=$(dirname "$0")
77
bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 latest
88
echo "Running PHPUnit on Single Site"
99
composer phpunit
10-
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
10+
rm -rf "$WP_TESTS_DIR" "$WP_CORE_DIR"
1111

1212
bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 nightly true
1313
echo "Running PHPUnit on Single Site (Nightly WordPress)"

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,29 @@
1010
}
1111
],
1212
"minimum-stability": "dev",
13+
"prefer-stable": true,
1314
"require": {
1415
"composer-plugin-api": "^2.3"
1516
},
17+
"require-dev": {
18+
"squizlabs/php_codesniffer": "^3.7"
19+
},
1620
"autoload": {
1721
"psr-4": {
1822
"Pantheon\\WPUnitHelpers\\": "src/"
1923
}
2024
},
2125
"extra": {
2226
"class": "Pantheon\\WPUnitHelpers\\Plugin"
27+
},
28+
"scripts": {
29+
"shellcheck": "find bin/ -name \"*.sh\" | grep -v \"install-wp-tests.sh\" | xargs shellcheck",
30+
"phpcs": "phpcs --standard=PSR2 src/",
31+
"phplint": "find src/ -name '*.php' -exec php -l {} \\;",
32+
"lint": [
33+
"@shellcheck",
34+
"@phpcs",
35+
"@phplint"
36+
]
2337
}
2438
}

composer.lock

Lines changed: 61 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Plugin.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public static function getSubscribedEvents()
3535
'post-install-cmd' => ['copyBinDirectory'],
3636
'post-update-cmd' => ['copyBinDirectory'],
3737
);
38-
}
38+
}
3939

4040
public function copyBinDirectory()
41-
{
42-
$io = $this->io;
41+
{
42+
$io = $this->io;
4343
$vendorDir = $this->composer->getConfig()->get('vendor-dir');
4444
$binDir = $vendorDir . '/pantheon-systems/wpunit-helpers/bin';
4545
$targetDir = dirname($vendorDir) . '/bin';
@@ -82,12 +82,14 @@ public function copyBinDirectory()
8282
\"phpunit\": \"phpunit --do-not-cache-result\",
8383
\"test\": \"@phpunit\",
8484
\"test:install\": \"bin/install-local-tests.sh --no-db\",
85-
\"test:install:withdb\": \"bin/install-local-tests.sh\"}";
85+
\"test:install:withdb\": \"bin/install-local-tests.sh\"
86+
}";
8687

8788
if (!$filesAreIdentical) {
88-
$io->write("Done copying files into /bin. You can now add the following to your composer.json file: \n $composerIncludes");
89+
$io->write("Done copying files into /bin.");
90+
$io->write("You can now add the following to your composer.json file: \n $composerIncludes");
8991
} else {
90-
$io->write("/bin files are already up to date");
92+
$io->write("/bin files are up to date");
9193
}
9294
}
93-
}
95+
}

0 commit comments

Comments
 (0)