PHPackages                             inpsyde/wp-tests-starter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. inpsyde/wp-tests-starter

ActiveLibrary[Testing &amp; Quality](/categories/testing)

inpsyde/wp-tests-starter
========================

A package that helps you setting up WordPress integration test environments quickly.

1.0.2(10y ago)251.1k[4 issues](https://github.com/inpsyde/WP-Tests-Starter/issues)1MITPHPPHP &gt;=5.3.0

Since Nov 17Pushed 1y ago6 watchersCompare

[ Source](https://github.com/inpsyde/WP-Tests-Starter)[ Packagist](https://packagist.org/packages/inpsyde/wp-tests-starter)[ RSS](/packages/inpsyde-wp-tests-starter/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (1)Versions (6)Used By (1)

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](https://github.com/inpsyde/WP-Tests-Starter/tree/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](https://github.com/inpsyde/wordpress-dev). 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:

```
public function testPersistBook(): void {

    $book = new Book('The Da Vinci Code', 'Dan Brown', '2003');
    $testee = new BookRepository($GLOBALS['wpdb']);
    $testee->persist($book); // maps book to WP_Post object and post meta

    self::assertGreaterThan(0, $book->id());

    $wpPost = get_post($book->id());

    self::assertSame('The Da Vinci Code', $wpPost->post_title);
    self::assertSame('2003', get_post_meta($book->id(), '_publishing_year', true));
    self::assertSame('Dan Brown', get_post_meta($book->id(), '_author', true));
}
```

No mocks required. Just WordPress working inside a PHPUnit test case.

Installation
------------

[](#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 repository](https://github.com/inpsyde/wordpress-dev)
- `phpunit/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:

```
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/WordPress/wordpress-develop"
    }
]
```

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
----------------

[](#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:

```

            ./tests/integration

```

The `tests/boostrap.php` finally loads Wp Tests Starter and WordPress:

```
