Download the PHP package inpsyde/wp-tests-starter without Composer
On this page you can find all versions of the php package inpsyde/wp-tests-starter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download inpsyde/wp-tests-starter
More information about inpsyde/wp-tests-starter
Files in inpsyde/wp-tests-starter
Package wp-tests-starter
Short Description A package that helps you setting up WordPress integration test environments quickly.
License MIT
Informations about the package wp-tests-starter
WP Tests Starter
Note: You're reading the documentation for the development branch towards version 2.0.0. You'll find the documentation for version 1.0 at the branch version-1
Wp Tests starter is a library that assist you in setting up integration tests for your plugin or library with WordPress core using the official wordpress-develop repository. The main difference to unit tests is that you don't need (and typically don't want) to mock any WordPress function. Instead, you have a fully booted WordPress core in place with an actual connection to a database server.
So if you're mapping your objects to WordPress posts in a ORM-style way your integration test would look like this:
No mocks required. Just WordPress working inside a PHPUnit test case.
Installation
In order to use Wp Tests Starter you need: a PHP environment with Composer and a MySQL server with a dedicated test database. This database should be completely ephemeral, so do not use any database that contains important data. You'll also need the following four Composer packages installed as dev dependencies:
inpsyde/wp-tests-starter
yoast/phpunit-polyfills
wordpress/wordpress
taken from wordpress-develop repositoryphpunit/phpunit
As the last one is not available on packagist.org, you'll have to add the repo manually to your composer.json file by adding:
Now you can run
composer require --dev inpsyde/wp-tests-starter yoast/phpunit-polyfills wordpress/wordpress phpunit/phpunit
Note that this will take a while as Composer will analyze the entire WordPress repository on GitHub. (Once a composer.lock is in place it will go faster on the next install run)
Setup your tests
To set up your PHPUnit tests you need two files in place: phpunit.xml.dist
and a boostrap.php
which gets loaded by
PHPUnit before your actual tests are executed. The shown examples of these two files assume a directory structure of your
library like this:
├ src/
| └ MyModule.php
├ tests/
| ├integration/
| | └ MyModuleTest.php
| └boostrap.php
├ vendor/
├ composer.json
└ phpunit.xml.dist
The following example of the phpunit.xml.dist file tells PHPUnit where the test files resides and contains the database credentials as an environment variable:
The tests/boostrap.php
finally loads Wp Tests Starter and WordPress:
These files just show a short way to run integration tests with WP Tests Starter. You have several other configuration
options available through the methods of the WpTestsStarter
object.
Run PHPUnit
With this configuration in place you can run PHPUnit to execute all test classes in tests/integration
with
vendor/bin/phpunit
On every run, WP Starter will write the configuration to vendor/wordpress/wordpress/wp-config.php
and load the WordPress
internal bootstrap script which ensures installation of the database tables for example.
Configuration
DB Url
In order to not have to maintain several environment variables you can pass all databse credentials and options via a single parameter like this:
mysql://user:password@localhost:3306/test_db?table_prefix=wp_tests_&charset=utf8mb4&collation=utf8_general_ci
This URL can be passed to WpTestsStarter
either by constructor parameter or by useDbUrl()
method:
The URL example above would turn into the following WordPress constants and globals:
Basically all the values of this URL are optional but keep in mind that you want to have a complete configuration for WordPress. Alternatively you can use explicit values:
License
This repository is a free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version. See LICENSE for complete license.
Back to top