rosio/wordpress-testing-harness

Makes testing plugins and themes with WordPress and Composer much easier

v1.1.1 2014-06-11 22:50 UTC

README

This library is meant to make it easier to test plugins and themes with the WordPress runtime via PHPUnit.
This is basically just a rewrite of kurtpayne/wordpress-unit-tests.

Usage:

  1. Add this library, your testing library, and your WordPress deployment of choice to your plugin or themes composer.php:

        "autoload-dev": {
            "psr-4": {
                "Plugin\\SomePlugin\\": "tests/"
            }
        },
        "require-dev": {
            "phpunit/phpunit": "~3.7",
            "rosio/wordpress-testing-harness": "dev-master",
            "johnpbloch/wordpress": "~3.8"
        },

    You don't actually have to include WordPress, and instead have it download to the correct folder during testing (which means you can then test multiple versions of WordPress easily), but it's recommended to add one for easy local testing.

  2. Configure your phpunit.xml.dist

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit backupGlobals="false"
             beStrictAboutTestSize="true"
             backupStaticAttributes="false"
             bootstrap="tests/bootstrap.php"
             colors="true"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false"
             syntaxCheck="false"
    >
        <testsuites>
            <testsuite name="Application Test Suite">
                <directory suffix="Test.php">./tests/</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    
  3. Configure your phpunit's tests/bootstrap.php file:

    <?php
    
    require_once __DIR__ . '/../vendor/autoload.php';
    
    $bootstrapper = new Rosio\WordPressTestingHarness\Bootstrapper(array(
        'bootstrap-path' => __FILE__,
    
        'db-name' => 'wp_test',
        'db-user' => 'root',
        'db-pass' => 'root',
        'db-host' => 'localhost',
        'db-prefix' => 'wptests_',
        'db-charset' => '',
        'db-collate' => '',
        'wplang' => '',
    
        'wordpress-path' => __DIR__ . '/../wordpress',
        'domain' => 'wp.localhost',
        'admin-email' => 'admin@example.org',
        'blog-title' => 'Tests',
        'admin-password' => 'admin',
    
        'always-reinstall' => true,
    
        'php-binary' => 'php',
        'testdata-path' => __DIR__ . '/../data',
    ));
    
    // Require in plugin
    require __DIR__ . '/../index.php';
  4. Write your first test!

    <?php
    namespace Plugin\SomePlugin;
    
    use WP_UnitTestCase;
    use \Mockery as m;
    
    class MainPluginTest extends WP_UnitTestCase
    {
        public function setUp()
        {
    
        }
    
        public function tearDown()
        {
            m::close();
        }
    
        public function testPluginIsCreated()
        {
            $this->assertInstanceOf('Plugin\SomePlugin\MainPlugin', getSomePlugin());
        }
    }