Skip to content
Yukihiko Shinoda edited this page Apr 26, 2020 · 2 revisions

💻PHPUnit Environment for WordPress Plugin🌐

Contents:

Tutorial

1. Install Visual Studio Code and Docker Desktop

Download:

2. Set up file sharing

To mount WordPress plugin project directory into environment container, otherwise, you'll get error.

Open settings

Enable file sharing

cf. Installation [Developing inside a Container using Visual Studio Code Remote Development]

3. Clone or download this repository

git clone https://github.com/yukihiko-shinoda/docker-compose-phpunit-wordpress-plugin.git

4. Open Visual Studio Code on root directory

Enter into cloned directory and open Visual Studio Code.

Case when Windows GUI:

Right click margin of folder > Click [Open with Code]

Open Visual Studio Code on root directory

5. Install Remote Development extension

When open Visual Studio Code, the notification dialog about extension recommendations will be presented.

Click [Install All] button. (Leave other dialog at once.)

Install Remote Development extension

6. Set environment variables

After finish to install extension, create .env file:

Create .env file

Then, edit .env to set PATH_TO_INDIVIDUAL_PLUGIN_DIRECTORY key. Value should be the path to WordPress plugin project directory.

Ex:

PATH_TO_INDIVIDUAL_PLUGIN_DIRECTORY=../plugins/staticpress2019

Then, save file.

Edit .env file

7. Start environment

Click on the quick actions Status Bar item in the lower left corner, then, click "Remote-Containers: Reopen in Container".

Reopen in Container

8. Open Workspace

Click [Open Workspace] button.

Open Workspace

Then, you can explore WordPress plugin project files and WordPress libraries, and when you open PHP file, code analysis process will start.

Opened Workspace

After finish code analysis, correct warnings for PHP code will be presented.

Let's open terminal.

Open Terminal

And then, you can execute PHPUnit and PHP_CodeSniffer.

Run PHPUnit

Type:

phpunit

or:

phpcs

and hit enter key, you'll get result.

Result of PHPUnit

Troubleshoot

Fail to restart environment

Before restart, remove database container.

docker stop database && docker rm database

This is limitation since install shell script doesn't have idempotency.

How to test in legacy environment?

Update .env file to comment in DOCKER_IMAGE_PHPUNIT and MYSQL_VERSION

Ex:

DOCKER_IMAGE_PHPUNIT=futureys/phpunit-wordpress-plugin:4.3.22-php7.1.33-apache-buster
MYSQL_VERSION=5.6

You can choose image for legacy environment from repository futureys/phpunit-wordpress-plugin on Docker Hub.

If your required tag is not found, write Issues to order image.

Order template:

  • Which WordPress version you required
  • Which PHP version you required
  • Which MySQL version expected to work togather (if you know)
  • Which WordPress plugin you are developing

Because images are built by matrix, from the perspective of development and build time cost, it's better to reduce kind of images as less as possible.

PHPUnit nor PHP_CodeSniffer can't work well

Has plugin project been initialized? Plugin project should have following files:

  • .phpcs.xml.dist
  • bin/install-wp-tests.sh
  • phpunit.xml.dist
  • tests/bootstrap.php

if these files don't exist, initialize plugin project at first.

Quick reference

cf. Developing inside a Container using Visual Studio Code Remote Development