2017 © Pedro Peláez
 

library wordpress-test-environment

Boot wordpress to test your code in an actual wordpress environment

image

makeweb/wordpress-test-environment

Boot wordpress to test your code in an actual wordpress environment

  • Tuesday, July 17, 2018
  • by MakeWeb
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 50 % Grown

The README.md

Wordpress Test Environment

Easily pull Wordpress in to your plugin, library or package as a composer development dependency and test your code in an actual wordpress environment., (*1)

This package aims to support developers who wish to test code which depends on Wordpress in two key ways:, (*2)

1) Boot wordpress to allow usage of wordpress database and global functions, (*3)

2) Simulate actual HTTP requests to the wordpress application, (*4)

Installation

From command line:, (*5)

composer require --dev makeweb/wordpress-test-environment

Basic Usage

1) Once installed, Create a new database in your local MySQL installation., (*6)

2) Create a .env file in the base directory of the codebase you are testing and add the following to it (updating the credentials to match your newly created database and local environment):, (*7)

DATABASE_NAME=database_name_here
DATABASE_USER=database_user_here
DATABASE_PASSWORD=database_password_here
DATABASE_HOST=127.0.0.1
SITE_URL=example.test

3) Initialise Wordpress in your parent test case class, (*8)

Pass the relative path to the directory which contains your .env file into loadEnvFrom(), (*9)

use MakeWeb\WordpressTestEnvironment\Wordpress;
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase;
{
    public function setUp()
    {
        $this->wordpress = (new Wordpress)
            ->loadEnvFrom(__DIR__.'/..');
    } 
}

4) Usage in your tests, (*10)

Boot only

If you simply wish to boot wordpress to allow access to the database and Wordpress global functions. Add the following lines to the start of your test., (*11)

In your test file

class MyPluginTest extends TestCase
{
    public function the_main_plugin_class_can_use_wordpress_global_functions()
    {
        $this->wordpress->boot();

        $this->assertEquals('http://example.test', (new MyPluginClass)->url());
    }
}

In your main plugin file or source code

Here we are just calling a native Wordpress function get_site_url(). If everything is set up correctly the test above will pass with the code below because get_site_url() has been defined by wordpress., (*12)

Note: Make sure you are including the plugin file in your test bootstrap file manually or with an autoloader like composer, (*13)

class MyPluginClass
{
    public function url()
    {
        return get_site_url();
    }
}

Test HTTP requests to your wordpress application

Sometimes your theme, plugin or library might actually handle HTTP requests (for example, if you are creating an API), or perhaps you just want to test the way your code affects the actual html output of the application. The Wordpress class exposes a get method which simulates an actual HTTP GET request to the given url. Wordpress, and your dependent code will be booted and executed as it would be in production. We don't need to call $this->wordpress->boot() when using the HTTP methods because it is done for us automatically., (*14)

In your test file

class MyPluginTest extends TestCase
{
    public function the_main_plugin_class_can_use_wordpress_global_functions()
    {
        // To boot the plugin which we are testing when booting wordpress, pass the relative path
        // to the main plugin file into the withPlugin() method. Installation and activation hooks
        // will not be called.
        $this->wordpress->withPlugin(__DIR__.'/../edd-sl-deployer.php');

        // Request the front page of the wordpress site and capture the result
        $response = $this->wordpress->get('/');

        // Assert that the response returned a 200 http status code
        $response->assertSuccessful();

        // Assert that our text was output in the html
        $response->assertSee('Hello World');
    }
}

In your plugin file or source code

/**
 * Plugin Name: Hello World Outputter
 * Plugin Description: An enterprise ready solution to output the text 'Hello World' on your website
 * Plugin URI: https://hello-world-outputter.com
 * Version: 1.0.0
 * Author: Andrew Feeney
**/

class MyPluginClass
{
    public function boot()
    {
        echo('Hello World');
    }
}

(new MyPluginClass)->boot();

The Versions

17/07 2018

dev-master

9999999-dev

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrew Feeney

13/07 2018

1.1.2

1.1.2.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

11/07 2018

1.1.1

1.1.1.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

11/07 2018

dev-dev

dev-dev

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

07/07 2018

1.1.0

1.1.0.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

10/04 2018

1.0.2

1.0.2.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

10/04 2018

1.0.1

1.0.1.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney

10/04 2018

1.0.0

1.0.0.0

Boot wordpress to test your code in an actual wordpress environment

  Sources   Download

MIT

The Requires

 

by Andrew Feeney