Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPUnit tests + Linting for WP plugin #26

Merged
merged 16 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Linting
name: Linting (Browser extension)

on:
pull_request:
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/wp-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Linting (WP plugin)

on:
pull_request:
types: [ opened, synchronize ]

jobs:
phpcs:
name: phpcs
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
coverage: 'none'
tools: composer, cs2pr

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
composer-options: "--no-progress --no-ansi --no-interaction"

- name: Run PHPCS
run: src/plugin/vendor/bin/phpcs --standard=./phpcs.xml -q --report=checkstyle | cs2pr --notices-as-warnings
57 changes: 57 additions & 0 deletions .github/workflows/wp-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Tests (WP plugin)

on:
pull_request:
types: [ opened, synchronize ]

jobs:
phpunit:
name: PHPUnit ${{ matrix.php }}
runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
ports:
- 3306/tcp
env:
MYSQL_ROOT_PASSWORD: password
# Set health checks to wait until mysql has started
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 3

strategy:
fail-fast: false
matrix:
php: [ '8.3' ]

steps:
- name: Check out Git repository
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: 'none'
tools: composer

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
composer-options: "--no-progress --no-ansi --no-interaction"

- name: Install WordPress test setup
run: bash tests/bin/install-wp-tests.sh wordpress_test root password 127.0.0.1:${{ job.services.mysql.ports[3306] }} latest

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run tests
env:
WP_TESTS_DIR: /tmp/wordpress-tests-lib/
PHPUNIT_UNDER_GITHUB_ACTIONS: true
run: src/plugin/vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ firefox/
node_modules/
test-results/
build/
vendor/

.phpunit.result.cache
14 changes: 14 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"core": "WordPress/WordPress",
"phpVersion": "8.3",
"env": {
"tests": {
"phpVersion": "8.3"
}
},
"plugins": [ "./src/plugin" ],
"mappings": {
"wp-content/phpunit.xml.dist": "./phpunit.xml.dist",
"wp-content/tests": "./tests"
}
}
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Try WordPress
Data Liberation browser extension powered by WordPress Playground.

## Development environment
This repo provides a development environment that facilitates developing the browser extension, using the [`web-ext`](https://extensionworkshop.com/documentation/develop/getting-started-with-web-ext/) tool.
This repo provides a development environment that facilitates:

- Developing the browser extension, using the [`web-ext`](https://extensionworkshop.com/documentation/develop/getting-started-with-web-ext/) tool.
- Developing the WordPress plugin that is used under WordPress Playground, using [`wp-env`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/).

## Development environment - Browser extension

First install required dependencies:

Expand All @@ -28,6 +32,49 @@ The extension will also automatically reload whenever you modify source files.

> Please note that at the moment not all `web-ext` features work on chrome, so firefox is the recommended browser for developing this project, since it provides the best developer experience. One example of a `web-ext` feature that doesn't currently work on chrome is to have the developer tools and extension console automatically open when the extension loads.

## Development environment - WordPress plugin

First install required dependencies:

```shell
composer install
```

The development environment requires [wp-env](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/), you can install it with:

```shell
npm install -g @wordpress/env
```

Start the development environment:
```shell
composer run dev:start
```

You will need docker engine running for this command to work, since `wp-env` uses container that runs on docker engine.
This command starts the WordPress environment and sets up the permalink structure.

To stop the development environment:
```shell
composer run dev:stop
```

Additionally, there is also support for `xdebug`, `phpcs` and `phpcbf`:

For debugging with Xdebug:
```shell
composer run dev:debug
```

To run linting on the codebase:
```shell
composer run lint
```

To automatically fix linting issues:
```shell
composer run lint:fix
```

## Building for production
You can build both the firefox and chrome versions of the extension with the following command. The resulting files will be under the `build/firefox` and `build/chrome` directories, respectively.
Expand All @@ -36,9 +83,21 @@ You can build both the firefox and chrome versions of the extension with the fol
npm run build
```

> We would soon have the build & release pipeline for publishing the plugin to WP.org repo.

## Running tests

You can run tests with:

**For browser extension:**

```shell
npm run test
```

**For WordPress plugin:**

```shell
composer run dev:test
```
This command runs the tests in the WordPress environment using PHPUnit.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "wordpress/try-wordpress",
"description": "",
"license": "",
"require-dev": {
"phpunit/phpunit": "^9.6.16",
"yoast/phpunit-polyfills": "^2.0.0",
"wp-coding-standards/wpcs": "^3.0"
},
"scripts": {
"lint": "phpcs --standard=phpcs.xml -s",
"lint:fix": "phpcbf --standard=phpcs.xml",
"dev:start": "wp-env start && wp-env run cli wp rewrite structure '/%postname%/'",
"dev:debug": "wp-env start --xdebug",
"dev:stop": "wp-env stop",
"dev:destroy": "yes | wp-env destroy",
"dev:test": "wp-env run tests-cli --env-cwd=wp-content/ plugins/plugin/vendor/bin/phpunit"
},
"config": {
"vendor-dir": "src/plugin/vendor",
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Loading
Loading