https://github.com/polylang/wp-phpunit
https://github.com/polylang/wp-phpunit
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/polylang/wp-phpunit
- Owner: polylang
- Created: 2020-12-10T16:52:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-07-18T13:28:48.000Z (11 months ago)
- Last Synced: 2025-10-18T22:49:30.731Z (8 months ago)
- Language: PHP
- Size: 91.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Polylang PHPUnit
A code library for WP Syntex projects, containing:
- scripts to install the WordPress test suite,
- scripts to install the required plugins and themes,
- scripts for building and distributing the project,
- bootstraps allowing to init unit and integration tests,
- helpers for the tests.
## How to
### Install the test suite
The test suite is installed in **your project**'s `tmp` folder.
To tell the installation script how to connect to your database, you can create a `DB-CONFIG` file at the root of your project and formatted like follow (the file is not versioned with git of course).
Each line is optional, the default values are:
```txt
db_host: localhost
db_name: wordpress_tests
db_user: root
db_pass: '' (an empty string)
```
### Install the dependencies
The plugins and themes are installed in **your project**'s `tmp` folder.
Create a `install-plugins.sh` file that launches all the downloads. Example:
```bash
#!/usr/bin/env bash
. "$PWD/vendor/wpsyntex/wp-phpunit/bin/wp-download-tools.sh"
mkdir -p $WP_PLUGINS_DIR
mkdir -p $WP_THEMES_DIR
# Install WP All Import Pro.
downloadPluginFromEdd wp-all-import-pro 'WP All Import' https://www.wpallimport.com
# Install Polylang Pro.
downloadPolylangPro
# Install Polylang for WooCommerce.
downloadPolylangForWoocommerce
# Install WooCommerce.
downloadWoocommerce
# Install TwentyFourteen.
downloadThemeFromRepository twentyfourteen
```
Note: two variables are available for you to use: `$WP_PLUGINS_DIR` and `$WP_THEMES_DIR`. They contain the path to `tmp/plugins` and `tmp/themes` respectively, where the plugins and themes are installed. Those folders are not created by default so don't forget to do `mkdir -p $WP_PLUGINS_DIR` and/or `mkdir -p $WP_THEMES_DIR` like in the previous example, before using the download functions.
Premium plugins installed from EDD need a bit more attention because they need a license key. You can provide it by creating a `LICENSE-CODES` file at the root of your project and formatted like follow (the file is not versioned with git of course):
```txt
license {PLUGIN-SLUG}:{YOUR-LICENSE}
site {PLUGIN-SLUG}:{YOUR-SITE}
```
Depending on EDD config, the `site` line may not be required.
Also, if your plugin from EDD doesn't require a license key, do the following:
```txt
license {PLUGIN-SLUG}:none
```
### Composer scripts
Then you can create composer scripts like these ones for example:
```json
{
"scripts": {
"install-suite": "vendor/wpsyntex/wp-phpunit/bin/install-wp-suite.sh",
"install-suite-with-db": "vendor/wpsyntex/wp-phpunit/bin/install-wp-suite.sh latest true",
"install-plugins": "Tests/bin/install-plugins.sh",
"install-tests": [
"@install-suite",
"@install-plugins"
],
"install-tests-with-db": [
"@install-suite-with-db",
"@install-plugins"
],
"build": "vendor/wpsyntex/wp-phpunit/bin/build.sh",
"build-update": "vendor/wpsyntex/wp-phpunit/bin/build.sh -- -u",
"dist": "vendor/wpsyntex/wp-phpunit/bin/distribute.sh -- polylang-foobar"
},
"scripts-descriptions": {
"install-suite": "Installs the WordPress tests suite (without installing the database).",
"install-suite-with-db": "Installs the WordPress tests suite (with database creation).",
"install-plugins": "Installs dependencies needed for integration tests.",
"install-tests": "Installs both the WordPress tests suite (without installing the database) and the dependencies needed for integration tests.",
"install-tests-with-db": "Installs both the WordPress tests suite (with database creation) and the dependencies needed for integration tests, without creating the database.",
"build": "Builds the project.",
"build-update": "Builds the project (runs `composer update` instead of `composer install`).",
"dist": "Makes the zip file to distribute the project release."
}
}
```
### Integration tests
#### Bootstrap for integration tests
Example for your `bootstrap.php` file:
```php
[
'polylang-pro/polylang.php' => true,
'woocommerce/woocommerce.php' => [
'group' => 'WithWoo',
'init' => '\WP_Syntex\Polylang_Phpunit\Integration\WooCommerce\Bootstrap::initWoocommerce',
],
'polylang-wc/polylang-wc.php' => [
'group' => 'WithWoo',
],
dirname( dirname( __DIR__ ) ) . '/polylang-foobar.php' => true,
], // A list of plugins to include and activate.
]
);
```
The previous code will:
- Require `polylang.php` and `polylang-foobar.php`.
- Require `woocommerce.php` and `polylang-wc.php` if `--group=WithWoo` is used when invoking phpunit.
- Call `\WP_Syntex\Polylang_Phpunit\Integration\WooCommerce\Bootstrap::initWoocommerce()` after `woocommerce.php` is required.
#### Use the trait in your integration tests
You can use the trait `WP_Syntex\Polylang_Phpunit\Integration\TestCaseTrait` in your integration tests.
Hint: if you need to create your own methods `set_up()`, `wpSetUpBeforeClass`, etc, you can't simply call the ones from the trait with `parent::` like you would with parent/child classes. In this case you can do that:
```php
traitSetUp();
// Do more things.
}
}
```
If you need to list some plugins among the "active ones" (`get_option( 'active_plugins' )`) for your integration tests (this may be required for some plugins that test which plugins are active), you can create a class like this one in your tests:
```php
*/
protected $activePlugins = [
'wp-all-import-pro/wp-all-import-pro.php',
];
}
```
Some helpers are available in your tests, like toying with reflections or getting test data. You can find them in the `TestCaseTrait` trait.
### Unit tests
#### Bootstrap for unit tests
Example for your `bootstrap.php` file:
```php